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 '../../widgets/shared/text_fields/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';

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;

  /// todo clear the function here
  EntityListCheckboxSearchWidget(
      {Key? key,
      required this.model,
      required this.addSelectedHistories,
      required this.removeHistory,
      required this.masterList,
      required this.addHistory,
      required this.isEntityListSelected,
      this.addRemarks})
      : super(key: key);

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

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

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

  List<EntityList> items = [];
  List<String> remarksList = [];
  List<int> typeList = [];

  @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.75,
              child: Center(
                  child: Container(
                margin: EdgeInsets.only(top: 15),
                decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), color: Colors.white),
                child: ListView(
                  children: [
                    TextFields(
                      hintText: TranslationBase.of(context).searchProcedures,
                      suffixIcon: EvaIcons.search,
                      suffixIconColor: Color(0xff2B353E),
                      onChanged: (value) {
                        filterSearchResults(value);
                      },
                      hasBorder: false,
                    ),
                    SizedBox(
                      height: 15,
                    ),
                    items.length != 0
                        ? Column(
                            children: items.map((historyInfo) {
                              return Column(
                                children: [
                                  ExpansionTile(
                                    title: Row(
                                      children: [
                                        Checkbox(
                                            value: widget.isEntityListSelected(historyInfo),
                                            activeColor: Color(0xffD02127),
                                            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: AppText(historyInfo.procedureName,
                                                fontSize: 14.0,
                                                variant: "bodyText",
                                                bold: true,
                                                color: Color(0xff575757)),
                                          ),
                                        ),
                                      ],
                                    ),
                                    children: [
                                      Container(
                                        child: Padding(
                                          padding: EdgeInsets.symmetric(horizontal: 12),
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Row(
                                                children: [
                                                  Padding(
                                                    padding: const EdgeInsets.symmetric(horizontal: 11),
                                                    child: AppText(
                                                      TranslationBase.of(context).orderType,
                                                      fontWeight: FontWeight.w700,
                                                      color: Color(0xff2B353E),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              Row(
                                                children: [
                                                  Radio(
                                                    activeColor: Color(0xFFD02127),
                                                    value: 0,
                                                    groupValue: selectedType,
                                                    onChanged: (int? value) {
                                                      historyInfo.type = setSelectedType(value!).toString();

                                                      historyInfo.type = value.toString();
                                                    },
                                                  ),
                                                  AppText(
                                                    'routine',
                                                    color: Color(0xff575757),
                                                    fontWeight: FontWeight.w600,
                                                  ),
                                                  Radio(
                                                    activeColor: Color(0xFFD02127),
                                                    groupValue: selectedType,
                                                    value: 1,
                                                    onChanged: (int? value) {
                                                      historyInfo.type = setSelectedType(value!).toString();

                                                      historyInfo.type = value.toString();
                                                    },
                                                  ),
                                                  AppText(
                                                    TranslationBase.of(context).urgent,
                                                    color: Color(0xff575757),
                                                    fontWeight: FontWeight.w600,
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        height: 2.0,
                                      ),
                                      Padding(
                                        padding: EdgeInsets.symmetric(horizontal: 12, vertical: 12.0),
                                        child: TextFields(
                                          hintText: TranslationBase.of(context).remarks,
                                          //controller: remarksController,
                                          onChanged: (value) {
                                            historyInfo.remarks = value;
                                          },
                                          minLines: 3,
                                          maxLines: 5,
                                          borderWidth: 0.5,
                                          borderColor: Colors.grey[500],
                                        ),
                                      ),
                                      DividerWithSpacesAround(),
                                    ],
                                  ),
                                ],
                              );
                            }).toList(),
                          )
                        : Center(
                            child: Container(
                              child: AppText("Sorry , No Match", color: Color(0xFFB9382C)),
                            ),
                          )
                  ],
                ),
              )),
            ),
          ),
          SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }

  void filterSearchResults(String query) {
    List<EntityList> dummySearchList = [];
    dummySearchList.addAll(widget.masterList);
    if (query.isNotEmpty) {
      List<EntityList> dummyListData = [];
      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);
      });
    }
  }
}
