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/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.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/input_widget.dart';

class SearchRadiologyContent extends StatefulWidget {
  final List<String> radiologySuggestionsList;

  const SearchRadiologyContent({super.key, required this.radiologySuggestionsList});

  @override
  State<SearchRadiologyContent> createState() => _SearchRadiologyContentContentState();
}

class _SearchRadiologyContentContentState extends State<SearchRadiologyContent> {
  TextEditingController searchEditingController = TextEditingController();
  List<String> filteredSuggestions = [];

  @override
  void initState() {
    super.initState();
    filteredSuggestions = List.from(widget.radiologySuggestionsList);

    // Listen for changes in the search field
    searchEditingController.addListener(() {
      filterSuggestions();
    });
  }

  @override
  void dispose() {
    searchEditingController.dispose();
    super.dispose();
  }

  void filterSuggestions() {
    final query = searchEditingController.text.toLowerCase();

    if (query.isEmpty) {
      setState(() {
        filteredSuggestions = List.from(widget.radiologySuggestionsList);
      });
    } else {
      setState(() {
        filteredSuggestions = widget.radiologySuggestionsList.where((suggestion) => suggestion.toLowerCase().contains(query)).toList();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return CollapsingListView(
      title: LocaleKeys.radiology.tr(),
      isClose: true,
      bottomChild: Container(
        color: AppColors.whiteColor,
        padding: EdgeInsets.all(ResponsiveExtension(20).h),
        child: CustomButton(
          text: LocaleKeys.search.tr(),
          icon: AppAssets.search_icon,
          iconColor: Colors.white,
          onPressed: () => Navigator.pop(context, searchEditingController.text),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 24, right: 24, top: 24),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                TextInputWidget(
                  fontFamily: "Poppins",
                  labelText: LocaleKeys.search.tr(context: context),
                  hintText: "",
                  controller: searchEditingController,
                  isEnable: true,
                  prefix: null,
                  autoFocus: false,
                  isBorderAllowed: false,
                  keyboardType: TextInputType.text,
                  padding: EdgeInsets.symmetric(
                    vertical: ResponsiveExtension(9).h,
                    horizontal: ResponsiveExtension(15).h,
                  ),
                ),
                SizedBox(height: ResponsiveExtension(20).h),
                if (filteredSuggestions.isNotEmpty) ...[
                  LocaleKeys.suggestions.tr(context: context).toText16(isBold: true),
                ],
              ],
            ),
          ),
          SingleChildScrollView(
            physics: NeverScrollableScrollPhysics(),
            padding: const EdgeInsets.only(left: 24, right: 24, bottom: 24, top: 16),
            child: Wrap(
              alignment: WrapAlignment.start,
              spacing: 10,
              runSpacing: 10,
              children: filteredSuggestions
                  .map((label) => SuggestionChip(
                label: label,
                onTap: () {
                  searchEditingController.text = label;
                },
              ))
                  .toList(),
            ),
          ),
        ],
      ),
    );
  }
}

class SuggestionChip extends StatelessWidget {
  final String label;
  final bool isSelected;
  final VoidCallback? onTap;

  const SuggestionChip({
    super.key,
    required this.label,
    this.isSelected = false,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
        decoration: BoxDecoration(
          color: isSelected ? AppColors.primaryRedColor : AppColors.whiteColor,
          borderRadius: BorderRadius.circular(8),
        ),
        child: label.toText12(
          // color: isSelected ? Colors.white : Colors.black87,
          color: AppColors.textColor,
            isBold: true,
            isEnglishOnly: true),
      ),
    );
  }
}
