import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/physical_exam/GeneralSpeciality.dart';
import 'package:doctor_app_flutter/core/model/SOAP/physical_exam/patient_physical_examination.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_examination.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/objective/add_examination_vida_plus_widget.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';

class ExaminationsListSearchVidaPlusWidget extends StatefulWidget {
  final Function(GeneralSpeciality?) removeExamination;
  final Function(GeneralSpeciality?) addExamination;
  final List<GeneralSpeciality>? masterList;

  ExaminationsListSearchVidaPlusWidget(
      {required this.removeExamination,
      required this.addExamination,
      this.masterList});

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

class _ExaminationsListSearchVidaPlusWidgetState
    extends State<ExaminationsListSearchVidaPlusWidget> {
  int expandedIndex = -1;
  List<GeneralSpeciality>? items = [];
  TextEditingController filteredSearchController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return  BaseView<SOAPViewModel>(

    builder: (_, model, w) => Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: [
          Material(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10),
                side: BorderSide(
                  width: 1,
                  color: Color(0xFFEFEFEF),
                )),
            color: Colors.white,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: AppTextFieldCustom(
                height: Utils.getTextFieldHeight(),
                hintText: TranslationBase.of(context).searchExamination,
                isTextFieldHasSuffix: false,
                hasBorder: false,
                controller: filteredSearchController,
                onChanged: (value) {
                  if (value != null) filterSearchResults(model,value);
                },
                suffixWidget: InkWell(
                  child: Icon(
                    Icons.search,
                    color: Colors.black,
                  ),
                  onTap: () {},
                ),
                onClick: () {},
                onFieldSubmitted: () {},
              ),
            ),
          ),


          ...model.speciality.mapIndexed((index, item) {
            return Column(
              children: [
                AddExaminationVidaPlusWidget(
                  item: item,
                  addExamination: widget.addExamination,
                  removeExamination: widget.removeExamination,
                ),
                SizedBox(height: 8,),
                Divider()
              ],
            );
          }).toList(),
        ],
      ),
    ));
  }

  void filterSearchResults(SOAPViewModel model,String query) {
    model.filterData(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]);
    }
  }
}
