import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/radiology/final_radiology.dart';
import 'package:hmg_patient_app/core/model/radiology/request_send_rad_report_email.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart';

class RadiologyService extends BaseService {
  List<FinalRadiology> finalRadiologyList = [];
  String url = '';

  bool isRadiologyVIDAPlus = false;

  String radReportPDF = "";

  Future getRadImageURL({int? invoiceNo, String? invoiceType, int? lineItem, int? projectId, bool? isVidaPlus, String? examId}) async {
    hasError = false;
    final Map<String, dynamic> body = new Map<String, dynamic>();
    body['InvoiceNo'] = isVidaPlus! ? "0" : invoiceNo;
    body['InvoiceNo_VP'] = isVidaPlus ? invoiceNo : "0";
    body['LineItemNo'] = lineItem;
    body['ProjectID'] = projectId;
    body['InvoiceType'] = invoiceType;
    body['ExamId'] = examId;

    await baseAppClient.post(GET_RAD_IMAGE_URL, isAllowAny: true, onSuccess: (dynamic response, int statusCode) {
      url = response['Data'];
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getPatientRadOrders() async {
    hasError = false;
    await baseAppClient.post(GET_PATIENT_ORDERS, onSuccess: (dynamic response, int statusCode) {
      finalRadiologyList.clear();
      // response['FinalRadiologyList'].forEach((radiology) {
      if (response['FinalRadiologyList'] != null && response['FinalRadiologyList'].length != 0) {
        isRadiologyVIDAPlus = false;
        response['FinalRadiologyList'].forEach((radiology) {
          finalRadiologyList.add(FinalRadiology.fromJson(radiology));
        });
      } else {
        isRadiologyVIDAPlus = true;
        response['FinalRadiologyListAPI'].forEach((radiology) {
          finalRadiologyList.add(FinalRadiology.fromJson(radiology));
        });
      }
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  Future getPatientRadOrdersDetails() async {
    hasError = false;
    await baseAppClient.post(GET_PATIENT_ORDERS, onSuccess: (dynamic response, int statusCode) {
      finalRadiologyList.clear();
      response['FinalRadiologyList'].forEach((radiology) {
        finalRadiologyList.add(FinalRadiology.fromJson(radiology));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  RequestSendRadReportEmail _requestSendRadReportEmail = RequestSendRadReportEmail();

  Future sendRadReportEmail({FinalRadiology? finalRadiology, AuthenticatedUser? userObj, required bool isDownload}) async {
    _requestSendRadReportEmail.projectID = finalRadiology!.projectID;
    _requestSendRadReportEmail.clinicName = finalRadiology.clinicDescription;
    _requestSendRadReportEmail.invoiceNo = finalRadiology.invoiceNo;
    _requestSendRadReportEmail.invoiceNo_VP = finalRadiology.invoiceNo_VP;
    _requestSendRadReportEmail.invoiceLineItemNo = finalRadiology.invoiceLineItemNo;
    _requestSendRadReportEmail.setupID = finalRadiology.setupID;
    _requestSendRadReportEmail.doctorName = finalRadiology.doctorName;
    _requestSendRadReportEmail.orderDate = '${finalRadiology.orderDate!.year}-${finalRadiology.orderDate!.month}-${finalRadiology.orderDate!.day}';
    _requestSendRadReportEmail.patientIditificationNum = userObj!.patientIdentificationNo;
    _requestSendRadReportEmail.patientMobileNumber = userObj.mobileNumber;
    _requestSendRadReportEmail.patientName = userObj.firstName! + " " + userObj.lastName!;
    _requestSendRadReportEmail.projectName = finalRadiology.projectName;
    _requestSendRadReportEmail.radResult = finalRadiology.reportData;
    _requestSendRadReportEmail.to = userObj.emailAddress;
    _requestSendRadReportEmail.dateofBirth = userObj.dateofBirth;
    _requestSendRadReportEmail.invoiceType = finalRadiology.invoiceType;
    _requestSendRadReportEmail.isDownload = isDownload;

    hasError = false;
    await baseAppClient.post(SEND_RAD_REPORT_EMAIL, isAllowAny: true, onSuccess: (dynamic response, int statusCode) {
      if(isDownload) {
        radReportPDF = response["Base64Data"];
      }
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestSendRadReportEmail.toJson());
  }
}
