import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/radiology/final_radiology.dart';

import '../../base/base_service.dart';

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

  Future getRadImageURL({required invoiceNo, required int lineItem, required int projectId, bool isVidaPlus = false, required PatiantInformtion patient, String? invoiceType}) 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;
    await baseAppClient.postPatient(GET_RAD_IMAGE_URL, patient: patient, onSuccess: (dynamic response, int statusCode) {
      url = response['Data'];
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getPatientRadOrders(PatiantInformtion patient, {isInPatient = false}) async {
    String url = GET_PATIENT_ORDERS;
    final Map<String, dynamic> body = new Map<String, dynamic>();
    if (isInPatient) {
      url = GET_IN_PATIENT_ORDERS;
      body['ProjectID'] = patient.projectId;
    }else{
      body['ProjectID'] = 0; // ADDED AS PER BACKEND REQUEST

    }

    hasError = false;
    await baseAppClient.postPatient(url, patient: patient, onSuccess: (dynamic response, int statusCode) {
      finalRadiologyList = [];
      String label = "ListRAD";
      if (isInPatient) {
        label = "List_GetRadOreders";
      }
      if (response['FinalRadiologyList'] != null && response['FinalRadiologyList'].length != 0) {
        isRadiologyVIDAPlus = false;
        response['FinalRadiologyList'].forEach((radiology) {
          finalRadiologyList.add(FinalRadiology.fromJson(radiology));
        });
      } else {
        isRadiologyVIDAPlus = true;
        if (response['FinalRadiologyListAPI'] != null) {
          response['FinalRadiologyListAPI'].forEach((radiology) {
            finalRadiologyList.add(FinalRadiology.fromJson(radiology));
          });
        }
      }
      // if (response[label] == null || response[label].length == 0) {
      //   label = "FinalRadiologyList";
      // }
      // response[label].forEach((radiology) {
      //   finalRadiologyList.add(FinalRadiology.fromJson(radiology));
      // });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
