import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_allergy.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/shared/divider_with_spaces_around.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';

import 'allergies_item.dart';

class MasterKeyCheckboxSearchAllergiesWidget extends StatefulWidget {
  final SOAPViewModel model;
  final Function() addSelectedAllergy;
  final Function(MasterKeyModel) removeAllergy;
  final Function(MySelectedAllergy mySelectedAllergy) addAllergy;
  final bool Function(MasterKeyModel) isServiceSelected;
  final MySelectedAllergy Function(MasterKeyModel) getServiceSelectedAllergy;

  final List<MasterKeyModel> masterList;
  final String? buttonName;
  final String? hintSearchText;

  MasterKeyCheckboxSearchAllergiesWidget(
      {Key? key,
      required this.model,
      required this.addSelectedAllergy,
      required this.removeAllergy,
      required this.masterList,
      required this.addAllergy,
      required this.isServiceSelected,
      this.buttonName,
      this.hintSearchText,
      required this.getServiceSelectedAllergy})
      : super(key: key);

  @override
  _MasterKeyCheckboxSearchAllergiesWidgetState createState() => _MasterKeyCheckboxSearchAllergiesWidgetState();
}

class _MasterKeyCheckboxSearchAllergiesWidgetState extends State<MasterKeyCheckboxSearchAllergiesWidget> {
  List<MasterKeyModel> items = [];

  TextEditingController filteredSearchController = TextEditingController();

  @override
  void initState() {
    items.addAll(widget.masterList);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(
            child: Container(
              height: Utils.getTextFieldHeight(),
              child: Center(
                child: Container(
                  decoration: BoxDecoration(borderRadius: BorderRadius.circular(12), color: Colors.white),
                  child: Column(
                    children: [
                      AppTextFieldCustom(
                        hintText: TranslationBase.of(context).selectAllergy,
                        isTextFieldHasSuffix: true,
                        hasBorder: false,
                        controller: filteredSearchController,
                        onChanged: (value) {
                          // filterSearchResults(value);
                        },
                        onFieldSubmitted: () {

                            // widget.model.getMasterLookup(
                            //     MasterKeysService.Allergies,
                            //     searchKey: filteredSearchController.text);
                            // widget.model.getMasterLookup(
                            //     MasterKeysService.AllergySeverity,
                            //     searchKey: "");
                        },
                        // onFieldSubmitted: (value) {
                        //   widget.model.getMasterLookup(MasterKeysService.Allergies!, searchKey: filteredSearchController.text!);
                        //   widget.model.getMasterLookup(MasterKeysService.AllergySeverity, searchKey: "");
                        // },
                        suffixIcon: IconButton(
                          icon: Icon(
                            Icons.search,
                            color: Colors.black,
                          ),
                          onPressed: () {

                            widget.model.getMasterLookup(MasterKeysService.Allergies, searchKey: filteredSearchController.text);
                            widget.model.getMasterLookup(MasterKeysService.AllergySeverity, searchKey: "");
                          },
                        ),
                      ),
                      DividerWithSpacesAround(),
                      SizedBox(
                        height: 10,
                      ),
                      Expanded(
                        child: FractionallySizedBox(
                          widthFactor: 0.9,
                          child: Container(
                            height: MediaQuery.of(context).size.height * 0.60,
                            child: widget.model.state == ViewState.Idle
                                ? ListView.builder(
                                    itemCount: widget.model.allergiesList.length,
                                    itemBuilder: (context, index) {
                                      return AddAllergiesItem(
                                        item: widget.model.allergiesList[index],
                                        model: widget.model,
                                        removeAllergy: widget.removeAllergy,
                                        addAllergy: widget.addAllergy,
                                        isServiceSelected: widget.isServiceSelected,
                                        getServiceSelectedAllergy: widget.getServiceSelectedAllergy,
                                      );
                                    },
                                  )
                                : widget.model.state == ViewState.Busy
                                    ? Center(child: CircularProgressIndicator())
                                    : Container(),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }

  void filterSearchResults(String query) {
    List<MasterKeyModel> dummySearchList = [];
    dummySearchList.addAll(widget.masterList);
    if (query.isNotEmpty) {
      List<MasterKeyModel> dummyListData = [];
      dummySearchList.forEach((items) {
        if (items.nameAr!.toLowerCase().contains(query.toLowerCase()) || items.nameEn!.toLowerCase().contains(query.toLowerCase())) {
          dummyListData.add(items);
        }
      });
      setState(() {
        items.clear();
        items.addAll(dummyListData);
      });
      return;
    } else {
      setState(() {
        items.clear();
        items.addAll(widget.masterList);
      });
    }
  }
}
