import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.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/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/shared/divider_with_spaces_around.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'app_texts_widget.dart';
import 'text_fields/app-textfield-custom.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 = [];
  TextEditingController filteredSearchController = TextEditingController();

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

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return Container(
      child: Column(
        children: [
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
                border: Border.fromBorderSide(BorderSide(
                  color: Colors.grey.shade400,
                  width: 0.4,
                )),
              ),
              height: MediaQuery.of(context).size.height * 0.60,
              child: Center(
                  child: Container(
                decoration: BoxDecoration(borderRadius: BorderRadius.circular(12), color: Colors.white),
                child: ListView(
                  children: [
                    AppTextFieldCustom(
                      height: Utils.getTextFieldHeight(),
                      //MediaQuery.of(context).size.height * 0.070,
                      hintText: TranslationBase.of(context).searchHistory,
                      isTextFieldHasSuffix: true,
                      hasBorder: false,
                      controller: filteredSearchController,
                      onChanged: (String? value) {
                        filterSearchResults(value!);
                      },
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.search,
                          color: Colors.black,
                        ),
                        onPressed: () {},
                      ),
                      onClick: () {},
                      onFieldSubmitted: () {},
                    ),

                    // SizedBox(height: 15,),
                    DividerWithSpacesAround(),
                    Container(
                      // padding:EdgeInsets.all(20),
                      child: Column(
                        children: items.map((historyInfo) {
                          return Column(
                            children: [
                              InkWell(
                                onTap: () {
                                  setState(() {
                                    if (widget.isServiceSelected!(historyInfo)) {
                                      widget.removeHistory!(historyInfo);
                                    } else {
                                      widget.addHistory!(historyInfo);
                                    }
                                  });
                                },
                                child: 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: AppText(
                                        projectViewModel.isArabic
                                            ? historyInfo.nameAr != ""
                                                ? historyInfo.nameAr!
                                                : historyInfo.nameEn!
                                            : historyInfo.nameEn!,
                                        color: Color(0xFF575757),
                                        fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * (SizeConfig.isWidthLarge ? 3 : 3.8),
                                        letterSpacing: -0.56,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              // DividerWithSpacesAround(),
                            ],
                          );
                        }).toList(),
                      ),
                    ),
                  ],
                ),
              )),
            ),
          ),
          SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }

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