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_export.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/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/theme/colors.dart';
import 'package:provider/provider.dart';

/// Gender selection page content
class GenderSelectionPage extends StatelessWidget {
  final String? selectedGender;
  final Function(String) onGenderSelected;

  const GenderSelectionPage({
    super.key,
    required this.selectedGender,
    required this.onGenderSelected,
  });

  // Internal gender keys (constant, not localized)
  static const String maleKey = "male";
  static const String femaleKey = "female";

  _buildGenderOption(String iconPng, String label, bool isSelected) {
    return Container(
      height: 160.h,
      width: 160.w,
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
          color: AppColors.whiteColor, borderRadius: 24.r, side: isSelected ? BorderSide(color: AppColors.primaryRedColor, width: 2.5) : null),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Utils.buildImgWithAssets(icon: iconPng, height: 80.h, width: 80.h, fit: BoxFit.contain),
          SizedBox(height: 8.h),
          label.toText16(
            weight: FontWeight.w600,
          )
        ],
      ),
    );
  }

  /// Check if the selected gender matches the given key
  /// Handles both internal keys and localized strings
  bool _isGenderSelected(String genderKey, String? currentSelection, BuildContext context) {
    if (currentSelection == null) return false;

    final lowerSelection = currentSelection.toLowerCase();

    if (genderKey == maleKey) {
      // Check against internal key, English, and Arabic
      return lowerSelection == maleKey ||
          lowerSelection == "male" ||
          currentSelection == LocaleKeys.malE.tr(context: context) ||
          currentSelection == "ذكر";
    } else {
      // Check against internal key, English, and Arabic
      return lowerSelection == femaleKey ||
          lowerSelection == "female" ||
          currentSelection == LocaleKeys.femaleGender.tr(context: context) ||
          currentSelection == "أنثى";
    }
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      padding: EdgeInsets.all(24.w),
      child: Consumer<SymptomsCheckerViewModel>(
        builder: (BuildContext context, symptomsViewModel, Widget? child) {
          return Column(
            children: [
              LocaleKeys.genderSymptoms.tr(context: context).toText18(isBold: true, color: AppColors.textColor),
              SizedBox(height: 70.h),
              Row(
                children: [
                  Expanded(
                    child: InkWell(
                      onTap: () => onGenderSelected(maleKey),
                      child: _buildGenderOption(
                        AppAssets.maleIcon,
                        LocaleKeys.malE.tr(context: context),
                        _isGenderSelected(maleKey, symptomsViewModel.selectedGender, context),
                      ),
                    ),
                  ),
                  SizedBox(width: 16.w),
                  Expanded(
                    child: InkWell(
                      onTap: () => onGenderSelected(femaleKey),
                      child: _buildGenderOption(
                        AppAssets.femaleIcon,
                        LocaleKeys.femaleGender.tr(context: context),
                        _isGenderSelected(femaleKey, symptomsViewModel.selectedGender, context),
                      ),
                    ),
                  )
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}
