import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/select_clinic_page.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/health_calculator_view_model.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/bf.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/blood_cholesterol.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/blood_sugar.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/bmi.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/bmr.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/calories.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/crabs.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/dduedate.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/ibw.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/ovulation.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/widgets/triglycerides.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

class HealthCalculatorDetailedPage extends StatefulWidget {
  final HealthCalculatorsTypeEnum calculatorType;
  final int? clinicID;
  final int? calculationID;

  const HealthCalculatorDetailedPage({super.key, required this.calculatorType, this.clinicID, this.calculationID});

  @override
  State<HealthCalculatorDetailedPage> createState() => _HealthCalculatorDetailedPageState();
}

class _HealthCalculatorDetailedPageState extends State<HealthCalculatorDetailedPage> {
  dynamic calculatedResult;

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => HealthCalcualtorViewModel(),
      child: Consumer<HealthCalcualtorViewModel>(builder: (context, provider, _) {
        return CollapsingListView(
          title: widget.calculatorType.displayName,
          bottomChild: widget.calculatorType == HealthCalculatorsTypeEnum.bloodSugar ||
                  widget.calculatorType == HealthCalculatorsTypeEnum.bloodCholesterol ||
                  widget.calculatorType == HealthCalculatorsTypeEnum.triglycerides
              ? SizedBox()
              : Container(
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.whiteColor, customBorder: BorderRadius.only(topLeft: Radius.circular(24.r), topRight: Radius.circular(24.r))),
                  padding: EdgeInsets.symmetric(vertical: 20.h, horizontal: 20.h),
                  child: CustomButton(
                      text: widget.calculatorType == HealthCalculatorsTypeEnum.bloodSugar ||
                              widget.calculatorType == HealthCalculatorsTypeEnum.bloodCholesterol ||
                              widget.calculatorType == HealthCalculatorsTypeEnum.triglycerides
                          ? LocaleKeys.convert.tr(context: context)
                          : LocaleKeys.calculate.tr(context: context),
                      onPressed: () {
                        if (calculatedResult == null) return;
                        Navigator.of(context).push(CustomPageRoute(
                          page: SelectClinicPage(calculatorType: widget.calculatorType, calculatedResult: calculatedResult),
                        ));
                      },
                      icon: null,
                      fontSize: 16.f,
                      backgroundColor: calculatedResult == null ? AppColors.bgRedLightColor : AppColors.primaryRedColor,
                      borderColor: calculatedResult == null ? AppColors.bgRedLightColor : AppColors.primaryRedColor,
                      borderRadius: 12.r,
                      isBold: true)),
          child: getCalculatorWidget(
            type: widget.calculatorType,
            onCalculate: (result) {
              // result may be directly provided by widget, but prefer provider maps if available

              switch (widget.calculatorType) {
                case HealthCalculatorsTypeEnum.bmi:
                  calculatedResult = provider.bmiResultMap != null
                      ? {...?provider.bmiResultMap, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.calories:
                case HealthCalculatorsTypeEnum.bmr:
                  calculatedResult = provider.caloriesResultMap != null
                      ? {...?provider.caloriesResultMap, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.idealBodyWeight:
                  calculatedResult = provider.ibwResultMap != null
                      ? {...?provider.ibwResultMap, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.bodyFat:
                  calculatedResult = provider.bodyFatResultMap != null
                      ? {...?provider.bodyFatResultMap, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.crabsProteinFat:
                  calculatedResult = provider.macrosResultMap != null
                      ? {...?provider.macrosResultMap, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.ovulation:
                  calculatedResult = (provider.ovulationResult is Map)
                      ? {...provider.ovulationResult as Map, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': provider.ovulationResult ?? result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.deliveryDueDate:
                  calculatedResult = (provider.deliveryResult is Map)
                      ? {...provider.deliveryResult as Map, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': provider.deliveryResult ?? result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.bloodSugar:
                  calculatedResult = (provider.bloodSugarResult is Map)
                      ? {...provider.bloodSugarResult as Map, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': provider.bloodSugarResult ?? result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.bloodCholesterol:
                  calculatedResult = (provider.bloodCholesterolResult is Map)
                      ? {...provider.bloodCholesterolResult as Map, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': provider.bloodCholesterolResult ?? result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
                case HealthCalculatorsTypeEnum.triglycerides:
                  calculatedResult = (provider.triglyceridesResult is Map)
                      ? {...provider.triglyceridesResult as Map, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID}
                      : {'result': provider.triglyceridesResult ?? result, 'clinicId': widget.clinicID, 'calculationID': widget.calculationID};
                  break;
              }
            },
          ).paddingSymmetrical(20.w, 24.h),
        );
      }),
    );
  }

  Widget getCalculatorWidget({required HealthCalculatorsTypeEnum type, required Function(dynamic result) onCalculate}) {
    switch (widget.calculatorType) {
      case HealthCalculatorsTypeEnum.bmi:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: BMIWidget(onChange: onCalculate),
        );

      case HealthCalculatorsTypeEnum.calories:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: CaloriesWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.bmr:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: BMRWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.idealBodyWeight:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: IdealBodyWeightWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.bodyFat:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: BodyFatWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.crabsProteinFat:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: CrabsWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.ovulation:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: OvulationWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.deliveryDueDate:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: DeliveryDueDWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.bloodSugar:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: BloodSugarWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.bloodCholesterol:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: BloodCholesterolWidget(onChange: onCalculate),
        );
      case HealthCalculatorsTypeEnum.triglycerides:
        return Container(
          decoration:
              RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, customBorder: BorderRadius.all(Radius.circular(24.r))),
          child: TriglyceridesWidget(onChange: onCalculate),
        );
    }
  }
}
