import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_examination.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/bottom_sheet/custom_bottom_sheet_container.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:flutter/material.dart';

import 'examinations_list_search_widget.dart';

class AddExaminationPage extends StatefulWidget {
  final List<MySelectedExamination>? mySelectedExamination;
  final Function(List<MySelectedExamination>) addSelectedExamination;
  final Function(MasterKeyModel) removeExamination;

  AddExaminationPage({this.mySelectedExamination, required this.addSelectedExamination, required this.removeExamination});

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

class _AddExaminationPageState extends State<AddExaminationPage> {
  List<MySelectedExamination> mySelectedExaminationLocal = [];

  @override
  initState() {
    super.initState();
    mySelectedExaminationLocal = widget.mySelectedExamination!;
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<SOAPViewModel>(
        onModelReady: (model) async {
          if (model.physicalExaminationList.length == 0) {
            WidgetsBinding.instance.addPostFrameCallback((_) async {
              await model.getMasterLookup(
                MasterKeysService.PhysicalExamination,
              );
            });
          }
        },
        builder: (_, model, w) => AppScaffold(
              baseViewModel: model,
              isShowAppBar: true,
              appBar: BottomSheetTitle(
                title: "${TranslationBase.of(context).addExamination}",
              ),
              backgroundColor: Color.fromRGBO(248, 248, 248, 1),
              body: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                    child: SingleChildScrollView(
                      child: Column(
                        children: [
                          Container(
                            margin: EdgeInsets.all(16.0),
                            padding: EdgeInsets.all(0.0),
                            decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(12),
                              border: Border.fromBorderSide(BorderSide(
                                color: Colors.grey.shade400,
                                width: 0.4,
                              )),
                            ),
                            child: Column(
                              children: [
                                ExaminationsListSearchWidget(
                                  mySelectedExamination: widget.mySelectedExamination,
                                  masterList: model.physicalExaminationList,
                                  isServiceSelected: (master) => isServiceSelected(master),
                                  removeExamination: (selectedExamination) {
                                    setState(() {
                                      mySelectedExaminationLocal.remove(selectedExamination);
                                    });
                                  },
                                  addExamination: (selectedExamination) {
                                    mySelectedExaminationLocal.insert(0, selectedExamination);
                                    //setState(() {});
                                  },
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              bottomSheet:

                  // model.state != ViewState.Idle
                  //     ? Container(
                  //         height: 10,
                  //         color: Colors.red,
                  //       )
                  //     :

                  CustomBottomSheetContainer(
                label: "${TranslationBase.of(context).addExamination}",
                onTap: () {
                  widget.addSelectedExamination(mySelectedExaminationLocal);
                },
              ),
            ));
  }

  isServiceSelected(MasterKeyModel masterKey) {
    Iterable<MySelectedExamination> exam = mySelectedExaminationLocal.where((element) => masterKey.id == element.selectedExamination!.id && masterKey.typeId == element.selectedExamination!.typeId);
    if (exam.length > 0) {
      return true;
    }
    return false;
  }
}
