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/features/profile_settings/profile_settings_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.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/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
import 'package:provider/provider.dart';

class UpdateEmailDialog extends StatefulWidget {
  UpdateEmailDialog({super.key});

  @override
  State<UpdateEmailDialog> createState() => _UpdateEmailDialogState();
}

class _UpdateEmailDialogState extends State<UpdateEmailDialog> {
  late FocusNode _textFieldFocusNode;
  late TextEditingController? textController;
  ProfileSettingsViewModel? profileSettingsViewModel;

  @override
  void initState() {
    _textFieldFocusNode = FocusNode();
    textController = TextEditingController();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      setState(() {
        textController!.text = profileSettingsViewModel!.getPatientInfoForUpdate.emailAddress ?? "";
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _textFieldFocusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    profileSettingsViewModel = Provider.of<ProfileSettingsViewModel>(context);
    return GestureDetector(
      onTap: () {
        _textFieldFocusNode.unfocus();
        FocusScope.of(context).unfocus();
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          LocaleKeys.updateEmailAddressTitle.tr().toText16(textAlign: TextAlign.start, isBold: true),
          SizedBox(height: 12.h),
          TextInputWidget(
            labelText: LocaleKeys.email.tr(),
            hintText: "demo@gmail.com",
            controller: textController,
            focusNode: _textFieldFocusNode,
            autoFocus: true,
            padding: EdgeInsets.all(8.h),
            keyboardType: TextInputType.emailAddress,
            isEnable: true,
            isReadOnly: false,
            prefix: null,
            isBorderAllowed: false,
            isAllowLeadingIcon: true,
            fontSize: 14.f,
            isCountryDropDown: false,
            leadingIcon: AppAssets.email,
            fontFamily: "Poppins",
          ),
          SizedBox(height: 12.h),
          CustomButton(
            text: LocaleKeys.submit.tr(context: context),
            onPressed: () {
              LoaderBottomSheet.showLoader(loadingText: LocaleKeys.updatingEmailAddress.tr(context: context));
              profileSettingsViewModel!.updatePatientInfo(
                patientInfo: {
                  "PreferredLanguage": profileSettingsViewModel!.getPatientInfoForUpdate.preferredLanguage,
                  "EmailAddress": textController!.text,
                  "EmergencyContactName": profileSettingsViewModel!.getPatientInfoForUpdate.emergencyContactName,
                  "EmergencyContactNo": profileSettingsViewModel!.getPatientInfoForUpdate.emergencyContactNo,
                  "IsEmailAlertRequired": true,
                  "IsSMSAlertRequired": true,
                },
                onSuccess: (response) {
                  LoaderBottomSheet.hideLoader();
                  showCommonBottomSheetWithoutHeight(context, title: LocaleKeys.success.tr(context: context), child: Utils.getSuccessWidget(loadingText: LocaleKeys.success.tr()),
                      callBackFunc: () async {
                    Navigator.of(context).pop();
                    profileSettingsViewModel!.getProfileSettings();
                  }, isFullScreen: false, isAutoDismiss: true);
                },
                onError: (error) {
                  LoaderBottomSheet.hideLoader();
                  // Show error message
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text(error)),
                  );
                },
              );
            },
            backgroundColor: AppColors.primaryRedColor,
            borderColor: AppColors.primaryRedColor,
            textColor: const Color(0xFFffffff),
          ),
        ],
      ),
    );
  }
}
