import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';
import 'package:doctor_app_flutter/models/patient/MedicalReport/MedicalReportTemplate.dart';
import 'package:doctor_app_flutter/models/patient/MedicalReport/MeidcalReportModel.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';

class PatientMedicalReportService extends BaseService {
  List<MedicalReportModel> medicalReportList = [];
  List<MedicalReportTemplate> medicalReportTemplate = [];

  Future getMedicalReportList(PatiantInformtion patient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    await getDoctorProfile();
    body['AdmissionNo'] = patient.admissionNo;
    body['SetupID'] = doctorProfile.setupID;
    body['ProjectID'] = doctorProfile.projectID;
    medicalReportList = [];
    await baseAppClient.postPatient(PATIENT_MEDICAL_REPORT_GET_LIST, onSuccess: (dynamic response, int statusCode) {
      if (response['DAPP_ListMedicalReportList'] != null) {
        response['DAPP_ListMedicalReportList'].forEach((v) {
          medicalReportList.add(MedicalReportModel.fromJson(v));
        });
      }
      medicalReportList = medicalReportList.reversed.toList();
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error.toString();
    }, body: body, patient: patient);
  }

  Future getMedicalReportTemplate() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['TokenID'] = "@dm!n";
    body['SetupID'] = "91877";
    body['TemplateID'] = 43;

    await baseAppClient.post(PATIENT_MEDICAL_REPORT_GET_TEMPLATE, onSuccess: (dynamic response, int statusCode) {
      medicalReportTemplate.clear();
      if (response['DAPP_GetTemplateByIDList'] != null) {
        response['DAPP_GetTemplateByIDList'].forEach((v) {
          medicalReportTemplate.add(MedicalReportTemplate.fromJson(v));
        });
      }
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error.toString();
    }, body: body);
  }

  Future insertMedicalReport(PatiantInformtion patient, String htmlText) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    // body['TokenID'] = "@dm!n";
    body['SetupID'] = "91877";
    body['AdmissionNo'] = patient.admissionNo;
    body['MedicalReportHTML'] = htmlText;

    await baseAppClient.postPatient(PATIENT_MEDICAL_REPORT_INSERT, onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error.toString();
    }, body: body, patient: patient);
  }

  Future verifyMedicalReport(PatiantInformtion patient, MedicalReportModel medicalReport) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['TokenID'] = "@dm!n";
    body['SetupID'] = "91877";
    body['AdmissionNo'] = patient.admissionNo;
    body['InvoiceNo'] = medicalReport.invoiceNo;
    body['LineItemNo'] = medicalReport.lineItemNo;
    if (body['ProjectID'] == null) {
      body['ProjectID'] = doctorProfile?.projectID;
    }
    if (body['DoctorID'] == null) body['DoctorID'] = doctorProfile?.doctorID;

    await baseAppClient.postPatient(PATIENT_MEDICAL_REPORT_VERIFIED, onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error.toString();
    }, body: body, patient: patient);
  }

  Future addMedicalReport(PatiantInformtion patient, String htmlText) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    // body['TokenID'] = "@dm!n";
    body['SetupID'] = body.containsKey('SetupID')
        ? body['SetupID'] != null
            ? body['SetupID']
            : SETUP_ID
        : SETUP_ID;
    body['AdmissionNo'] = int.parse(patient.admissionNo);
    body['MedicalReportHTML'] = htmlText;
    if (body['ProjectID'] == null) {
      body['ProjectID'] = doctorProfile?.projectID;
    }
    if (body['DoctorID'] == null) body['DoctorID'] = doctorProfile?.doctorID;

    await baseAppClient.postPatient(INSERT_MEDICAL_REPORT, onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error.toString();
    }, body: body, patient: patient);
  }

  Future updateMedicalReport(PatiantInformtion patient, String htmlText, int limitNumber, String invoiceNumber) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    // body['TokenID'] = "@dm!n";

    body['LineItemNo'] = limitNumber;
    body['InvoiceNo'] = invoiceNumber;

    body['SetupID'] = body.containsKey('SetupID')
        ? body['SetupID'] != null
            ? body['SetupID']
            : SETUP_ID
        : SETUP_ID;
    //body['AdmissionNo'] = int.parse(patient.admissionNo);
    body['MedicalReportHTML'] = htmlText;
    if (body['ProjectID'] == null) {
      body['ProjectID'] = doctorProfile?.projectID;
    }
    if (body['DoctorID'] == null) body['DoctorID'] = doctorProfile?.doctorID;

    await baseAppClient.postPatient(UPDATE_MEDICAL_REPORT, onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error.toString();
    }, body: body, patient: patient);
  }
}
