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/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/symptoms_checker/models/organ_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/chip/app_custom_chip_widget.dart';

class SelectedOrgansSection extends StatefulWidget {
  final List<OrganModel> selectedOrgans;
  final Function(String organId) onRemoveOrgan;

  const SelectedOrgansSection({
    super.key,
    required this.selectedOrgans,
    required this.onRemoveOrgan,
  });

  @override
  State<SelectedOrgansSection> createState() => _SelectedOrgansSectionState();
}

class _SelectedOrgansSectionState extends State<SelectedOrgansSection> {
  bool _isExpanded = true;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(16.h),
      decoration: BoxDecoration(
        color: AppColors.whiteColor,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: [
          BoxShadow(
            color: AppColors.blackColor.withValues(alpha: 0.05),
            blurRadius: 10,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          GestureDetector(
            onTap: () {
              setState(() {
                _isExpanded = !_isExpanded;
              });
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  LocaleKeys.selectedOrgans.tr(context: context),
                  style: TextStyle(
                    fontSize: 16.f,
                    // isBold: true,
                    fontWeight: FontWeight.w600,
                    color: AppColors.textColor,
                  ),
                ),
                Row(
                  children: [
                    if (widget.selectedOrgans.isNotEmpty)
                      Container(
                        padding: EdgeInsets.symmetric(
                          horizontal: 8.w,
                          vertical: 4.h,
                        ),
                        decoration: BoxDecoration(
                          color: AppColors.primaryRedColor,
                          borderRadius: BorderRadius.circular(12.r),
                        ),
                        child: Text(
                          '${widget.selectedOrgans.length}',
                          style: TextStyle(
                            color: AppColors.whiteColor,
                            fontSize: 12.f,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    SizedBox(width: 8.w),
                    AnimatedRotation(
                      turns: _isExpanded ? 0.5 : 0.0,
                      duration: const Duration(milliseconds: 200),
                      child: Icon(
                        Icons.keyboard_arrow_down,
                        color: AppColors.textColorLight,
                        size: 24.h,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          if (_isExpanded) ...[
            SizedBox(height: 16.h),
            if (widget.selectedOrgans.isEmpty)
              Padding(
                padding: EdgeInsets.symmetric(vertical: 8.h),
                child: Text(
                  LocaleKeys.noOrgansSelected.tr(context: context),
                  style: TextStyle(
                    color: AppColors.greyTextColor,
                    fontSize: 14.f,
                  ),
                ),
              )
            else
              Wrap(
                spacing: 8.w,
                runSpacing: 8.h,
                children: widget.selectedOrgans.map((organ) {
                  return AppCustomChipWidget(
                    labelText: organ.description,
                    backgroundColor: AppColors.secondaryLightRedColor,
                    textColor: AppColors.primaryRedColor,
                    deleteIcon: AppAssets.cross_circle,
                    deleteIconColor: AppColors.primaryRedColor,
                    deleteIconHasColor: true,
                    onDeleteTap: () => widget.onRemoveOrgan(organ.id),
                    padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
                  );
                }).toList(),
              ),
          ],
        ],
      ),
    );
  }
}
