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/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/route_extensions.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/family_file_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/my_family/my_family.dart';
import 'package:hmg_patient_app_new/presentation/my_family/widget/family_cards.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/bottomsheet/exception_bottom_sheet.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/family_files/family_file_add_widget.dart';

import '../widgets/medication_reminder/reminder_timer_dialog.dart';

abstract class DialogService {
  Future<void> showErrorBottomSheet({String title = "", required String message, Function()? onOkPressed, Function()? onCancelPressed});

  Future<void> showExceptionBottomSheet({required String message, required Function() onOkPressed, Function()? onCancelPressed});

  Future<void> showCommonBottomSheetWithoutH(
      {String? label, required String message, String? okLabel, String? cancelLabel, bool isConfirmButton = false, required Function() onOkPressed, Function()? onCancelPressed});

  Future<void> showSuccessBottomSheetWithoutH({String? label, required String message, required Function() onOkPressed, Function()? onCancelPressed});

  Future<void> showFamilyBottomSheetWithoutH(
      {String? label,
      required String message,
      required Function(FamilyFileResponseModelLists response) onSwitchPress,
      required List<FamilyFileResponseModelLists> profiles,
      bool isShowManageButton = false,
      bool isForWalletRecharge = false});

  Future<void> showFamilyBottomSheetWithoutHWithChild({String? label, required String message, Widget? child, required Function() onOkPressed, Function()? onCancelPressed});

  Future<void> showPhoneNumberPickerSheet({String? label, String? message, required Function() onSMSPress, required Function() onWhatsappPress});

  Future<void> showAddFamilyFileSheet({String? label, String? message, required Function() onVerificationPress});

  Future<void> showReminderBottomSheetWithoutHWithChild({String? label, required String message, Widget? child, required Function() onOkPressed, Function()? onCancelPressed});
// TODO : Need to be Fixed showPhoneNumberPickerSheet ( From Login ADn Signup Bottom Sheet Move Here
}

class DialogServiceImp implements DialogService {
  final NavigationService navigationService;

  bool _isErrorSheetShowing = false;

  DialogServiceImp({required this.navigationService});

  @override
  Future<void> showErrorBottomSheet({String title = "", required String message, Function()? onOkPressed, Function()? onCancelPressed}) async {
    if (_isErrorSheetShowing) return;
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;

    _isErrorSheetShowing = true;
    await showModalBottomSheet(
      context: context,
      isScrollControlled: false,
      constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      builder: (_) => ExceptionBottomSheet(
        // title: title,
        message: message,
        showCancel: onCancelPressed != null ? true : false,
        onOkPressed: () {
          if (onOkPressed != null) {
            onOkPressed();
          } else {
            Navigator.pop(context);
          }
        },
        onCancelPressed: () {
          if (onCancelPressed != null) {
            onCancelPressed();
          } else {
            Navigator.pop(context);
          }
        },
      ),
    );
    _isErrorSheetShowing = false;
  }

  @override
  Future<void> showExceptionBottomSheet({required String message, required Function() onOkPressed, Function()? onCancelPressed}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;

    await showModalBottomSheet(
      context: context,
      constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width),
      isScrollControlled: false,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      builder: (_) => StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return ExceptionBottomSheet(
            message: message,
            showCancel: onCancelPressed != null ? true : false,
            onOkPressed: onOkPressed,
            onCancelPressed: () {
              if (onCancelPressed != null) {
                Navigator.of(context).pop();
              }
            },
          );
        },
      ),
    );
  }

  @override
  Future<void> showCommonBottomSheetWithoutH(
      {String? label, required String message, String? okLabel, String? cancelLabel, bool isConfirmButton = false, required Function() onOkPressed, Function()? onCancelPressed}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(
      context,
      title: label ?? "",
      child: exceptionBottomSheetWidget(
          context: context, message: message, okLabel: okLabel, cancelLabel: cancelLabel, onOkPressed: onOkPressed, onCancelPressed: onCancelPressed, isConfirmButton: isConfirmButton),
      callBackFunc: () {},
    );
  }

  @override
  Future<void> showSuccessBottomSheetWithoutH({String? label, required String message, required Function() onOkPressed, Function()? onCancelPressed}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(
      context,
      title: label ?? "",
      child: Utils.getSuccessWidget(loadingText: message.needTranslation),
      callBackFunc: () {},
    );
  }

  @override
  Future<void> showFamilyBottomSheetWithoutH(
      {String? label,
      required String message,
      required Function(FamilyFileResponseModelLists response) onSwitchPress,
      required List<FamilyFileResponseModelLists> profiles,
      bool isShowManageButton = false,
      bool isForWalletRecharge = false}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(context,
        title: label ?? "",
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            if (message != null) (message).toText16(isBold: false, color: AppColors.textColor),
            // SizedBox(height: 24.h),
            FamilyCards(
              profiles: profiles,
              onSelect: (FamilyFileResponseModelLists profile) {
                onSwitchPress(profile);
              },
              onRemove: (FamilyFileResponseModelLists profile) {},
              isShowDetails: false,
              isForWalletRecharge: isForWalletRecharge,
            ),
            SizedBox(height: isShowManageButton ? 15.h : 24.h),
            if (isShowManageButton)
              CustomButton(
                  text: LocaleKeys.manageFiles.tr(context: context),
                  backgroundColor: AppColors.secondaryLightRedColor,
                  borderColor: AppColors.secondaryLightRedColor,
                  textColor: AppColors.primaryRedBorderColor,
                  height: 48.h,
                  onPressed: () {
                    navigationService.pop();
                    navigationService.pushPage(page: FamilyMedicalScreen());
                  })
          ],
        ),
        useSafeArea: true,
        callBackFunc: () {});
  }

  @override
  Future<void> showFamilyBottomSheetWithoutHWithChild({String? label, required String message, Widget? child, required Function() onOkPressed, Function()? onCancelPressed}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(
      context,
      title: label ?? "",
      child: child ?? SizedBox(),
      callBackFunc: () {},
    );
  }

  @override
  Future<void> showReminderBottomSheetWithoutHWithChild({String? label, required String message, Widget? child, required Function() onOkPressed, Function()? onCancelPressed}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(
      context,
      title: label ?? "",
      child: child ?? SizedBox(),
      callBackFunc: () {},
    );
  }

  @override
  Future<void> showPhoneNumberPickerSheet({String? label, String? message, required Function() onSMSPress, required Function() onWhatsappPress}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(context,
        title: label ?? "", child: showPhoneNumberPickerWidget(context: context, message: message, onSMSPress: onSMSPress, onWhatsappPress: onWhatsappPress), callBackFunc: () {});
  }

  @override
  Future<void> showAddFamilyFileSheet({String? label, String? message, required Function() onVerificationPress}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;
    showCommonBottomSheetWithoutHeight(context,
        title: label ?? "",
        child: FamilyFileAddWidget(() {
          onVerificationPress();
        }, message ?? ""),
        callBackFunc: () {});
  }
}

