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/inpatient_services/inpatient_services_view_model.dart';
import 'package:hmg_patient_app_new/presentation/inpatient_services/general_consent_page.dart';
import 'package:hmg_patient_app_new/presentation/inpatient_services/general_instructions_page.dart';
import 'package:hmg_patient_app_new/presentation/inpatient_services/inpatient_advance_payment_page.dart';
import 'package:hmg_patient_app_new/presentation/inpatient_services/medical_instructions_page.dart';
import 'package:hmg_patient_app_new/presentation/inpatient_services/widgets/patient_relation_office_widget.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/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';
import 'package:url_launcher/url_launcher.dart';

import '../../generated/locale_keys.g.dart';

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

  late InpatientServicesViewModel inPatientServicesViewModel;

  @override
  Widget build(BuildContext context) {
    inPatientServicesViewModel = Provider.of<InpatientServicesViewModel>(context, listen: false);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: LocaleKeys.InPatientServicesHeader.tr(context: context),
        child: Padding(
          padding: EdgeInsets.all(24.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.generalConsentTitle.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {
                  Navigator.of(context).push(
                    CustomPageRoute(
                      page: GeneralConsentPage(),
                    ),
                  );
                },
              ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.generalInstructions.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {
                  inPatientServicesViewModel.getGeneralInstructions(
                      projectID: inPatientServicesViewModel.isAdmitted
                          ? inPatientServicesViewModel.getAdmissionInfoResponseModel!.projectID!
                          : inPatientServicesViewModel.getAdmissionRequestInfoResponseModel!.projectId!);
                  Navigator.of(context).push(
                    CustomPageRoute(
                      page: GeneralInstructionsPage(),
                    ),
                  );
                },
              ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.medicalInstructions.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {
                  inPatientServicesViewModel.getMedicalInstructions(
                      projectID: inPatientServicesViewModel.isAdmitted
                          ? inPatientServicesViewModel.getAdmissionInfoResponseModel!.projectID!
                          : inPatientServicesViewModel.getAdmissionRequestInfoResponseModel!.projectId!);
                  Navigator.of(context).push(
                    CustomPageRoute(
                      page: MedicalInstructionsPage(),
                    ),
                  );
                },
              ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.mealPlanTitle.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {},
              ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.birthNotificationTitle.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {},
              ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.advancePayment.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {
                  LoaderBottomSheet.showLoader();
                  inPatientServicesViewModel.getInPatientAdvancePaymentRequests(
                      projectID: inPatientServicesViewModel.isAdmitted
                          ? inPatientServicesViewModel.getAdmissionInfoResponseModel!.projectID!
                          : inPatientServicesViewModel.getAdmissionRequestInfoResponseModel!.projectId!,
                      admissionNo: inPatientServicesViewModel.isAdmitted ? inPatientServicesViewModel.getAdmissionInfoResponseModel!.admissionNo! : 0,
                      admissionReqNo: inPatientServicesViewModel.isAdmitted
                          ? inPatientServicesViewModel.getAdmissionInfoResponseModel!.admissionRequestNo!
                          : inPatientServicesViewModel.getAdmissionRequestInfoResponseModel!.admissionRequestNo!,
                      onSuccess: (val) {
                        LoaderBottomSheet.hideLoader();
                        Navigator.of(context).push(
                          CustomPageRoute(
                            page: InpatientAdvancePaymentPage(),
                          ),
                        );
                      },
                      onError: (err) {
                        LoaderBottomSheet.hideLoader();
                        showCommonBottomSheetWithoutHeight(
                          context,
                          child: Utils.getErrorWidget(
                            loadingText: err.toString(),
                          ),
                          callBackFunc: () {},
                          isFullScreen: false,
                          isCloseButtonVisible: true,
                        );
                      });
                },
              ),
              // SizedBox(height: 16.h),
              // buildInpatientServicesCard(
              //   iconBgColor: AppColors.primaryRedColor,
              //   icon: AppAssets.bloodSugarOnlyIcon,
              //   title: LocaleKeys.admissionNoticeTitle.tr(context: context),
              //   description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
              //   onTap: () {},
              // ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.prescriptions.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {
                  inPatientServicesViewModel.isAdmitted
                      ? inPatientServicesViewModel.checkDischargeMedications(
                          getAdmissionInfoResponseModel: inPatientServicesViewModel.getAdmissionInfoResponseModel!,
                          onError: (err) {
                            showCommonBottomSheetWithoutHeight(
                              context,
                              child: Utils.getErrorWidget(
                                loadingText: err.toString(),
                              ),
                              callBackFunc: () {},
                              isFullScreen: false,
                              isCloseButtonVisible: true,
                            );
                          })
                      : showCommonBottomSheetWithoutHeight(
                          context,
                          child: Utils.getErrorWidget(
                            loadingText: LocaleKeys.onlyAdmitted.tr(context: context),
                          ),
                          callBackFunc: () {},
                          isFullScreen: false,
                          isCloseButtonVisible: true,
                        );
                },
              ),
              SizedBox(height: 16.h),
              buildInpatientServicesCard(
                iconBgColor: AppColors.primaryRedColor,
                icon: AppAssets.bloodSugarOnlyIcon,
                title: LocaleKeys.patientRelationOffice.tr(context: context),
                description: LocaleKeys.trackYourGlucoseLevels.tr(context: context),
                onTap: () {
                  if (DateTime.now().hour >= 7 && DateTime.now().hour <= 22) {
                    showCommonBottomSheetWithoutHeight(context,
                        child: PatientRelationOfficeWidget(
                          inPatientServicesViewModel: inPatientServicesViewModel,
                        ),
                        callBackFunc: () {},
                        isFullScreen: false,
                        isCloseButtonVisible: true,
                        title: LocaleKeys.patientRelationOffice.tr(context: context));
                  } else {
                    showCommonBottomSheetWithoutHeight(
                      context,
                      child: Utils.getWarningWidget(
                          loadingText: LocaleKeys.proErrorMessage.tr(),
                          isShowActionButtons: true,
                          confirmText: LocaleKeys.callNow.tr(context: context),
                          onCancelTap: () {
                            Navigator.of(context).pop();
                          },
                          onConfirmTap: () async {
                            launchUrl(Uri.parse("tel://" + "011 525 9553"));
                          }),
                      callBackFunc: () {},
                      isFullScreen: false,
                      isCloseButtonVisible: true,
                    );
                  }
                },
              ),
              SizedBox(height: 16.h),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildInpatientServicesCard({
    required String icon,
    required String title,
    required String description,
    required Color iconBgColor,
    required VoidCallback onTap,
  }) {
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 20.r),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: iconBgColor, borderRadius: 10.r),
            height: 40.w,
            width: 40.w,
            child: Utils.buildSvgWithAssets(
              icon: icon,
              fit: BoxFit.none,
              height: 22.w,
              width: 22.w,
            ),
          ),
          SizedBox(width: 12.w),
          Flexible(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                title.toText16(isBold: true),
                description.toText12(
                  isBold: true,
                  color: Color(0xFF8F9AA3),
                ),
              ],
            ),
          ),
          SizedBox(width: 12.w),
          Transform.flip(
            flipX: getIt.get<AppState>().isArabic(),
            child: Utils.buildSvgWithAssets(
              icon: AppAssets.arrowRight,
              width: 24.w,
              height: 24.h,
              fit: BoxFit.contain,
              iconColor: AppColors.textColor,
            ),
          ),
        ],
      ).paddingAll(16.w),
    ).onPress(onTap);
  }
}
