import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/Prescriptions.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/perscription_pharmacy.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report_enh.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescriptions_order.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/request_get_list_pharmacy_for_prescriptions.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/request_prescription_report.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/request_prescription_report_enh.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/request_prescriptions.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/request_prescriptions_orders.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/request_send_prescription_email.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';

class PrescriptionsService extends BaseService {
  List<Prescriptions> prescriptionsList = List();
  RequestPrescriptions _requestPrescriptions = RequestPrescriptions(
      versionID: 5.5,
      channel: 3,
      languageID: 2,
      iPAdress: '10.20.10.20',
      generalid: 'Cs2020@2016\$2958',
      patientOutSA: 0,
      sessionID: 'KIbLoqkytuKJEWECHQ',
      isDentalAllowedBackend: false,
      deviceTypeID: 2,
      patientID: 1231755,
      tokenID: '@dm!n',
      patientTypeID: 1,
      patientType: 1);

  List<PrescriptionsOrder> prescriptionsOrderList = List();
  RequestPrescriptionsOrders _requestPrescriptionsOrders =
      RequestPrescriptionsOrders(
          patientID: 1231755,
          patientOutSA: 0,
          versionID: 5.5,
          channel: 3,
          languageID: 1,
          iPAdress: '10.20.10.20',
          generalid: 'Cs2020@2016\$2958',
          sessionID: 'KIbLoqkytuKJEWECHQ',
          isDentalAllowedBackend: false,
          deviceTypeID: 2);

  Future getPrescriptions() async {
    hasError = false;
    await baseAppClient.post(PRESCRIPTIONS,
        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: _requestPrescriptions.toJson());
  }

  Future getPrescriptionsOrders() async {
    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: _requestPrescriptionsOrders.toJson());
  }

  RequestPrescriptionReport _requestPrescriptionReport =
      RequestPrescriptionReport(
          appointmentNo: 0,
          channel: 3,
          clinicID: 4,
          deviceTypeID: 2,
          dischargeNo: 2018003246,
          episodeID: 0,
          iPAdress: '10.20.10.20',
          languageID: 1,
          patientID: 1231755,
          patientOutSA: 0,
          patientType: 1,
          patientTypeID: 1,
          projectID: 12,
          sessionID: 'wgKuHpsPsEuLnlJhAYCQ',
          tokenID: '@dm!n',
          setupID: "91877",
          versionID: 5.5,
          generalid: 'Cs2020@2016\$2958',
          isDentalAllowedBackend: false);
  List<PrescriptionReport> prescriptionReportList = List();

  Future getPrescriptionReport({int dischargeNo}) async {
    hasError = false;
    _requestPrescriptionReport.dischargeNo = dischargeNo;
    await baseAppClient.post(GET_PRESCRIPTION_REPORT,
        onSuccess: (dynamic response, int statusCode) {
      prescriptionReportList.clear();
      response['INP_GetPrescriptionReport_List'].forEach((prescriptions) {
        prescriptionReportList.add(PrescriptionReport.fromJson(prescriptions));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestPrescriptionReport.toJson());
  }

  RequestSendPrescriptionEmail _requestSendPrescriptionEmail =
      RequestSendPrescriptionEmail(
          versionID: 5.5,
          languageID: 2,
          channel: 3,
          iPAdress: '10.20.10.20',
          generalid: 'Cs2020@2016\$2958',
          patientOutSA: 0,
          sessionID: 'twIUmHfOHqFdDfVcyw',
          isDentalAllowedBackend: false,
          deviceTypeID: 2,
          tokenID: '@dm!n',
          patientTypeID: 1,
          patientType: 1,
          to: 'aljammalzmohammad@outlook.com',
          dateofBirth: '/Date(536743800000+0300)/',
          patientIditificationNum: '2344670985',
          patientMobileNumber: '537503378',
          patientName: 'TAMER FANASHEH',
          setupID: '91877');

  Future sendPrescriptionEmail(String appointmentDate, int patientID,
      String clinicName, String doctorName, int projectID) async {
    _requestSendPrescriptionEmail.listPrescriptions = prescriptionReportList;
    _requestSendPrescriptionEmail.appointmentDate = appointmentDate;
    _requestSendPrescriptionEmail.patientID = patientID;
    _requestSendPrescriptionEmail.clinicName = clinicName;
    _requestSendPrescriptionEmail.doctorName = doctorName;
    _requestSendPrescriptionEmail.projectID = projectID;
    hasError = false;
    await baseAppClient.post(SEND_PRESCRIPTION_EMAIL,
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestSendPrescriptionEmail.toJson());
  }

  RequestGetListPharmacyForPrescriptions
      requestGetListPharmacyForPrescriptions =
      RequestGetListPharmacyForPrescriptions(
    latitude: 0,
    longitude: 0,
    versionID: 5.5,
    channel: 3,
    languageID: 2,
    iPAdress: '10.20.10.20',
    generalid: 'Cs2020@2016\$2958',
    patientOutSA: 0,
    sessionID: 'HGNerTUSXhpaHXBg',
    isDentalAllowedBackend: false,
    deviceTypeID: 2,
  );
  List<PharmacyPrescriptions> pharmacyPrescriptionsList = List();

  Future getListPharmacyForPrescriptions({int itemId}) async {
    hasError = false;
    requestGetListPharmacyForPrescriptions.itemID = itemId;
    await baseAppClient.post(GET_PHARMACY_LIST,
        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(
    versionID: 5.5,
    channel: 3,
    languageID: 2,
    iPAdress: '10.20.10.20',
    generalid: 'Cs2020@2016\$2958',
    patientOutSA: 0,
    sessionID: 'bQQdesEKpyYKTFMVNeg',
    isDentalAllowedBackend: false,
    deviceTypeID: 2,
    patientID: 1231755,
    tokenID: '@dm!n',
    patientTypeID: 1,
    patientType: 1,
    setupID: '91877',
    appointmentNo: 5926390,
    episodeID: 140251928,
    clinicID: 25,
    projectID: 12

  );

  List<PrescriptionReportEnh> prescriptionReportEnhList = List();
  Future getPrescriptionReportEnh({PrescriptionsOrder prescriptionsOrder})async{
    hasError = false;
   // _requestPrescriptionReportEnh.appointmentNo = int.parse(prescriptionsOrder.appointmentNo);
   // _requestPrescriptionReportEnh.patientID = prescriptionsOrder.projectID;

    await baseAppClient.post(GET_PRESCRIPTION_REPORT_ENH,
        onSuccess: (dynamic response, int statusCode) {
          prescriptionReportEnhList.clear();
          response['ListPRM'].forEach((prescriptions) {
            prescriptionReportEnhList.add(PrescriptionReportEnh.fromJson(prescriptions));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: _requestPrescriptionReportEnh.toJson());
  }
}
