import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/labs/LabOrderResult.dart';
import 'package:diplomaticquarterapp/core/model/labs/lab_result.dart';
import 'package:diplomaticquarterapp/core/model/labs/patient_lab_orders.dart';
import 'package:diplomaticquarterapp/core/model/labs/patient_lab_special_result.dart';
import 'package:diplomaticquarterapp/core/model/labs/request_patient_lab_special_result.dart';
import 'package:diplomaticquarterapp/core/model/labs/request_send_lab_report_email.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';

class LabsService extends BaseService {
  List<PatientLabOrders> patientLabOrdersList = List();

  Future getPatientLabOrdersList() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_Patient_LAB_ORDERS, onSuccess: (dynamic response, int statusCode) {
      patientLabOrdersList.clear();
      response['ListPLO'].forEach((hospital) {
        patientLabOrdersList.add(PatientLabOrders.fromJson(hospital));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  RequestPatientLabSpecialResult _requestPatientLabSpecialResult = RequestPatientLabSpecialResult();

  List<PatientLabSpecialResult> patientLabSpecialResult = List();
  List<LabResult> labResultList = List();
  List<LabOrderResult> labOrdersResultsList = List();

  Future getLaboratoryResult({String projectID, int clinicID, String invoiceNo, String orderNo, String setupID}) async {
    hasError = false;
    _requestPatientLabSpecialResult.projectID = projectID;
    _requestPatientLabSpecialResult.clinicID = clinicID;
    _requestPatientLabSpecialResult.invoiceNo = invoiceNo;
    _requestPatientLabSpecialResult.orderNo = orderNo;
    _requestPatientLabSpecialResult.setupID = setupID;

    await baseAppClient.post(GET_Patient_LAB_SPECIAL_RESULT, onSuccess: (dynamic response, int statusCode) {
      patientLabSpecialResult.clear();
      response['ListPLSR'].forEach((hospital) {
        patientLabSpecialResult.add(PatientLabSpecialResult.fromJson(hospital));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestPatientLabSpecialResult.toJson());
  }

  Future getPatientLabResult({PatientLabOrders patientLabOrder}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['InvoiceNo'] = patientLabOrder.invoiceNo;
    body['OrderNo'] = patientLabOrder.orderNo;
    body['isDentalAllowedBackend'] = false;
    body['SetupID'] = patientLabOrder.setupID;
    body['ProjectID'] = patientLabOrder.projectID;
    body['ClinicID'] = patientLabOrder.clinicID;
    await baseAppClient.post(GET_Patient_LAB_RESULT, onSuccess: (dynamic response, int statusCode) {
      patientLabSpecialResult.clear();
      labResultList.clear();
      response['ListPLR'].forEach((lab) {
        labResultList.add(LabResult.fromJson(lab));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future generateCovidLabReport(LabResult covidLabResult, String isOutsideKSA) async {
    hasError = false;
    Map<String, dynamic> body = Map();

    body['To'] = user.emailAddress;
    body['OrderNo'] = covidLabResult.orderNo;
    body['OrderLineItemNo'] = covidLabResult.orderLineItemNo;
    body['LineItemNo'] = covidLabResult.resultValueBasedLineItemNo;
    body['CertificateFormat'] = 5;
    body['GeneratedBy'] = 102;
    body['ShowPassportNumber'] = isOutsideKSA;
    body['isDentalAllowedBackend'] = false;
    body['SetupID'] = covidLabResult.setupID;
    body['ProjectID'] = covidLabResult.projectID;

    dynamic localRes;

    await baseAppClient.post(SEND_COVID_LAB_RESULT_EMAIL, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

  Future updateCovidPassportNumber(String passportNumber) async {
    hasError = false;
    Map<String, dynamic> body = Map();

    body['PassportNo'] = passportNumber;

    dynamic localRes;

    await baseAppClient.post(COVID_PASSPORT_UPDATE, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

  Future updateWorkplaceName(String workplaceName, int requestNumber, String setupID, int projectID) async {
    hasError = false;
    Map<String, dynamic> body = Map();

    body['Placeofwork'] = workplaceName;
    body['Placeofworkar'] = workplaceName;
    body['Req_ID'] = requestNumber;
    body['TargetSetupID'] = setupID;
    body['ProjectID'] = projectID;
    body['UserID'] = 102;
    body['RequestType'] = 1;

    dynamic localRes;

    await baseAppClient.post(UPDATE_WORKPLACE_NAME, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

  Future getCovidPassportNumber() async {
    hasError = false;
    Map<String, dynamic> body = Map();

    dynamic localRes;

    await baseAppClient.post(GET_PATIENT_PASSPORT_NUMBER, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

  Future getPatientLabOrdersResults({PatientLabOrders patientLabOrder, String procedure}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['InvoiceNo'] = patientLabOrder.invoiceNo;
    body['OrderNo'] = patientLabOrder.orderNo;
    body['isDentalAllowedBackend'] = false;
    body['SetupID'] = patientLabOrder.setupID;
    body['ProjectID'] = patientLabOrder.projectID;
    body['ClinicID'] = patientLabOrder.clinicID;
    body['Procedure'] = procedure;
    await baseAppClient.post(GET_Patient_LAB_ORDERS_RESULT, onSuccess: (dynamic response, int statusCode) {
      labOrdersResultsList.clear();
      response['ListPLR'].forEach((lab) {
        labOrdersResultsList.add(LabOrderResult.fromJson(lab));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  RequestSendLabReportEmail _requestSendLabReportEmail = RequestSendLabReportEmail();

  Future sendLabReportEmail({PatientLabOrders patientLabOrder, AuthenticatedUser userObj}) async {
    _requestSendLabReportEmail.projectID = patientLabOrder.projectID;
    _requestSendLabReportEmail.invoiceNo = patientLabOrder.invoiceNo;
    _requestSendLabReportEmail.doctorName = patientLabOrder.doctorName;
    _requestSendLabReportEmail.clinicName = patientLabOrder.clinicDescription;
    _requestSendLabReportEmail.patientName = userObj.firstName + " " + userObj.lastName;
    _requestSendLabReportEmail.patientIditificationNum = userObj.patientIdentificationNo;
    _requestSendLabReportEmail.dateofBirth = userObj.dateofBirth;
    _requestSendLabReportEmail.to = userObj.emailAddress;
    _requestSendLabReportEmail.orderDate = '${patientLabOrder.orderDate.year}-${patientLabOrder.orderDate.month}-${patientLabOrder.orderDate.day}';
    _requestSendLabReportEmail.patientMobileNumber = userObj.mobileNumber;
    _requestSendLabReportEmail.projectName = patientLabOrder.projectName;
    _requestSendLabReportEmail.setupID = patientLabOrder.setupID;

    await baseAppClient.post(SEND_LAB_RESULT_EMAIL, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestSendLabReportEmail.toJson());
  }
}
