import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/master_key_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/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 'package:provider/provider.dart';

class MasterKeyCheckboxSearchWidget extends StatefulWidget {
  final SOAPViewModel model;
  final Function addSelectedHistories;
  final Function(MasterKeyModel) removeHistory;
  final Function(MasterKeyModel) addHistory;
  final bool Function(MasterKeyModel) isServiceSelected;
  final List<MasterKeyModel> masterList;
  final String buttonName;
  final String hintSearchText;

  MasterKeyCheckboxSearchWidget(
      {Key key,
      this.model,
      this.addSelectedHistories,
      this.removeHistory,
      this.masterList,
      this.addHistory,
      this.isServiceSelected, this.buttonName, this.hintSearchText})
      : super(key: key);

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

class _MasterKeyCheckboxSearchWidgetState extends State<MasterKeyCheckboxSearchWidget> {
  List<MasterKeyModel> items = List();

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

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    return Container(
      child: Column(
        children: [
          Container(
            height: MediaQuery.of(context).size.height * 0.5,
            child: Center(
                child: Container(
              margin: EdgeInsets.only(top: 15),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(12),
                      color: Colors.white),
                  child: ListView(
                    children: [
                      TextFields(
                        hintText: widget.hintSearchText ?? TranslationBase
                            .of(context)
                            .searchHistory,
                        suffixIcon: EvaIcons.search,
                        onChanged: (value) {
                          filterSearchResults(value);
                        },
                      ),
                      SizedBox(height: 15,),
                      Column(
                        children: items.map((historyInfo) {
                          return Column(
                            children: [
                              Row(
                                children: [
                                  Checkbox(
                                      value:
                                      widget.isServiceSelected(historyInfo),
                                      activeColor: Colors.red[800],
                                      onChanged: (bool newValue) {
                                        setState(() {
                                          if (widget
                                              .isServiceSelected(historyInfo)) {
                                            widget.removeHistory(historyInfo);
                                          } else {
                                            widget.addHistory(historyInfo);
                                          }
                                        });
                                      }),
                                  Expanded(
                                    child: Padding(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 10, vertical: 0),
                                      child: Texts(projectViewModel.isArabic
                                          ? historyInfo.nameAr!=""?historyInfo.nameAr:historyInfo.nameEn
                                          : historyInfo.nameEn,
                                          variant: "bodyText",
                                          bold: true,
                                          color: Colors.black),
                                    ),
                                  ),
                                ],
                              ),
                              DividerWithSpacesAround(),
                            ],
                          );
                        }).toList(),
                      ),
                    ],
                  ),
                )),
          ),
          SizedBox(
            height: 10,
          ),
          if (widget.model.state == ViewState.Idle)
            AppButton(
              title: widget.buttonName?? TranslationBase.of(context).addSelectedHistories.toUpperCase(),
              padding: 10,
              onPressed: () {
                widget.addSelectedHistories();
              },
            ),
        ],
      ),
    );
  }

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