import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_examination.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 'add_examination_widget.dart';

class ExaminationsListSearchWidget extends StatefulWidget {
  final Function(MySelectedExamination) removeExamination;
  final Function(MySelectedExamination) addExamination;
  final bool Function(MasterKeyModel) isServiceSelected;
  final List<MasterKeyModel>? masterList;
  final List<MySelectedExamination>? mySelectedExamination;

  ExaminationsListSearchWidget({required this.removeExamination, required this.addExamination, required this.isServiceSelected, this.masterList, this.mySelectedExamination});

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

class _ExaminationsListSearchWidgetState extends State<ExaminationsListSearchWidget> {
  int expandedIndex = -1;
  List<MasterKeyModel>? items = [];
  TextEditingController filteredSearchController = TextEditingController();

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        AppTextFieldCustom(
          height: Utils.getTextFieldHeight(),
          hintText: TranslationBase.of(context).searchExamination,
          isTextFieldHasSuffix: true,
          hasBorder: false,
          controller: filteredSearchController,
          onChanged: (value) {
            if (value != null) filterSearchResults(value);
          },
          suffixIcon: IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.black,
            ),
            onPressed: () {},
          ),
          onClick: () {},
          onFieldSubmitted: () {},
        ),
        DividerWithSpacesAround(
          height: 2,
        ),
        ...items!.mapIndexed((index, item) {
          return AddExaminationWidget(
            item: item,
            addExamination: widget.addExamination,
            removeExamination: widget.removeExamination,
            mySelectedExamination: widget.mySelectedExamination,
            isServiceSelected: widget.isServiceSelected,
            isExpand: index == expandedIndex,
            expandClick: () {
              setState(() {
                if (expandedIndex == index) {
                  expandedIndex = -1;
                } else {
                  expandedIndex = index;
                }
              });
            },
          );
        }).toList(),
      ],
    );
  }

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

extension FicListExtension<T> on List<T> {
  /// Maps each element of the list.
  /// The [map] function gets both the original [item] and its [index].
  Iterable<E> mapIndexed<E>(E Function(int index, T item) map) sync* {
    for (var index = 0; index < length; index++) {
      yield map(index, this[index]);
    }
  }
}
