import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:provider/provider.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/dependencies.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/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/presentation/health_calculators_and_converts/health_calculator_detailed_page.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/routes/custom_page_route.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/health_calculator_view_model.dart';

class CrabsWidget extends StatefulWidget {
  Function(dynamic result)? onChange;

  CrabsWidget({super.key, required this.onChange});

  @override
  _CrabsWidgetState createState() => _CrabsWidgetState();
}

class _CrabsWidgetState extends State<CrabsWidget> {
  final TextEditingController _caloriesController = TextEditingController();
  String selectedDietType = 'Very Low Crabs';
  Function(String)? onUnitSelected;

  @override
  void initState() {
    super.initState();
    _caloriesController.addListener(_onInputChanged);
  }

  @override
  void dispose() {
    _caloriesController.removeListener(_onInputChanged);
    _caloriesController.dispose();
    super.dispose();
  }

  void _onInputChanged() {
    final provider = Provider.of<HealthCalcualtorViewModel>(context, listen: false);
    provider.calculateMacros(caloriesText: _caloriesController.text, selectedDietType: selectedDietType);
    if (widget.onChange != null) widget.onChange!(provider.macrosResultMap);
  }

  @override
  Widget build(BuildContext context) {
    DialogService dialogService = getIt<DialogService>();

    return Consumer<HealthCalcualtorViewModel>(builder: (context, provider, _) {
      return Container(
        margin: EdgeInsets.zero,
        padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 4.h, bottom: 4.h),
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.r, hasShadow: true),
        child: Column(
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Utils.buildSvgWithAssets(icon: AppAssets.age, width: 40.w, height: 40.w, applyThemeColor: false),
                SizedBox(width: 12.w),
                Expanded(
                  flex: 3,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      LocaleKeys.caloriesPerDay.tr(context: context).toText12(isBold: true, color: AppColors.inputLabelTextColor),
                      Container(
                        height: 20.w,
                        alignment: Alignment.centerLeft,
                        child: TextField(
                          controller: _caloriesController,
                          keyboardType: TextInputType.number,
                          maxLines: 1,
                          cursorHeight: 14,
                          textAlignVertical: TextAlignVertical.center,
                          decoration: const InputDecoration(
                            border: InputBorder.none,
                            contentPadding: EdgeInsets.zero,
                            isCollapsed: true,
                            hintText: '2000',
                            hintStyle: TextStyle(color: Colors.grey),
                          ),
                          style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: Colors.black87, height: 1.0),
                        ),
                      )
                    ],
                  ),
                ),
                Utils.buildSvgWithAssets(icon: AppAssets.ask_doctor_icon, applyThemeColor: false).onPress(() {
                  Navigator.pop(context);
                  Navigator.of(context).push(CustomPageRoute(page: HealthCalculatorDetailedPage(calculatorType: HealthCalculatorsTypeEnum.calories)));
                }),
                SizedBox(width: 4.w),
              ],
            ).paddingSymmetrical(0.w, 16.w),
            Divider(height: 1, color: Color(0xFFEEEEEE)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Utils.buildSvgWithAssets(icon: AppAssets.activity, width: 40.w, height: 40.w, applyThemeColor: false),
                SizedBox(width: 12.w),
                Expanded(
                  child: Row(
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          LocaleKeys.dietType.tr(context: context).toText12(isBold: true, color: AppColors.inputLabelTextColor),
                          selectedDietType.toText12(isBold: true, color: AppColors.textColor),
                        ],
                      ),
                    ],
                  ),
                ),
                Utils.buildSvgWithAssets(icon: AppAssets.arrow_down),
                SizedBox(width: 4.w),
              ],
            ).paddingSymmetrical(0.w, 16.w).onPress(() {
              List<String> _activity = ["Very Low Crabs", "Low Crabs", "Moderate Crabs", "USDA Guidelines ", "Zone Diet"];
              dialogService.showFamilyBottomSheetWithoutHWithChild(
                label: LocaleKeys.selectDietType.tr(context: context),
                message: "",
                child: Container(
                  padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 4.h, bottom: 4.h),
                  decoration: BoxDecoration(color: AppColors.whiteColor, borderRadius: BorderRadius.circular(20.r)),
                  child: ListView.separated(
                    shrinkWrap: true,
                    itemCount: _activity.length,
                    itemBuilder: (context, index) {
                      final unit = _activity[index];
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          SizedBox(
                            height: 70,
                            child: Row(
                              spacing: 8.h,
                              children: [
                                Radio<String>(
                                  value: unit,
                                  groupValue: selectedDietType,
                                  activeColor: AppColors.errorColor,
                                  onChanged: (String? value) {
                                    if (value == null) return;
                                    setState(() {
                                      selectedDietType = value;
                                      _onInputChanged();
                                    });
                                    Navigator.pop(context);
                                  },
                                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                ),
                                unit.toCamelCase.toText16(isBold: true, textOverflow: TextOverflow.ellipsis, maxlines: 1).expanded,
                              ],
                            ).onPress(() {
                              setState(() {
                                selectedDietType = unit;
                                _onInputChanged();
                              });
                              Navigator.pop(context);
                            }),
                          ),
                        ],
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Divider(height: 1, color: Color(0xFFEEEEEE));
                    },
                  ),
                ),
                onOkPressed: () {},
              );
            }),
            Divider(height: 1, color: Color(0xFFEEEEEE)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Utils.buildSvgWithAssets(icon: AppAssets.globe, width: 18.w, height: 18.w),
                SizedBox(width: 12.w),
                Expanded(
                  child: LocaleKeys.carbsAlert.tr()
                      .toText12(isBold: true, color: AppColors.inputLabelTextColor),
                ),
              ],
            ).paddingSymmetrical(0.w, 16.w),
          ],
        ),
      );
    });
  }
}
