import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/Prescriptions.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/in_patient_prescription_model.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/perscription_pharmacy.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_in_patient.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_report.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_report_enh.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescriptions_order.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/request_get_list_pharmacy_for_prescriptions.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/request_prescription_report.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/request_prescription_report_enh.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:flutter/cupertino.dart';

import 'base/base_service.dart';

class PrescriptionsService extends BaseService {
  List<Prescriptions> prescriptionsList = List();
  List<PrescriptionsOrder> prescriptionsOrderList = List();
  List<PrescriotionInPatient> prescriptionInPatientList = List();

  InPatientPrescriptionRequestModel _inPatientPrescriptionRequestModel =
      InPatientPrescriptionRequestModel();

  Future getPrescriptionInPatient({int mrn, String adn}) async {
    _inPatientPrescriptionRequestModel = InPatientPrescriptionRequestModel(
      patientMRN: mrn,
      admissionNo: adn,
    );
    hasError = false;
    prescriptionInPatientList.clear();

    await baseAppClient.post(GET_PRESCRIPTION_IN_PATIENT,
        onSuccess: (dynamic response, int statusCode) {
      prescriptionsList.clear();
      response['List_PrescriptionReportForInPatient'].forEach((prescriptions) {
        prescriptionInPatientList
            .add(PrescriotionInPatient.fromJson(prescriptions));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _inPatientPrescriptionRequestModel.toJson());
  }

  Future getPrescriptions(PatiantInformtion patient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.postPatient(PRESCRIPTIONS, patient: patient,
        onSuccess: (dynamic response, int statusCode) {
      prescriptionsList.clear();
      response['PatientPrescriptionList'].forEach((prescriptions) {
        prescriptionsList.add(Prescriptions.fromJson(prescriptions));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  RequestPrescriptionReport _requestPrescriptionReport =
      RequestPrescriptionReport(
          appointmentNo: 0, isDentalAllowedBackend: false);
  List<PrescriptionReport> prescriptionReportList = List();

  Future getPrescriptionReport(
      {Prescriptions prescriptions,
      @required PatiantInformtion patient}) async {
    hasError = false;
    _requestPrescriptionReport.dischargeNo = prescriptions.dischargeNo;
    _requestPrescriptionReport.projectID = prescriptions.projectID;
    _requestPrescriptionReport.clinicID = prescriptions.clinicID;
    _requestPrescriptionReport.setupID = prescriptions.setupID;
    _requestPrescriptionReport.episodeID = prescriptions.episodeID;
    _requestPrescriptionReport.appointmentNo = prescriptions.appointmentNo;

    await baseAppClient.postPatient(
        prescriptions.isInOutPatient
            ? GET_PRESCRIPTION_REPORT_ENH
            : GET_PRESCRIPTION_REPORT_NEW,
        patient: patient, onSuccess: (dynamic response, int statusCode) {
      prescriptionReportList.clear();
      prescriptionReportEnhList.clear();
      if (prescriptions.isInOutPatient) {
        response['ListPRM'].forEach((prescriptions) {
          prescriptionReportList
              .add(PrescriptionReport.fromJson(prescriptions));
          prescriptionReportEnhList
              .add(PrescriptionReportEnh.fromJson(prescriptions));
        });
      } else {
        response['INP_GetPrescriptionReport_List'].forEach((prescriptions) {
          prescriptionReportList
              .add(PrescriptionReport.fromJson(prescriptions));
        });
      }
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestPrescriptionReport.toJson());
  }

  RequestGetListPharmacyForPrescriptions
      requestGetListPharmacyForPrescriptions =
      RequestGetListPharmacyForPrescriptions(
    latitude: 0,
    longitude: 0,
    isDentalAllowedBackend: false,
  );
  List<PharmacyPrescriptions> pharmacyPrescriptionsList = List();

  Future getListPharmacyForPrescriptions(
      {int itemId, @required PatiantInformtion patient}) async {
    hasError = false;
    requestGetListPharmacyForPrescriptions.itemID = itemId;
    await baseAppClient.postPatient(GET_PHARMACY_LIST, patient: patient,
        onSuccess: (dynamic response, int statusCode) {
      pharmacyPrescriptionsList.clear();
      response['PharmList'].forEach((prescriptions) {
        pharmacyPrescriptionsList
            .add(PharmacyPrescriptions.fromJson(prescriptions));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: requestGetListPharmacyForPrescriptions.toJson());
  }

  RequestPrescriptionReportEnh _requestPrescriptionReportEnh =
      RequestPrescriptionReportEnh(
    isDentalAllowedBackend: false,
  );

  List<PrescriptionReportEnh> prescriptionReportEnhList = List();

  Future getPrescriptionReportEnh(
      {PrescriptionsOrder prescriptionsOrder,
      @required PatiantInformtion patient}) async {
    ///This logic copy from the old app from class [order-history.component.ts] in line 45
    bool isInPatient = false;
    prescriptionsList.forEach((element) {
      if (prescriptionsOrder.appointmentNo == "0") {
        if (element.dischargeNo == int.parse(prescriptionsOrder.dischargeID)) {
          _requestPrescriptionReportEnh.appointmentNo = element.appointmentNo;
          _requestPrescriptionReportEnh.clinicID = element.clinicID;
          _requestPrescriptionReportEnh.projectID = element.projectID;
          _requestPrescriptionReportEnh.episodeID = element.episodeID;
          _requestPrescriptionReportEnh.setupID = element.setupID;
          _requestPrescriptionReportEnh.dischargeNo = element.dischargeNo;
          isInPatient = element.isInOutPatient;
        }
      } else {
        if (int.parse(prescriptionsOrder.appointmentNo) ==
            element.appointmentNo) {
          _requestPrescriptionReportEnh.appointmentNo = element.appointmentNo;
          _requestPrescriptionReportEnh.clinicID = element.clinicID;
          _requestPrescriptionReportEnh.projectID = element.projectID;
          _requestPrescriptionReportEnh.episodeID = element.episodeID;
          _requestPrescriptionReportEnh.setupID = element.setupID;
          _requestPrescriptionReportEnh.dischargeNo = element.dischargeNo;
          isInPatient = element.isInOutPatient;

          ///call inpGetPrescriptionReport
        }
      }
    });

    hasError = false;

    await baseAppClient.postPatient(
        isInPatient ? GET_PRESCRIPTION_REPORT_ENH : GET_PRESCRIPTION_REPORT_NEW,
        patient: patient, onSuccess: (dynamic response, int statusCode) {
      prescriptionReportEnhList.clear();

      if (isInPatient) {
        response['ListPRM'].forEach((prescriptions) {
          prescriptionReportEnhList
              .add(PrescriptionReportEnh.fromJson(prescriptions));
        });
      } else {
        response['INP_GetPrescriptionReport_List'].forEach((prescriptions) {
          PrescriptionReportEnh reportEnh =
              PrescriptionReportEnh.fromJson(prescriptions);
          reportEnh.itemDescription = prescriptions['ItemDescriptionN'];
          prescriptionReportEnhList.add(reportEnh);
        });
      }
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestPrescriptionReportEnh.toJson());
  }

  Future getPrescriptionsOrders() async {
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_PRESCRIPTIONS_ALL_ORDERS,
        onSuccess: (dynamic response, int statusCode) {
      prescriptionsOrderList.clear();
      response['PatientER_GetPatientAllPresOrdersList']
          .forEach((prescriptionsOrder) {
        prescriptionsOrderList
            .add(PrescriptionsOrder.fromJson(prescriptionsOrder));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
