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/screens/procedures/expansion_procedure.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../../config/config.dart';
import '../../../widgets/shared/text_fields/app_text_field_custom_serach.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 = 0;
  int typeRegular = 0;

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

  List<ProcedureTempleteDetailsModelList> items = [];
  List<ProcedureTempleteDetailsModel> itemsProcedure = [];
  List<String> remarksList = [];
  List<int> typeList = [];

  @override
  void initState() {
    items = [...widget.model!.templateList];
    super.initState();
  }

  TextEditingController remarksController = TextEditingController();
  TextEditingController patientFileInfoController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: NetworkBaseView(
        baseViewModel: widget.model!,
        child: Container(
          height: MediaQuery.of(context).size.height * 0.90,
          child: Center(
              child: Container(
            margin: EdgeInsets.only(top: 15),
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), color: Colors.white),
            child: ListView(
              children: [
                AppTextFieldCustomSearch(
                  searchController: patientFileInfoController,
                  onChangeFun: (value) {
                    widget.model!.filterProcedureSearchResults(value, widget.model!.templateList, (items){
                      setState(() {
                        this.items = items;
                      });
                    });
                  },
                  marginTop: 5,
                  inputFormatters: [FilteringTextInputFormatter.allow(RegExp(ONLY_LETTERS))],
                  inputType: TextInputType.text,
                  hintText: TranslationBase.of(context).searchFavoriteTemplate,
                  onFieldSubmitted: () {},
                ),
                SizedBox(
                  height: 15,
                ),
                this.items.length != 0
                    ? Column(
                        children: this.items.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(TranslationBase.of(context).sorryNoMatch, color: AppGlobal.appRedColor),
                        ),
                      )
              ],
            ),
          )),
        ),
      ),
    );
  }
}
