import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/physical_exam/GeneralSpeciality.dart';
import 'package:doctor_app_flutter/core/model/SOAP/physical_exam/patient_physical_examination.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_examination.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/authentication_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patient_search_header.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/objective/add_details_to_examination_vida_plus.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

import 'examinations_list_search_vida_plus_widget.dart';

class AddExaminationPageVidaPlus extends StatefulWidget {
  final PatiantInformtion patientInfo;
  final List<PatientPhysicalExamination>? examination;

  ///todo handle the data if edit is added
  AddExaminationPageVidaPlus({required this.patientInfo, this.examination});

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

class _AddExaminationPageVidaPlusState
    extends State<AddExaminationPageVidaPlus> {
  List<GeneralSpeciality> mySelectedExaminationLocal = [];

  @override
  initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<SOAPViewModel>(
      onModelReady: (model) async {
          WidgetsBinding.instance.addPostFrameCallback((_) async =>
              await model.getGeneralSpeciality(widget.patientInfo)
          );
      },
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        isLoading: model.state == ViewState.BusyLocal,
        appBar: PatientSearchHeader(
            title: TranslationBase.of(context).examinationPart),
        backgroundColor: Colors.white,
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: [
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: Material(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                      side: BorderSide(
                        width: 1,
                        color: Color(0xFFEFEFEF),
                      )),
                  color: Colors.white,
                  child: ExaminationsListSearchVidaPlusWidget(
                    masterList: model.speciality,
                    removeExamination: (selectedExamination) {
                      if(selectedExamination == null ) return;
                      setState(() {
                        mySelectedExaminationLocal
                            .remove(selectedExamination);
                        // Find the object by its ID
                        try {
                          model.changeCurrentCategorySelectedState(selectedExamination, false);
                        } catch (e) {}
                      });
                    },
                    addExamination: (selectedExamination) {
                      if(selectedExamination == null ) return;
          
                      mySelectedExaminationLocal.insert(
                          0, selectedExamination);
          
                      try {
                        model.changeCurrentCategorySelectedState(selectedExamination, true);
                      } catch (e) {}
                      //setState(() {});
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
        bottomNavigationBar: Material(
            color: Colors.white,
            child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Row(
                  children: [
                    Expanded(
                      child: AppButton(
                        title: TranslationBase.of(context).cancelSmall,
                        color: Color(0xffEAEAEA),
                        fontColor: Colors.black,
                        fontWeight: FontWeight.w600,
                        onPressed: () async {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                      child: AppButton(
                        title: TranslationBase.of(context).next,
                        color: Color(0xffD02127),
                        fontColor: Colors.white,
                        fontWeight: FontWeight.w600,
                        onPressed: () async {
                          if(mySelectedExaminationLocal.isEmpty){
                            DrAppToastMsg.showErrorToast(TranslationBase.of(context).kindlySelectAllMandatoryField);
                            return;
                          }
                          pushAddExamination(mySelectedExaminationLocal);
                        },
                      ),
                    ),
                  ],
                ))),
      ),
    );
  }

  void pushAddExamination(
      List<GeneralSpeciality> mySelectedExaminationLocal) {
    Navigator.push(
      context,
      FadePage(
        page: AddDetailsToExaminationVidaPlus(
          mySelectedExamination: mySelectedExaminationLocal,
          patientInfo: widget.patientInfo,
        ),
      ),
    );
  }
}
