import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/search_drug/get_medication_response_model.dart';
import 'package:doctor_app_flutter/core/viewModel/medicine_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/prescription/prescription_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/utils.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:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../widgets/dialog/confirm_dialog.dart';
import 'drug_to_drug.dart';

class AddDrugWidget extends StatefulWidget {
  final PatiantInformtion? patient;
  final List<PrescriptionModel>? prescriptionList;
  final MedicineViewModel? medicineModel;
  final PrescriptionViewModel? modelPrescription;
  final dynamic units;
  final dynamic route;
  final dynamic frequency;
  final dynamic duration;
  final dynamic doseTime;
  final dynamic uom;
  final dynamic x;
  final GetMedicationResponseModel? selectedMedication;
  final String strength;
  final String indication;
  final String instruction;
  final DateTime? selectedDate;

  const AddDrugWidget(
      {Key? key,
      this.patient,
      this.prescriptionList,
      this.medicineModel,
      this.modelPrescription,
      this.units,
      this.route,
      this.frequency,
      this.duration,
      this.doseTime,
      this.uom,
      this.x,
      this.selectedMedication,
      this.strength = '',
      this.indication = '',
      this.instruction = '',
      this.selectedDate})
      : super(key: key);

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

class _AddDrugWidgetState extends State<AddDrugWidget> {
  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      baseViewModel: widget.medicineModel!,
      appBar: BottomSheetTitle(title: TranslationBase.of(context).addMedication),
      body: Container(
        height: SizeConfig.realScreenHeight! * .8,
        width: SizeConfig.realScreenWidth!,
        child: SingleChildScrollView(
            child: Column(
          children: [
            DrugToDrug(widget.patient!, widget.medicineModel!.getPrescriptionForDrug(widget.prescriptionList!, widget.medicineModel!), widget.medicineModel!.patientAssessmentList),
          ],
        )),
      ),
      bottomSheet: widget.medicineModel!.state == ViewState.BusyLocal || widget.medicineModel!.state == ViewState.Busy
          ? Container(
              height: 0,
            )
          : CustomBottomSheetContainer(
              label: TranslationBase.of(context).addMedication,
              onTap: () async {
                showDialog(
                    context: context,
                    builder: (BuildContext context2) {
                      return ConfirmationDialog(
                          title: TranslationBase.of(context2).afterOrderCreation,
                          onTapGrant: () async {
                            GifLoaderDialogUtils.showMyDialog(context);
                            await widget.medicineModel!.postPrescription(
                              isLocalBusy: true,
                              icdCode: widget.medicineModel!
                                      .patientAssessmentList.isNotEmpty
                                  ? widget
                                          .medicineModel
                                          !.patientAssessmentList[0]
                                          .icdCode10ID!
                                          .isEmpty
                                      ? "test"
                                      : widget.medicineModel!
                                          .patientAssessmentList![0].icdCode10ID
                                          .toString()
                                  : "test",
                              dose: widget.strength,
                              doseUnit:
                                  widget.medicineModel!.itemMedicineListUnit
                                              .length ==
                                          1
                                      ? widget
                                          .medicineModel
                                          !.itemMedicineListUnit[0]
                                              ['parameterCode']
                                          .toString()
                                      : widget.units['parameterCode']
                                          .toString(),
                              patient: widget.patient,
                              doseTimeIn: widget.doseTime['id'].toString(),
                              model: widget.modelPrescription,
                              duration: ProjectViewModel.getVidaPlusStatus()? widget.duration.toString(): widget.duration['id'].toString(),
                              frequency: widget.medicineModel!.itemMedicineList
                                          .length ==
                                      1
                                  ? widget.medicineModel!
                                      .itemMedicineList![0]['parameterCode']
                                      .toString()
                                  : widget.frequency['parameterCode']
                                      .toString(),
                              route: widget.medicineModel!.itemMedicineListRoute
                                          .length ==
                                      1
                                  ? widget.medicineModel!
                                      .itemMedicineListRoute![0]['parameterCode']
                                      .toString()
                                  : widget.route['parameterCode'].toString(),
                              drugId:
                                  widget.selectedMedication!.itemId.toString(),
                              strength: widget.strength,
                              indication: widget.indication,
                              instruction: widget.instruction,
                              doseTime: widget.selectedDate,
                            );
                            if (widget.medicineModel!.state ==
                                ViewState.ErrorLocal) {
                              GifLoaderDialogUtils.hideDialog(context);
                              Utils.showErrorToast(widget.medicineModel!.error);
                            } else if (widget.medicineModel!.state ==
                                ViewState.Idle) {
                              await widget.modelPrescription
                                  !.getPrescriptionListNew(
                                      appNo: widget.patient!.appointmentNo,
                                      mrn: widget.patient!.patientMRN,
                                      episodeId: widget.patient!.episodeNo,
                                      isLocalBusy: true);
                              GifLoaderDialogUtils.hideDialog(context);
                              DrAppToastMsg.showSuccesToast(
                                  TranslationBase.of(context)
                                      .medicationHasBeenAdded);
                              Navigator.of(context).pop();
                              Navigator.of(context).pop();
                            }
                          });
                    });
              },
            ),
    );
  }
}
