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/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:sizer/sizer.dart';

class SearchLabResultsContent extends StatefulWidget {
  final List<String> labSuggestionsList;

  const SearchLabResultsContent({super.key, required this.labSuggestionsList});

  @override
  State<SearchLabResultsContent> createState() => _SearchLabResultsContentState();
}

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

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

    // 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.labSuggestionsList);
      });
    } else {
      setState(() {
        filteredSuggestions = widget.labSuggestionsList
            .where((suggestion) => suggestion.toLowerCase().contains(query))
            .toList();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextInputWidget(
                labelText: "Search lab results",
                hintText: "Type test name",
                controller: searchEditingController,
                isEnable: true,
                prefix: null,
                autoFocus: true,
                isBorderAllowed: false,
                padding: EdgeInsets.symmetric(
                  vertical: ResponsiveExtension(10).h,
                  horizontal: ResponsiveExtension(15).h,
                ),
              ),
              SizedBox(height: ResponsiveExtension(20).h),
              if (filteredSuggestions.isNotEmpty) ...[
                "Suggestions".toText16(isBold: true),
                const SizedBox(height: 12),
              ],
            ],
          ),
        ),
        Expanded(
          child: SingleChildScrollView(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Wrap(
              alignment: WrapAlignment.start,
              spacing: 10,
              runSpacing: 10,
              children: filteredSuggestions
                  .map((label) => SuggestionChip(
                label: label,
                onTap: () {
                  searchEditingController.text = label;
                },
              ))
                  .toList(),
            ),
          ),
        ),
        Container(
          color: Colors.white,
          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),
          ),
        ),
      ],
    );
  }
}

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),
          border: Border.all(
            color: AppColors.greyColor,
            width: 1,
          ),
        ),
        child: label.toText12(
          color: isSelected ? Colors.white : Colors.black87,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }
}