Widget exceptionBottomSheetWidget(
    {required BuildContext context, required String message, String? okLabel, String? cancelLabel, bool isConfirmButton = false, required Function() onOkPressed, Function()? onCancelPressed}) {
  return Column(
    children: [
      (message).toText16(isBold: false, color: AppColors.textColor),
      SizedBox(height: 10.h),
      SizedBox(height: 24.h),
      if (onCancelPressed != null)
        Row(
          children: [
            Expanded(
              child: CustomButton(
                text: cancelLabel ?? LocaleKeys.cancel.tr(context: context),
                onPressed: () {
                  Navigator.of(context).pop();
                },
                backgroundColor: AppColors.secondaryLightRedColor,
                borderColor: AppColors.secondaryLightRedColor,
                textColor: AppColors.primaryRedColor,
                icon: AppAssets.cancel,
                iconColor: AppColors.primaryRedColor,
              ),
            ),
            SizedBox(width: 10.h),
            Expanded(
              child: CustomButton(
                text: okLabel ?? LocaleKeys.confirm.tr(context: context),
                onPressed: onOkPressed,
                backgroundColor: AppColors.bgGreenColor,
                borderColor: AppColors.successColor,
                textColor: Colors.white,
                icon: AppAssets.confirm,
              ),
            ),
          ],
        ),
      if (onCancelPressed == null)
        Padding(
          padding: EdgeInsets.only(bottom: 10.h),
          child: CustomButton(
            text: cancelLabel ?? LocaleKeys.cancel.tr(context: context),
            onPressed: (onCancelPressed == null)
                ? () {
                    context.pop();
                  }
                : onCancelPressed,
            // backgroundColor: AppColors.primaryRedColor,
            backgroundColor: isConfirmButton ? AppColors.successColor : AppColors.primaryRedColor,
            borderColor: isConfirmButton ? AppColors.successColor : AppColors.primaryRedColor,
            textColor: Colors.white,
            icon: AppAssets.confirm,
          ),
        ),
    ],
  );
}

Widget showPhoneNumberPickerWidget({required BuildContext context, String? message, required Function() onSMSPress, required Function() onWhatsappPress}) {
  return StatefulBuilder(builder: (BuildContext context, StateSetter setModalState) {
    return Column(
      children: [
        (message ?? "").toText16(isBold: false, color: AppColors.textColor),
        SizedBox(height: 10.h),
        Padding(
          padding: EdgeInsets.only(bottom: 10.h),
          child: CustomButton(
            text: LocaleKeys.sendOTPSMS.tr(context: context),
            onPressed: onSMSPress,
            backgroundColor: AppColors.primaryRedColor,
            borderColor: AppColors.primaryRedBorderColor,
            textColor: Colors.white,
            icon: AppAssets.message,
          ),
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 8.h),
              child: LocaleKeys.oR.tr(context: context).toText16(color: AppColors.textColor),
            ),
          ],
        ),
        Padding(
          padding: EdgeInsets.only(bottom: 10.h, top: 10.h),
          child: CustomButton(
            text: LocaleKeys.sendOTPWHATSAPP.tr(context: context),
            onPressed: onWhatsappPress,
            backgroundColor: AppColors.whiteColor,
            borderColor: AppColors.borderOnlyColor,
            textColor: AppColors.textColor,
            icon: AppAssets.whatsapp,
            iconColor: null,
            applyThemeColor: false,
          ),
        ),
      ],
    );
    //return Padding(
    //       padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
    //       child: SingleChildScrollView(
    //         child: GenericBottomSheet(
    //           countryCode: authViewModel.selectedCountrySignup.countryCode,
    //           initialPhoneNumber: "",
    //           textController: authViewModel.phoneNumberController,
    //           isEnableCountryDropdown: true,
    //           onCountryChange: authViewModel.onCountryChange,
    //           onChange: authViewModel.onPhoneNumberChange,
    //           buttons: [
    //
    //           ],
    //         ),
    //       ),
    //     );
  });
}

// Widget familyMemberAddWidget() {
//   AuthenticationViewModel authVm = getIt.get<AuthenticationViewModel>();
//   return showCommonBottomSheetWithoutHeight(context,
//       title: "Add Family Member".needTranslation,
//       useSafeArea: true,
//       child:
//       callBackFunc: () {});
// }
