// import 'package:flutter/cupertino.dart';
// import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
//
// class SelectMedicalFile extends StatelessWidget {
//   const SelectMedicalFile({super.key});
//
//   @override
//   Widget build(BuildContext context) {
//     return Padding(
//       padding: EdgeInsets.all(24.h),
//       child: Column(
//         crossAxisAlignment: CrossAxisAlignment.start,
//         children: [
//           Container()
//         ],
//       ),
//     );
//   }
// }

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/habib_wallet/habib_wallet_view_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/medical_file_view_model.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/services/dialog_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/input_widget.dart';
import 'package:provider/provider.dart';

class MultiPageBottomSheet extends StatefulWidget {
  const MultiPageBottomSheet({Key? key}) : super(key: key);

  @override
  State<MultiPageBottomSheet> createState() => _MultiPageBottomSheetState();
}

class _MultiPageBottomSheetState extends State<MultiPageBottomSheet> {
  late AppState appState;
  static final DialogService _dialogService = getIt.get<DialogService>();

  TextEditingController fileNumberEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    return Consumer<HabibWalletViewModel>(builder: (context, habibWalletVM, child) {
      return Padding(
        padding: MediaQuery.of(context).viewInsets,
        child: habibWalletVM.isBottomSheetContentLoading ? Utils.getLoadingWidget() : getCurrentIndexWidget(habibWalletVM),
      );
    });
  }

  Widget getCurrentIndexWidget(HabibWalletViewModel habibWalletVM) {
    switch (habibWalletVM.currentIndex) {
      case 0:
        return getSelectMedicalFileContent(habibWalletVM);
      case 1:
        return getOtherAccountContent(habibWalletVM);
      case 2:
        return getOtherAccountContent(habibWalletVM);
      default:
        return getSelectMedicalFileContent(habibWalletVM);
    }
  }

  Widget getOtherAccountContent(HabibWalletViewModel habibWalletVM) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        LocaleKeys.pleaseEnterAFileNumber.tr(context: context).toText20(isBold: true),
        SizedBox(height: 12.h),
        TextInputWidget(
          fontFamily: "Poppins",
          labelText: LocaleKeys.fileNumber.tr(),
          hintText: "xxxxxxxxx",
          controller: fileNumberEditingController,
          isEnable: true,
          prefix: null,
          isAllowRadius: true,
          isBorderAllowed: false,
          isAllowLeadingIcon: true,
          autoFocus: false,
          padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 8.h),
          leadingIcon: AppAssets.requests,
        ).withVerticalPadding(8),
        SizedBox(height: 12.h),
        CustomButton(
          text: LocaleKeys.submit.tr(),
          onPressed: () async {
            await habibWalletVM.getPatientInfoByPatientID(
                patientID: fileNumberEditingController.text,
                onSuccess: (response) async {
                  debugPrint(response.data["GetPatientInfoByPatientIDList"][0]["FullName"]);
                  await _dialogService.showCommonBottomSheetWithoutH(
                      message: "A file was found with name: ${response.data["GetPatientInfoByPatientIDList"][0]["FullName"]}, Would you like to recharge wallet for this file number?",
                      label: LocaleKeys.notice.tr(),
                      onOkPressed: () {
                        habibWalletVM.setSelectedRechargeType(3);
                        habibWalletVM.setDepositorDetails(response.data["GetPatientInfoByPatientIDList"][0]["PatientID"].toString(), response.data["GetPatientInfoByPatientIDList"][0]["FullName"],
                            response.data["GetPatientInfoByPatientIDList"][0]["MobileNumber"]);
                        Navigator.of(context).pop();
                        Navigator.of(context).pop();
                      },
                      onCancelPressed: () {});
                },
                onError: (error) {});
          },
          backgroundColor: AppColors.primaryRedColor,
          borderColor: AppColors.primaryRedBorderColor,
          textColor: AppColors.whiteColor,
        ),
      ],
    );
  }

  Widget getSelectMedicalFileContent(HabibWalletViewModel habibWalletVM) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
            color: AppColors.whiteColor,
            borderRadius: 16.h,
            hasShadow: false,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LocaleKeys.myMedicalFile.tr(context: context).toText16(color: AppColors.textColor, isBold: true),
                  Row(
                    children: [
                      "${LocaleKeys.fileno.tr(context: context)}: ".toText12(color: AppColors.greyTextColor, isBold: true),
                      appState.getAuthenticatedUser()!.patientId.toString().toText12(color: AppColors.greyTextColor, isBold: true, isEnglishOnly: true),
                    ],
                  ),
                ],
              ),
              Transform.flip(flipX: appState.isArabic(), child: Utils.buildSvgWithAssets(icon: AppAssets.forward_chevron_icon, iconColor: AppColors.textColor, width: 15.h, height: 15.h)),
            ],
          ).paddingAll(16.h),
        ).onPress(() {
          habibWalletVM.setDepositorDetails(appState.getAuthenticatedUser()!.patientId!.toString(),
              "${appState.getAuthenticatedUser()!.firstName.toString()} ${appState.getAuthenticatedUser()!.lastName.toString()}", appState.getAuthenticatedUser()!.mobileNumber!.toString());
          habibWalletVM.setCurrentIndex(0);
          habibWalletVM.setSelectedRechargeType(1);
          Navigator.of(context).pop();
        }),
        SizedBox(height: 16.h),
        Container(
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
            color: AppColors.whiteColor,
            borderRadius: 16.h,
            hasShadow: false,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LocaleKeys.familyTitle.tr(context: context).toText16(color: AppColors.textColor, isBold: true),
                  LocaleKeys.selectMedicalFileFromFamily.tr(context: context).toText14(color: AppColors.greyTextColor, isBold: true),
                ],
              ),
              Transform.flip(flipX: appState.isArabic(), child: Utils.buildSvgWithAssets(icon: AppAssets.forward_chevron_icon, iconColor: AppColors.textColor, width: 15.h, height: 15.h)),
            ],
          ).paddingAll(16.h),
        ).onPress(() {
          DialogService dialogService = getIt.get<DialogService>();
          dialogService.showFamilyBottomSheetWithoutH(
              label: LocaleKeys.familyTitle.tr(context: context),
              message: "",
              isShowManageButton: false,
              isForWalletRecharge: true,
              onSwitchPress: (FamilyFileResponseModelLists profile) {
                habibWalletVM.setDepositorDetails(profile.responseId.toString(), profile.patientName.toString(), profile.mobileNumber.toString());
                habibWalletVM.setCurrentIndex(0);
                habibWalletVM.setSelectedRechargeType(2);
                Navigator.of(context).pop();
                Navigator.of(context).pop();

                // medicalFileViewModel.switchFamilyFiles(responseID: profile.responseId, patientID: profile.patientId, phoneNumber: profile.mobileNumber);
              },
              profiles: getIt.get<MedicalFileViewModel>().patientFamilyFiles);
        }),
        SizedBox(height: 16.h),
        Container(
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
            color: AppColors.whiteColor,
            borderRadius: 16.h,
            hasShadow: false,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LocaleKeys.otherAccount.tr(context: context).toText16(color: AppColors.textColor, isBold: true),
                  LocaleKeys.anyFileFromHMG.tr(context: context).toText14(color: AppColors.greyTextColor, isBold: true),
                ],
              ),
              Transform.flip(flipX: appState.isArabic(), child: Utils.buildSvgWithAssets(icon: AppAssets.forward_chevron_icon, iconColor: AppColors.textColor, width: 15.h, height: 15.h)),
            ],
          ).paddingAll(16.h),
        ).onPress(() {
          habibWalletVM.setCurrentIndex(2);
        }),
      ],
    );
  }
}
