import 'dart:developer';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.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/symptoms_checker/symptoms_checker_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/symptoms_checker/user_info_selection/widgets/weight_scale.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:provider/provider.dart';

/// Weight selection page content
class WeightSelectionPage extends StatelessWidget {
  final Function(double, bool) onWeightChanged;

  const WeightSelectionPage({super.key, required this.onWeightChanged});

  Widget _unitSelector(SymptomsCheckerViewModel viewModel) {
    bool isKg = viewModel.isWeightKg;
    return Container(
      height: 54.h,
      padding: EdgeInsets.all(4.h),
      decoration: BoxDecoration(
        color: AppColors.whiteColor,
        borderRadius: BorderRadius.circular(10.r),
      ),
      child: LayoutBuilder(
        builder: (context, constraints) {
          final tabWidth = (constraints.maxWidth - 8.w) / 2;
          return Stack(
            children: [
              // Animated sliding indicator
              AnimatedContainer(
                duration: const Duration(milliseconds: 250),
                curve: Curves.easeInOut,
                width: tabWidth,
                height: constraints.maxHeight,
                margin: EdgeInsets.only(left: isKg ? 0 : tabWidth + 8.w),
                decoration: BoxDecoration(
                  color: AppColors.bottomNAVBorder,
                  borderRadius: BorderRadius.circular(7.r),
                ),
              ),
              // Tab buttons
              Row(
                children: [
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        if (!isKg) {
                          // Convert from LBS to KG (1 lb = 0.453592 kg)
                          final convertedWeight = viewModel.selectedWeight! / 2.20462;
                          Future.microtask(() => onWeightChanged(convertedWeight, true));
                        }
                      },
                      child: Container(
                        alignment: Alignment.center,
                        color: Colors.white,
                        child: Text(
                          'KG',
                          style: TextStyle(
                            fontWeight: FontWeight.w700,
                            fontSize: 14.f,
                            color: isKg ? AppColors.primaryRedColor : AppColors.textColor.withValues(alpha: 0.6),
                              fontFamily: "Poppins"
                          ),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        if (isKg) {
                          // Convert from KG to LBS (1 kg = 2.20462 lbs)
                          final convertedWeight = viewModel.selectedWeight! * 2.20462;
                          Future.microtask(() => onWeightChanged(convertedWeight, false));
                        }
                      },
                      child: Container(
                        alignment: Alignment.center,
                        color: Colors.transparent,
                        child: Text(
                          'LBS',
                          style: TextStyle(
                            fontWeight: FontWeight.w700,
                            fontSize: 14.f,
                            color: !isKg ? AppColors.primaryRedColor : AppColors.textColor.withValues(alpha: 0.6),
                              fontFamily: "Poppins"
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<SymptomsCheckerViewModel>(builder: (context, viewModel, child) {
      bool isKg = viewModel.isWeightKg;
      // Define min/max values based on unit
      final minValue = isKg ? 10.0 : 22.0; // 10kg or 22lbs
      final maxValue = isKg ? 200.0 : 440.0; // 200kg or 440lbs

      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 24.h),
          Center(
            child: Text(
              LocaleKeys.weightSymptoms.tr(context: context),
              style: TextStyle(fontSize: 18.f, fontWeight: FontWeight.w600, color: AppColors.textColor),
            ),
          ),
          SizedBox(height: 24.h),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 24.w),
            child: _unitSelector(viewModel),
          ),
          SizedBox(height: 60.h),
          // Weight display centered
          Center(
            child: Row(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  viewModel.selectedWeight!.round().toString(),
                  style: TextStyle(fontSize: 100.f, color: AppColors.textColor, height: 1, fontFamily: "Poppins"),
                ),
                SizedBox(width: 8.w),
                Text(
                  isKg ? 'kg' : 'lbs',
                  style: TextStyle(fontWeight: FontWeight.w700, fontSize: 24.f, fontFamily: "Poppins"),
                ).paddingOnly(bottom: 10.h),
              ],
            ),
          ),
          SizedBox(height: 40.h),
          // Horizontal weight picker
          WeightScale(
            enableHaptic: true,
            enableSound: true,
            minValue: minValue,
            maxValue: maxValue,
            initialWeight: (viewModel.selectedWeight ?? 70).toDouble(),
            isKg: isKg,
            onWeightChanged: (newWeight) {
              log("weight: $newWeight");
              Future.microtask(() => onWeightChanged(newWeight, isKg));
            },
          ),
        ],
      );
    });
  }
}
