import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/radiology/final_radiology.dart';
import 'package:diplomaticquarterapp/core/model/radiology/request_send_rad_report_email.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';

class RadiologyService extends BaseService {
  List<FinalRadiology> finalRadiologyList = List();
  String url = '';

  Future getRadImageURL({int invoiceNo, int lineItem, int projectId}) async {
    hasError = false;
    final Map<String, dynamic> body = new Map<String, dynamic>();
    body['InvoiceNo'] = invoiceNo;
    body['LineItemNo'] = lineItem;
    body['ProjectID'] = projectId;

    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) {
        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}) async {
    _requestSendRadReportEmail.projectID = finalRadiology.projectID;
    _requestSendRadReportEmail.clinicName = finalRadiology.clinicDescription;
    _requestSendRadReportEmail.invoiceNo = finalRadiology.invoiceNo;
    _requestSendRadReportEmail.setupID = finalRadiology.setupID;
    _requestSendRadReportEmail.doctorName = finalRadiology.doctorName;
    _requestSendRadReportEmail.orderDate =
        '${finalRadiology.orderDate.year}-${finalRadiology.orderDate.month}-${finalRadiology.orderDate.day}';
    _requestSendRadReportEmail.patientIditificationNum =
        user.patientIdentificationNo;
    _requestSendRadReportEmail.patientMobileNumber = user.mobileNumber;
    _requestSendRadReportEmail.patientName =
        user.firstName + " " + user.lastName;
    _requestSendRadReportEmail.patientIditificationNum =
        user.patientIdentificationNo;
    _requestSendRadReportEmail.projectName = finalRadiology.projectName;
    _requestSendRadReportEmail.radResult = finalRadiology.reportData;
    _requestSendRadReportEmail.to = user.emailAddress;
    _requestSendRadReportEmail.dateofBirth = user.dateofBirth;

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