import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';
import 'package:doctor_app_flutter/core/model/patient/MedicalReport/MedicalReportTemplate.dart';
import 'package:doctor_app_flutter/core/model/patient/MedicalReport/MeidcalReportModel.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/extenstions_utils.dart';

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

  Future getMedicalReportList(PatiantInformtion patient, bool isVidaPlusProject) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    await getDoctorProfile(isGetProfile: true);
    body['AdmissionNo'] = isVidaPlusProject ?patient.admissionID : 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) {
          if (isVidaPlusProject) {
            medicalReportList.add(MedicalReportModel.fromJsonForVidaPlus(v));
          } else {
            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();


    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['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, bool? isVidaPlusProject,) async {
    hasError = false;
    print("the isVidaPlusProject flag in Service is $isVidaPlusProject");
    Map<String, dynamic> body = Map();
    if (isVidaPlusProject == true) {
      body['SetupID'] = doctorProfile!.setupID;
    } else
      body['SetupID'] = "91877";
    body['AdmissionNo'] = isVidaPlusProject == true? patient.admissionID : patient.admissionNo;
    body['InvoiceNo'] = medicalReport.invoiceNo;
    body['LineItemNo'] = medicalReport.lineItemNo;
    if (isVidaPlusProject == true) {
      body['TemplateID'] = medicalReport.templateID ?? 0;
      body['summaryId'] = medicalReport.summaryId ?? 0;
      try {
        if (body['InvoiceNo'].toString().isEmpty) {
          body['InvoiceNo'] = "0";
        }
      } catch (e) {
        body['InvoiceNo'] = "0";
      }
    }
    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, bool? isVidaPlusProject) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    if (isVidaPlusProject == false) {
      body['SetupID'] = body.containsKey('SetupID')
          ? body['SetupID'] != null
              ? body['SetupID']
              : SETUP_ID
          : SETUP_ID;
    }

    body['AdmissionNo'] = isVidaPlusProject == true? patient.admissionID ?? -1 :int.parse(patient.admissionNo!);

    body['MedicalReportHTML'] = htmlText;
      body['ProjectID'] = doctorProfile?.projectID;
      body['PatientID'] = patient.patientId;
      body['DoctorID'] = doctorProfile?.doctorID;
      if(isVidaPlusProject == true) {
      body['PatientType'] = patient.patientType;
    }

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

  Future updateMedicalReport(PatiantInformtion patient, String htmlText, int limitNumber, String invoiceNumber, int? summaryId, MedicalReportModel medicalReport) async {
    hasError = false;
    Map<String, dynamic> body = Map();

    body['LineItemNo'] = limitNumber;
    body['InvoiceNo'] = invoiceNumber;
    if (ProjectViewModel.getVidaPlusStatus()) {
      if (body['InvoiceNo'].toString().isEmpty) {
        body['InvoiceNo'] = "0";
      }
      body['SummaryID'] = summaryId ?? 0;
      body['TemplateID'] = medicalReport.templateID ?? 0;
      body['AdmissionNo'] = patient.admissionID ?? 0;
    }
    if (ProjectViewModel.getVidaPlusStatus()) {
      body['SetupID'] = doctorProfile!.setupID;
    } else
     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);
  }
}
