import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/labs/LabOrderResult.dart';
import 'package:doctor_app_flutter/core/model/labs/lab_result.dart';
import 'package:doctor_app_flutter/core/model/labs/patient_lab_orders.dart';
import 'package:doctor_app_flutter/core/model/labs/patient_lab_special_result.dart';
import 'package:doctor_app_flutter/core/model/labs/request_patient_lab_special_result.dart';
import 'package:doctor_app_flutter/core/model/labs/request_send_lab_report_email.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';

import 'base/base_service.dart';

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

  Future getPatientLabOrdersList(
      PatiantInformtion patient, bool isInpatient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    String url = "";
    if (isInpatient) {
      await getDoctorProfile();
      body['ProjectID'] = doctorProfile.projectID;
      url = GET_PATIENT_LAB_OREDERS;
    } else {
      body['isDentalAllowedBackend'] = false;
      url = GET_Patient_LAB_ORDERS;
    }

    await baseAppClient.postPatient(url, patient: patient,
        onSuccess: (dynamic response, int statusCode) {
      patientLabOrdersList=[];
      if (!isInpatient) {
        response['ListPLO'].forEach((hospital) {
          patientLabOrdersList.add(PatientLabOrders.fromJson(hospital));
        });
      } else {
        response['List_GetLabOreders'].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,
      PatiantInformtion patient,
      bool isInpatient = false}) async {
    hasError = false;

    Map<String, dynamic> body = Map();
    _requestPatientLabSpecialResult.projectID = projectID;
    _requestPatientLabSpecialResult.clinicID = clinicID;
    _requestPatientLabSpecialResult.invoiceNo = invoiceNo;
    _requestPatientLabSpecialResult.orderNo = orderNo;
    body = _requestPatientLabSpecialResult.toJson();

    await baseAppClient.postPatient(GET_Patient_LAB_SPECIAL_RESULT, patient: patient,
        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: body);
  }

  Future getPatientLabResult(
      {PatientLabOrders patientLabOrder, PatiantInformtion patient, bool isInpatient}) async {
    hasError = false;

    String url = "";
    if (isInpatient) {
      url = GET_PATIENT_LAB_RESULTS;
    } else {
      url = GET_Patient_LAB_RESULT;
    }

    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 ?? 0;

    await baseAppClient.postPatient(url, patient: patient,
        onSuccess: (dynamic response, int statusCode) {
      patientLabSpecialResult.clear();
      labResultList.clear();

      if(isInpatient){
        response['List_GetLabNormal'].forEach((hospital) {
          labResultList.add(LabResult.fromJson(hospital));
        });
      }else {
        response['ListPLR'].forEach((lab) {
          labResultList.add(LabResult.fromJson(lab));
        });
      }

    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getPatientLabOrdersResults(
      {PatientLabOrders patientLabOrder,
      String procedure,
      PatiantInformtion patient}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    if (patientLabOrder != null) {
      body['InvoiceNo'] = patientLabOrder.invoiceNo;
      body['OrderNo'] = patientLabOrder.orderNo;
      body['SetupID'] = patientLabOrder.setupID;
      body['ProjectID'] = patientLabOrder.projectID;
      body['ClinicID'] = patientLabOrder.clinicID ?? 0;
    }
    body['isDentalAllowedBackend'] = false;
    body['Procedure'] = procedure;
    await baseAppClient.postPatient(GET_Patient_LAB_ORDERS_RESULT,
        patient: patient, 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}) async {
    //  _requestSendLabReportEmail.projectID = patientLabOrder.projectID;
    //  _requestSendLabReportEmail.invoiceNo = patientLabOrder.invoiceNo;
    //  _requestSendLabReportEmail.doctorName = patientLabOrder.doctorName;
    //  _requestSendLabReportEmail.clinicName = patientLabOrder.clinicDescription;
    //  _requestSendLabReportEmail.patientName = user.firstName +" "+ user.lastName;
    //  _requestSendLabReportEmail.patientIditificationNum =
    //      user.patientIdentificationNo;
    //  _requestSendLabReportEmail.dateofBirth = user.dateofBirth;
    //  _requestSendLabReportEmail.to = user.emailAddress;
    //  _requestSendLabReportEmail.orderDate = '${patientLabOrder.orderDate.year}-${patientLabOrder.orderDate.month}-${patientLabOrder.orderDate.day}';
    //  _requestSendLabReportEmail.patientMobileNumber = user.mobileNumber;
    //  _requestSendLabReportEmail.projectName = patientLabOrder.projectName;
    // _requestSendLabReportEmail.setupID = user.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());
  }
}
