import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/model/procedure/categories_procedure.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/Text.dart';
import 'package:doctor_app_flutter/widgets/shared/TextFields.dart';
import 'package:doctor_app_flutter/widgets/shared/app_buttons_widget.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';

class EntityListCheckboxSearchWidget extends StatefulWidget {
  final ProcedureViewModel model;
  final Function addSelectedHistories;
  final Function(EntityList) removeHistory;
  final Function(EntityList) addHistory;
  final Function(EntityList) addRemarks;

  final bool Function(EntityList) isEntityListSelected;
  final List<EntityList> masterList;

  EntityListCheckboxSearchWidget(
      {Key key,
      this.model,
      this.addSelectedHistories,
      this.removeHistory,
      this.masterList,
      this.addHistory,
      this.isEntityListSelected,
      this.addRemarks})
      : super(key: key);

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

class _EntityListCheckboxSearchWidgetState
    extends State<EntityListCheckboxSearchWidget> {
  int selectedType = 0;
  int typeUrgent;
  int typeRegular;

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

  List<EntityList> items = 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.45,
              child: Center(
                  child: Container(
                margin: EdgeInsets.only(top: 15),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12),
                    color: Colors.white),
                child: ListView(
                  children: [
                    TextFields(
                      hintText: TranslationBase.of(context).searchProcedures,
                      suffixIcon: EvaIcons.search,
                      onChanged: (value) {
                        filterSearchResults(value);
                      },
                    ),
                    SizedBox(
                      height: 15,
                    ),
                    items.length != 0
                        ? Column(
                            children: items.map((historyInfo) {
                              return Column(
                                children: [
                                  ExpansionTile(
                                    title: Row(
                                      children: [
                                        Checkbox(
                                            value: widget.isEntityListSelected(
                                                historyInfo),
                                            activeColor: Colors.red[800],
                                            onChanged: (bool newValue) {
                                              setState(() {
                                                if (widget.isEntityListSelected(
                                                    historyInfo)) {
                                                  widget.removeHistory(
                                                      historyInfo);
                                                } else {
                                                  widget
                                                      .addHistory(historyInfo);
                                                }
                                              });
                                            }),
                                        Expanded(
                                          child: Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 10, vertical: 0),
                                            child: Texts(
                                                historyInfo.procedureName,
                                                variant: "bodyText",
                                                bold: true,
                                                color: Colors.black),
                                          ),
                                        ),
                                      ],
                                    ),
                                    children: [
                                      Container(
                                        child: Row(
                                          children: [
                                            AppText(TranslationBase.of(context)
                                                .orderType),
                                            Radio(
                                              activeColor: Color(0xFFB9382C),
                                              value: 0,
                                              groupValue: selectedType,
                                              onChanged: (value) {
                                                historyInfo.type =
                                                    setSelectedType(value)
                                                        .toString();

                                                historyInfo.type =
                                                    value.toString();
                                              },
                                            ),
                                            Text('routine'),
                                            Radio(
                                              activeColor: Color(0xFFB9382C),
                                              groupValue: selectedType,
                                              value: 1,
                                              onChanged: (value) {
                                                historyInfo.type =
                                                    setSelectedType(value)
                                                        .toString();

                                                historyInfo.type =
                                                    value.toString();
                                              },
                                            ),
                                            Text(TranslationBase.of(context)
                                                .urgent),
                                          ],
                                        ),
                                      ),
                                      SizedBox(
                                        height: 15.0,
                                      ),
                                      TextFields(
                                        hintText:
                                            TranslationBase.of(context).remarks,
                                        //controller: remarksController,
                                        onChanged: (value) {
                                          historyInfo.remarks = value;
                                        },
                                        minLines: 3,
                                        maxLines: 5,
                                      ),
                                    ],
                                  ),
                                  DividerWithSpacesAround(),
                                ],
                              );
                            }).toList(),
                          )
                        : Center(
                            child: Container(
                              child: AppText(
                                  "There's no procedures for this category",
                                  color: Color(0xFFB9382C)),
                            ),
                          )
                  ],
                ),
              )),
            ),
          ),
          SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }

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