import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/size_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/theme/colors.dart';

class OffersAndDiscountTypeSelectionWidget extends StatelessWidget {
  final List<String> selectedOffers;
  final Function(List<String>) onOfferClicked;

  const OffersAndDiscountTypeSelectionWidget({
    super.key,
    required this.selectedOffers,
    required this.onOfferClicked,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Row(
        children: [
          _buildOfferTypeCard(
            context: context,
            title: "All Offers",
            facilityType: 'All Offers',
            isSelected: selectedOffers.contains('All Offers'),
          ),
          SizedBox(width: 12.w),
          _buildOfferTypeCard(
            context: context,
            title: 'Female',
            facilityType: 'Female',
            isSelected: selectedOffers.contains('Female'),
          ),
          SizedBox(width: 12.w),
          _buildOfferTypeCard(
            context: context,
            title: 'OB-Gyne',
            facilityType: 'OB-Gyne',
            isSelected: selectedOffers.contains('OB-Gyne'),
          ),
          SizedBox(width: 12.w),
          _buildOfferTypeCard(
            context: context,
            title: 'Dermatology',
            facilityType: 'Dermatology',
            isSelected: selectedOffers.contains('Dermatology'),
          ),
          SizedBox(width: 12.w),
          _buildOfferTypeCard(
            context: context,
            title: 'Radiology',
            facilityType: 'Radiology',
            isSelected: selectedOffers.contains('Radiology'),
          ),
        ],
      ),
    );
  }

  Widget _buildOfferTypeCard({
    required BuildContext context,
    required String title,
    required String facilityType,
    required bool isSelected,
  }) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 200),
      padding: EdgeInsets.symmetric(vertical: 12.h, horizontal: 16.w),
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: isSelected ? AppColors.bgRedLightColor : AppColors.whiteColor,
        borderRadius: 12.r,
        hasShadow: true,
        side: isSelected ? BorderSide(color: AppColors.primaryRedColor, width: 2) : BorderSide(color: AppColors.borderGrayColor, width: 1),
      ),
      child: Center(
        child: title.toText14(
          color: isSelected ? AppColors.primaryRedColor : AppColors.textColor,
          weight: isSelected ? FontWeight.w700 : FontWeight.w500,
          isBold: isSelected,
        ),
      ),
    ).onPress(() {
      List<String> updatedSelection = List.from(selectedOffers);

      if (facilityType == 'All Offers') {
        if (updatedSelection.contains('All Offers')) {
          updatedSelection.clear();
        } else {
          updatedSelection.clear();
          updatedSelection.add('All Offers');
        }
      } else {
        // Remove "All Offers" if any specific type is selected
        updatedSelection.remove('All Offers');

        // Toggle the clicked facility type
        if (updatedSelection.contains(facilityType)) {
          updatedSelection.remove(facilityType);
          // If no selection left, default to "All Offers"
          if (updatedSelection.isEmpty) {
            updatedSelection.add('All Offers');
          }
        } else {
          updatedSelection.add(facilityType);
        }
      }

      onOfferClicked(updatedSelection);
    });
  }
}
