import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.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/immediate_livecare/models/resp_models/get_livecare_immediate_clinics_response_model.dart';
import 'package:hmg_patient_app_new/features/immediate_livecare/immediate_livecare_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/livecare/immediate_livecare_payment_details.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/livecare/widgets/select_livecare_call_type.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.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';

class LiveCareClinicCard extends StatelessWidget {
  LiveCareClinicCard({super.key, required this.liveCareClinicListResponseModel, required this.isLoading, required this.immediateLiveCareViewModel});

  GetLiveCareClinicListResponseModel liveCareClinicListResponseModel;
  bool isLoading;
  ImmediateLiveCareViewModel immediateLiveCareViewModel;
  late AppState appState;

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    return Container(
      padding: EdgeInsets.all(16.h),
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 24.h,
        hasShadow: false,
      ),
      child: Column(
        children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Utils.buildSvgWithAssets(icon: AppAssets.generic_clinic_icon, width: 24.h, height: 24.h, fit: BoxFit.contain).toShimmer2(isShow: isLoading),
            AppCustomChipWidget(
              icon: AppAssets.livecare_online_icon,
              iconColor: liveCareClinicListResponseModel.isOnline == 1 ? AppColors.successColor : AppColors.primaryRedColor,
              richText: (liveCareClinicListResponseModel.isOnline == 1 ? LocaleKeys.online.tr(context: context) : LocaleKeys.offline.tr()).toText12(
                isBold: true,
                color: liveCareClinicListResponseModel.isOnline == 1 ? AppColors.successColor : AppColors.primaryRedColor,
              ),
              backgroundColor: AppColors.transparent,
              textColor: liveCareClinicListResponseModel.isOnline == 1 ? AppColors.successColor : AppColors.primaryRedColor,
            ),
          ]),
          SizedBox(height: 8.h),
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Expanded(
                child: (isLoading ? "Cardiology" : (appState.isArabic() ? liveCareClinicListResponseModel.serviceNameN : liveCareClinicListResponseModel.serviceName))!
                    .toText16(isBold: true)
                    .toShimmer2(isShow: isLoading)),
            // Transform.flip(
            //     flipX: appState.isArabic(),
            //     child: Utils.buildSvgWithAssets(icon: AppAssets.forward_arrow_icon, width: 40.h, height: 40.h, fit: BoxFit.contain, iconColor: AppColors.textColor).toShimmer2(isShow: isLoading)),
          ]),
          SizedBox(height: 8.h),
          Row(
            children: [
              Expanded(
                flex: 7,
                child: CustomButton(
                  text: LocaleKeys.bookNow.tr(),
                  onPressed: () async {
                    onImmediateLiveCareClinicSelected(liveCareClinicListResponseModel);
                  },
                  backgroundColor: AppColors.successLightBgColor,
                  borderColor: AppColors.successLightBgColor,
                  textColor: AppColors.successColor,
                  borderWidth: 1.h,
                  fontSize: (isFoldable || isTablet) ? 12.f : 14.f,
                  fontWeight: FontWeight.w600,
                  borderRadius: 12.r,
                  padding: EdgeInsets.symmetric(horizontal: 10.w),
                  height: 40.h,
                  icon: AppAssets.bookAppoBottom,
                  iconColor: liveCareClinicListResponseModel.isOnline == 0 ? AppColors.textColorLight : AppColors.successColor,
                  iconSize: 16.h,
                  isDisabled: liveCareClinicListResponseModel.isOnline == 0,
                ),
              ),
              SizedBox(width: 8.w),
              Expanded(
                flex: 1,
                child: Container(
                  height: (isFoldable || isTablet) ? 50.h : 40.h,
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                    // color: Color(0xFFEFEFF0),
                    color: AppColors.transparent,
                    borderRadius: 10.h,
                    side: BorderSide(
                      color: AppColors.textColor,
                      width: 1.2,
                    ),
                  ),
                  child: Transform.flip(
                    flipX: appState.isArabic(),
                    child: Utils.buildSvgWithAssets(
                      icon: AppAssets.calendar,
                      iconColor: AppColors.textColor,
                      width: 5.w,
                      height: 5.h,
                      fit: BoxFit.scaleDown,
                    ),
                  ),
                ).onPress(() {
                  onClinicScheduleTapped(liveCareClinicListResponseModel.serviceID!);
                }),
              ),
            ],
          ),
        ],
      ),
    );
  }

  onClinicScheduleTapped(int serviceID) async {
    LoaderBottomSheet.showLoader(loadingText: LocaleKeys.loadingText.tr());
    await immediateLiveCareViewModel.getLiveCareImmediateClinicSchedule(
        serviceID: serviceID,
        onSuccess: (val) {
          LoaderBottomSheet.hideLoader();
          showCommonBottomSheetWithoutHeight(GetIt.instance<NavigationService>().navigatorKey.currentContext!,
              child: ListView.separated(
                  shrinkWrap: true,
                  itemBuilder: (_, index) {
                    return DecoratedBox(
                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                        color: AppColors.whiteColor,
                        borderRadius: 20.h,
                        hasShadow: false,
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              spacing: 8.h,
                              children: [
                                checkInOptionCard(AppAssets.calendarGrey, immediateLiveCareViewModel.liveCareClinicScheduleList[index].dayOfWeekStr!,
                                    "${immediateLiveCareViewModel.liveCareClinicScheduleList[index].shiftTimings!.first.startTime} to ${immediateLiveCareViewModel.liveCareClinicScheduleList[index].shiftTimings!.first.endTime}"),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ).paddingSymmetrical(0.h, 0.h).onPress(() {});
                  },
                  separatorBuilder: (_, __) => SizedBox(height: 16.h),
                  itemCount: immediateLiveCareViewModel.liveCareClinicScheduleList.length),
              title: LocaleKeys.clinicSchedule.tr(),
              isCloseButtonVisible: true,
              useSafeArea: true,
              isFullScreen: true);
        });
  }

  onImmediateLiveCareClinicSelected(GetLiveCareClinicListResponseModel liveCareClinic) {
    //TODO: add implementation to show clinic schedule
    // if (liveCareClinic.isOnline == 1) {
    showCommonBottomSheetWithoutHeight(GetIt.instance<NavigationService>().navigatorKey.currentContext!, child: SelectLiveCareCallType(immediateLiveCareViewModel: immediateLiveCareViewModel),
        callBackFunc: () async {
      if (immediateLiveCareViewModel.liveCareSelectedCallType != 0) {
        immediateLiveCareViewModel.setImmediateLiveCareSelectedClinic(liveCareClinic);
        LoaderBottomSheet.showLoader(loadingText: LocaleKeys.fetchingFeesPleaseWait.tr());
        await immediateLiveCareViewModel.getLiveCareImmediateAppointmentFees(onSuccess: (val) {
          LoaderBottomSheet.hideLoader();
          Navigator.of(GetIt.instance<NavigationService>().navigatorKey.currentContext!).push(
            CustomPageRoute(
              page: ImmediateLiveCarePaymentDetails(),
            ),
          );
        }, onError: (err) {
          LoaderBottomSheet.hideLoader();
          showCommonBottomSheetWithoutHeight(
            GetIt.instance<NavigationService>().navigatorKey.currentContext!,
            child: Utils.getErrorWidget(loadingText: err),
            callBackFunc: () {},
            isFullScreen: false,
            isCloseButtonVisible: true,
          );
        });
      }
    }, title: LocaleKeys.selectLiveCareCallType.tr(), isCloseButtonVisible: true, isFullScreen: false);
  }

  Widget checkInOptionCard(String icon, String title, String subTitle) {
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.h,
        hasShadow: false,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    title.toText16(isBold: true, color: AppColors.textColor),
                    SizedBox(height: 8.h),
                    Row(
                      children: [
                        AppCustomChipWidget(
                          icon: AppAssets.appointment_time_icon,
                          iconColor: AppColors.textColor,
                          labelText: subTitle.split("to").first.trim(),
                          isEnglishOnly: true,
                        ),
                        SizedBox(width: 8.w),
                        Transform.flip(
                          flipX: appState.isArabic(),
                          child: Utils.buildSvgWithAssets(
                            icon: AppAssets.arrow_forward,
                            height: 24.h,
                            width: 24.h,
                            iconColor: AppColors.textColor,
                          ),
                        ),
                        SizedBox(width: 8.w),
                        AppCustomChipWidget(
                          icon: AppAssets.appointment_time_icon,
                          iconColor: AppColors.textColor,
                          labelText: subTitle.split("to")[1].trim(),
                          isEnglishOnly: true,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ).paddingAll(16.h),
    );
  }
}
