import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/int_extensions.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/emergency_services/emergency_services_view_model.dart';
import 'package:hmg_patient_app_new/features/habib_wallet/habib_wallet_view_model.dart';
import 'package:hmg_patient_app_new/features/immediate_livecare/immediate_livecare_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/todo_section/todo_section_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:provider/provider.dart';

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

  @override
  _AppLanguageChangeState createState() {
    return _AppLanguageChangeState();
  }
}

class _AppLanguageChangeState extends State<AppLanguageChange> {
  String? selectedValue;

  late MyAppointmentsViewModel myAppointmentsViewModel;
  late ImmediateLiveCareViewModel immediateLiveCareViewModel;
  late EmergencyServicesViewModel emergencyServicesViewModel;
  late TodoSectionViewModel todoSectionViewModel;
  late HabibWalletViewModel habibWalletVM;

  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    selectedValue ??= context.locale.languageCode;

    myAppointmentsViewModel = Provider.of<MyAppointmentsViewModel>(context, listen: false);
    immediateLiveCareViewModel = Provider.of<ImmediateLiveCareViewModel>(context, listen: false);
    emergencyServicesViewModel = Provider.of<EmergencyServicesViewModel>(context, listen: false);
    todoSectionViewModel = Provider.of<TodoSectionViewModel>(context, listen: false);
    habibWalletVM = context.read<HabibWalletViewModel>();

    return Column(
      spacing: 24.h,
      children: [
        Container(
          padding: EdgeInsets.only(top: 4, bottom: 4),
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
          child: Column(
            children: [
              languageItem("العربية", "ar"),
              1.divider,
              languageItem("English", "en"),
            ],
          ),
        ),
        CustomButton(
            text: LocaleKeys.save.tr(context: context),
            onPressed: () {
              context.setLocale(selectedValue == 'en' ? Locale('en', 'US') : Locale('ar', 'SA')).then((val) {
                //Reload My Appointments Data
                myAppointmentsViewModel.setIsAppointmentDataToBeLoaded(true);
                myAppointmentsViewModel.initAppointmentsViewModel();
                myAppointmentsViewModel.getPatientAppointments(true, false);

                habibWalletVM.setIsWalletAmountToBeLoaded(true);
                habibWalletVM.initHabibWalletProvider();
                habibWalletVM.getPatientBalanceAmount();
                habibWalletVM.getLakumAccountInformation();

                //Reload Immediate LiveCare Data
                immediateLiveCareViewModel.initImmediateLiveCare();
                immediateLiveCareViewModel.getPatientLiveCareHistory();

                //Reload Emergency Services Data
                emergencyServicesViewModel.checkPatientERAdvanceBalance();

                todoSectionViewModel.setIsAncillaryOrdersNeedReloading(true);
                todoSectionViewModel.initializeTodoSectionViewModel();

                Navigator.pop(context);
              });
            }),
      ],
    );
  }

  Widget languageItem(String title, String _value) {
    return SizedBox(
      height: 72,
      child: Row(
        spacing: 8.h,
        children: [
          Radio(
            value: _value,
            groupValue: selectedValue,
            activeColor: AppColors.errorColor,
            onChanged: (value) {
              setState(() {
                selectedValue = _value;
              });
            },
            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          title.toText16(isBold: true, textOverflow: TextOverflow.ellipsis, maxlines: 1, fontFamily: _value == "ar" ? 'CairoArabic' : 'Poppins').expanded,
        ],
      ).paddingOnly(left: 16, right: 16).onPress(() {
        setState(() {
          selectedValue = _value;
        });
      }),
    );
  }
}
