import 'package:doctor_app_flutter/core/model/procedure/categories_procedure.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_templateModel.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_template_details_model.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/TextFields.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/divider_with_spaces_around.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'ExpansionProcedure.dart';

class EntityListCheckboxSearchFavProceduresWidget extends StatefulWidget {
  final ProcedureViewModel model;
  final Function addSelectedHistories;
  final Function(ProcedureTempleteModel) removeHistory;
  final Function(ProcedureTempleteModel) addHistory;
  final Function(ProcedureTempleteModel) addRemarks;

  final Function(ProcedureTempleteDetailsModel) removeFavProcedure;
  final Function(ProcedureTempleteDetailsModel) addFavProcedure;
  final Function(ProcedureTempleteDetailsModel) selectProcedures;
  final ProcedureTempleteDetailsModel groupProcedures;

  final bool Function(ProcedureTempleteModel) isEntityListSelected;
  final bool Function(ProcedureTempleteDetailsModel) isEntityFavListSelected;
  final List<ProcedureTempleteModel> masterList;

  final bool isProcedure;

  EntityListCheckboxSearchFavProceduresWidget(
      {Key key,
      this.model,
      this.addSelectedHistories,
      this.removeHistory,
      this.masterList,
      this.addHistory,
      this.addFavProcedure,
      this.selectProcedures,
      this.removeFavProcedure,
      this.isEntityListSelected,
      this.isEntityFavListSelected,
      this.addRemarks,
      this.isProcedure = true,
      this.groupProcedures})
      : super(key: key);

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

class _EntityListCheckboxSearchFavProceduresWidgetState extends State<EntityListCheckboxSearchFavProceduresWidget> {
  int selectedType = 0;
  int typeUrgent;
  int typeRegular;

  setSelectedType(int val) {
    setState(() {
      selectedType = val;
    });
  }

  List<ProcedureTempleteModel> items = List();
  List<ProcedureTempleteDetailsModel> itemsProcedure = List();
  List<String> remarksList = List();
  List<int> typeList = List();

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

  TextEditingController remarksController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          NetworkBaseView(
            baseViewModel: widget.model,
            child: Container(
              height: MediaQuery.of(context).size.height * 0.60,
              child: Center(
                  child: Container(
                margin: EdgeInsets.only(top: 15),
                decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), color: Colors.white),
                child: ListView(
                  children: [
                    TextFields(
                      hintText: 'Search Favourite templates',
                      suffixIcon: EvaIcons.search,
                      suffixIconColor: Color(0xff2B353E),
                      onChanged: (value) {
                        filterSearchResults(value);
                      },
                      hasBorder: false,
                    ),
                    SizedBox(
                      height: 15,
                    ),
                    widget.model.templateList.length != 0
                        ? Column(
                            children: widget.model.templateList.map((historyInfo) {
                              return ExpansionProcedure(
                                  procedureTempleteModel: historyInfo,
                                  model: widget.model,
                                  removeFavProcedure: widget.removeFavProcedure,
                                  addFavProcedure: widget.addFavProcedure,
                                  selectProcedures: widget.selectProcedures,
                                  isEntityListSelected: widget.isEntityListSelected,
                                  isEntityFavListSelected: widget.isEntityFavListSelected,
                                  isProcedure: widget.isProcedure,
                                  groupProcedures: widget.groupProcedures);
                            }).toList(),
                          )
                        : Center(
                            child: Container(
                              child: AppText("Sorry , No Match", color: Color(0xFFB9382C)),
                            ),
                          )
                  ],
                ),
              )),
            ),
          ),
        ],
      ),
    );
  }

  void filterSearchResults(String query) {
    List<ProcedureTempleteModel> dummySearchList = List();
    dummySearchList.addAll(widget.masterList);
    if (query.isNotEmpty) {
      List<ProcedureTempleteModel> dummyListData = List();
      dummySearchList.forEach((item) {
        if (item.templateName.toLowerCase().contains(query.toLowerCase())) {
          dummyListData.add(item);
        }
      });
      setState(() {
        items.clear();
        items.addAll(dummyListData);
      });
      return;
    } else {
      setState(() {
        items.clear();
        items.addAll(widget.masterList);
      });
    }
  }
}
