import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/prescriptions/prescriptions_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/prescriptions/prescription_delivery_orders_list_page.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

class PrescriptionDeliveryOrderSummaryPage extends StatelessWidget {
  PrescriptionDeliveryOrderSummaryPage({super.key});

  late PrescriptionsViewModel prescriptionsViewModel;

  @override
  Widget build(BuildContext context) {
    prescriptionsViewModel = Provider.of<PrescriptionsViewModel>(context, listen: false);
    AppState appState = getIt<AppState>();
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: Column(
        children: [
          Expanded(
            child: CollapsingListView(
              title: LocaleKeys.orderSummary.tr(context: context),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 16.h),
                  Container(
                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.whiteColor,
                      borderRadius: 20.r,
                      hasShadow: true,
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(16.h),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          LocaleKeys.orderDetail.tr(context: context).toText16(isBold: true),
                          SizedBox(height: 16.h),
                          ...List.generate(
                            prescriptionsViewModel.prescriptionDetailsList.length,
                            (index) => Container(
                              margin: EdgeInsets.all(0.0),
                              child: Row(
                                children: <Widget>[
                                  ClipRRect(
                                    borderRadius: BorderRadius.all(
                                      Radius.circular(5.r),
                                    ),
                                    child: Utils.buildImgWithNetwork(
                                      url: prescriptionsViewModel.prescriptionDetailsList[index].imageSRCUrl!,
                                      iconColor: AppColors.blackBgColor,
                                      fit: BoxFit.cover,
                                      width: 60.w,
                                      height: 70.h,
                                    ),
                                  ),
                                  Expanded(
                                    child: Padding(
                                      padding: EdgeInsets.all(8.h),
                                      child: prescriptionsViewModel.prescriptionDetailsList[index].itemDescription!.trim().toText14(isBold: true),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(height: 16.h),
                  Container(
                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.whiteColor,
                      borderRadius: 20.r,
                      hasShadow: true,
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(16.h),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          LocaleKeys.deliveryLocation.tr(context: context).toText16(isBold: true),
                          SizedBox(height: 16.h),
                          ClipRRect(
                            clipBehavior: Clip.hardEdge,
                            borderRadius: BorderRadius.circular(20.r),
                            child: Utils.buildImgWithNetwork(
                              url: "https://maps.googleapis.com/maps/api/staticmap?center=${prescriptionsViewModel.locationGeocodeResponse.results.first.geometry.location.lat},${prescriptionsViewModel.locationGeocodeResponse.results.first.geometry.location.lng}&zoom=15&size=350x165&maptype=roadmap&markers=color:red%7C${prescriptionsViewModel.locationGeocodeResponse.results.first.geometry.location.lat},${prescriptionsViewModel.locationGeocodeResponse.results.first.geometry.location.lng}&key=AIzaSyB6TERnxIr0yJ3qG4ULBZbu0sAD4tGqtng",
                              iconColor: AppColors.blackBgColor,
                              fit: BoxFit.contain,
                              width: double.infinity,
                              height: 165.h,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ).paddingSymmetrical(24.w, 0),
            ),
          ),
          Container(
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
              color: AppColors.whiteColor,
              borderRadius: 24.h,
              hasShadow: true,
            ),
            child: CustomButton(
              text: appState.isArabic() ? "ارسال" : "Submit",
              onPressed: () async {
                LoaderBottomSheet.showLoader(loadingText: LocaleKeys.loadingText.tr(context: context));
                await prescriptionsViewModel.submitPrescriptionDeliveryRequest(
                    latitude: prescriptionsViewModel.locationGeocodeResponse.results.first.geometry.location.lat.toString(),
                    longitude: prescriptionsViewModel.locationGeocodeResponse.results.first.geometry.location.lng.toString(),
                    appointmentNo: prescriptionsViewModel.prescriptionDetailsList.first.appointmentNo.toString(),
                    dischargeID: "0",
                    projectID: prescriptionsViewModel.prescriptionDetailsList.first.projectID.toString(),
                    onSuccess: (val) {
                      LoaderBottomSheet.hideLoader();
                      showCommonBottomSheetWithoutHeight(
                        context,
                        child: Utils.getSuccessWidget(loadingText: LocaleKeys.requestSubmittedSuccessfully.tr(context: context)),
                        callBackFunc: () {
                          Navigator.of(context).pop();
                          prescriptionsViewModel.getPrescriptionOrdersList();
                          Navigator.of(context).push(
                            CustomPageRoute(
                              page: PrescriptionDeliveryOrdersListPage(),
                            ),
                          );
                        },
                        title: "",
                        isCloseButtonVisible: true,
                        isDismissible: false,
                        isFullScreen: false,
                        isAutoDismiss: true
                      );
                    },
                    onError: (err) {
                      LoaderBottomSheet.hideLoader();
                      showCommonBottomSheetWithoutHeight(
                        context,
                        child: Utils.getErrorWidget(loadingText: err),
                        callBackFunc: () {
                          Navigator.of(context).pop();
                        },
                        title: "",
                        isCloseButtonVisible: true,
                        isDismissible: false,
                        isFullScreen: false,
                      );
                    });
              },
              backgroundColor: AppColors.successColor,
              borderColor: AppColors.successColor.withOpacity(0.01),
              textColor: AppColors.whiteColor,
              fontSize: 16.f,
              isBold: true,
              borderRadius: 12.r,
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              height: 50.h,
              icon: AppAssets.prescription_refill_icon,
              iconColor: AppColors.whiteColor,
              iconSize: 20.h,
            ).paddingSymmetrical(24.h, 24.h),
          ),
        ],
      ),
    );
  }
}
