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/app_state.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/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/medical_file/models/family_file_response_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:hmg_patient_app_new/widgets/chip/custom_chip_widget.dart';

class FamilyCards extends StatefulWidget {
  final List<FamilyFileResponseModelLists> profiles;
  final Function(FamilyFileResponseModelLists) onSelect;
  final bool isShowDetails;
  final bool isBottomSheet;

  const FamilyCards({super.key, required this.profiles, required this.onSelect, this.isShowDetails = false, this.isBottomSheet = false});

  @override
  State<FamilyCards> createState() => _FamilyCardsState();
}

class _FamilyCardsState extends State<FamilyCards> {
  AppState appState = getIt<AppState>();

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: widget.profiles.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 10.h,
        mainAxisSpacing: 10.h,
        childAspectRatio: widget.isShowDetails ? 0.56.h : 0.74.h,
      ),
      itemBuilder: (context, index) {
        final profile = widget.profiles[index];
        final isActive = (profile.responseId == appState.getAuthenticatedUser()?.patientId);
        return Container(
          padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 15.h),
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24),
          child: Opacity(
            opacity: isActive ? 0.4 : 1.0, // Fade all content if active
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(height: 5.h),
                Utils.buildImgWithAssets(
                    icon: profile.gender == 1 ? ((profile.age ?? 0) < 7 ? AppAssets.babyBoyImg : AppAssets.male_img) : (profile.age! < 7 ? AppAssets.babyGirlImg : AppAssets.femaleImg),
                    width: 80.h,
                    height: 78.h),
                SizedBox(height: 8.h),
                (profile.patientName ?? "Unknown").toText16(isBold: false, isCenter: true, maxlines: 1, weight: FontWeight.w600),
                SizedBox(height: 4.h),
                CustomChipWidget(
                    chipType: ChipTypeEnum.alert,
                    backgroundColor: AppColors.lightGrayBGColor,
                    chipText: "Relation: ${profile.relationship ?? "N/A"}",
                    iconAsset: AppAssets.heart,
                    isShowBorder: false,
                    borderRadius: 8.h,
                    textColor: AppColors.textColor),
                widget.isShowDetails ? SizedBox(height: 4.h) : SizedBox(),
                widget.isShowDetails
                    ? CustomChipWidget(
                        chipType: ChipTypeEnum.alert,
                        backgroundColor: AppColors.lightGrayBGColor,
                        chipText: "Age: ${profile.age ?? "N/A"} Years",
                        isShowBorder: false,
                        borderRadius: 8.h,
                        textColor: AppColors.textColor,
                      )
                    : SizedBox(),
                widget.isShowDetails ? SizedBox(height: 8.h) : SizedBox(),
                Spacer(),
                if (isActive)
                  CustomButton(
                    height: 40.h,
                    onPressed: () {},
                    text: LocaleKeys.active.tr(),
                    backgroundColor: Colors.grey.shade200,
                    borderColor: Colors.grey.shade200,
                    textColor: AppColors.greyTextColor,
                    fontSize: 13.h,
                  ).paddingOnly(top: 0, bottom: 0)
                else
                  CustomButton(
                    height: 40.h,
                    onPressed: () => widget.onSelect(profile),
                    text: LocaleKeys.select.tr(),
                    backgroundColor: AppColors.secondaryLightRedColor,
                    borderColor: AppColors.secondaryLightRedColor,
                    textColor: AppColors.primaryRedColor,
                    fontSize: 13.h,
                    icon: widget.isBottomSheet ? null : AppAssets.heart,
                    iconColor: AppColors.primaryRedColor,
                    padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                  ).paddingOnly(top: 0, bottom: 0),
              ],
            ),
          ),
        );
      },
    );
  }
}
