import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/reports/Reports.dart';
import 'package:hmg_patient_app/core/model/reports/admission_for_medical_report.dart';
import 'package:hmg_patient_app/core/model/reports/request_reports.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/pages/feedback/appointment_history.dart';

class ReportsService extends BaseService {
  List<Reports> reportsList = [];
  List<Reports> inpatientReportsList =[];
  List<AppointmentHistory> appointHistoryList =[];

  List<AdmissionMedicalReport> admissionsMedicalReport = [];

  String userAgreementContent = "";
  RequestReports _requestReports = RequestReports(isReport: true, encounterType: 1, requestType: 1, projectID: 0);

  Future getReports() async {
    hasError = false;
    await baseAppClient.post(REPORTS, onSuccess: (dynamic response, int statusCode) {
      reportsList.clear();
      response['GetPatientMedicalStatus'].forEach((reports) {
        reportsList.add(Reports.fromJson(reports));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestReports.toJson());
  }

  Future getInPatientReports() async {
    RequestReports _requestReportsInpatient = RequestReports(isReport: true, encounterType: 2, requestType: 1, patientOutSA: 0, projectID: 0);
    hasError = false;
    await baseAppClient.post(REPORTS, onSuccess: (dynamic response, int statusCode) {
      inpatientReportsList.clear();
      response['GetPatientMedicalStatus'].forEach((reports) {
        inpatientReportsList.add(Reports.fromJson(reports));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestReportsInpatient.toJson());
  }

  Future getInpatientAdmissionsList(int languageID) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['IsForMedicalReport'] = true;
    body['LanguageID'] = languageID;
    hasError = false;
    await baseAppClient.post(GET_INPATIENT_ADMISSIONS, onSuccess: (dynamic response, int statusCode) {
      admissionsMedicalReport.clear();
      print(response['AdmissionsForMedicalReport']);
      response['AdmissionsForMedicalReport'].forEach((reports) {
        admissionsMedicalReport.add(AdmissionMedicalReport.fromJson(reports));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    },  body: body);
  }

  Future getPatentAppointmentHistory() async {
    hasError = false;
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['IsForMedicalReport'] = true;
    await baseAppClient.post(GET_PATIENT_AppointmentHistory, onSuccess: (dynamic response, int statusCode) {
      appointHistoryList = [];
      response['AppoimentAllHistoryResultList'].forEach((appoint) {
        appointHistoryList.add(AppointmentHistory.fromJson(appoint));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getUserTermsAndConditions() async {
    hasError = false;
    await baseAppClient.post(GET_USER_TERMS, onSuccess: (dynamic response, int statusCode) {
      userAgreementContent = response['UserAgreementContent'];
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map<String, dynamic>());
  }

  Future updatePatientHealthSummaryReport({required bool isSummary}) async {
    Map<String, dynamic> body = Map<String, dynamic>();
    body['RSummaryReport'] = isSummary;
    hasError = false;
    await baseAppClient.post(UPDATE_HEALTH_TERMS, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future updateEmail({required String email}) async {
    Map<String, dynamic> body = Map<String, dynamic>();
    body['EmailAddress'] = email;
    body['isDentalAllowedBackend'] = false;
    hasError = false;
    await baseAppClient.post(UPDATE_PATENT_EMAIL, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future insertRequestForMedicalReport(AppointmentHistory appointmentHistory) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['ClinicID'] = appointmentHistory.clinicID;
    body['DoctorID'] = appointmentHistory.doctorID;
    body['SetupID'] = appointmentHistory.setupID;
    body['EncounterNo'] = appointmentHistory.appointmentNo;
    body['EncounterType'] = 1; // appointmentHistory.appointmentType;
    body['IsActive'] = appointmentHistory.isActiveDoctor;
    body['ProjectID'] = appointmentHistory.projectID;
    body['Remarks'] = "";
    body['ProcedureId'] = "";
    body['RequestType'] = 1;
    body['Source'] = 2;
    body['Status'] = 1;
    body['CreatedBy'] = 102;
    hasError = false;
    await baseAppClient.post(INSERT_REQUEST_FOR_MEDICAL_REPORT, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future insertRequestForInPatientMedicalReport(int clinicID, int doctorID, String setupID, int admissionNo, int projectID) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['ClinicID'] = clinicID;
    body['DoctorID'] = doctorID;
    body['SetupID'] = setupID;
    body['EncounterNo'] = admissionNo;
    body['EncounterType'] = 2; // appointmentHistory.appointmentType;
    body['IsActive'] = true;
    body['ProjectID'] = projectID;
    body['Remarks'] = "";
    body['ProcedureId'] = "";
    body['RequestType'] = 1;
    body['Source'] = 2;
    body['Status'] = 1;
    body['CreatedBy'] = 102;
    hasError = false;
    await baseAppClient.post(INSERT_REQUEST_FOR_MEDICAL_REPORT, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future sendEmailForMedicalReport(String projectName, String clinicName, String doctorName, String requestDate, String invoiceNo, int projectID, String stamp, String setupID) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['SetupID'] = setupID;
    body['PrintDate'] = requestDate;
    body['ProcedureID'] = "05005009";
    body['Reporttype'] = "MEDICAL REPORT";
    body['stamp'] = stamp;
    body['To'] = user.emailAddress;
    body['DateofBirth'] = user.dateofBirth;
    body['PatientIditificationNum'] = user.patientIdentificationNo;
    body['PatientMobileNumber'] = user.mobileNumber;
    body['PatientName'] = user.firstName! + " " + user.lastName!;
    body['ProjectName'] = projectName;
    body['ClinicName'] = clinicName;
    body['ProjectID'] = projectID;
    body['InvoiceNo'] = invoiceNo;
    body['PrintedByName'] = user.firstName! + " " + user.lastName!;

    dynamic response;

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

  Future getMedicalReportPDF(String projectName, String clinicName, String doctorName, String requestDate, String invoiceNo, int projectID, String stamp, String setupID) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['SetupID'] = setupID;
    body['PrintDate'] = requestDate;
    body['ProcedureID'] = "05005009";
    body['Reporttype'] = "MEDICAL REPORT";
    body['stamp'] = stamp;
    body['To'] = user.emailAddress;
    body['DateofBirth'] = user.dateofBirth;
    body['PatientIditificationNum'] = user.patientIdentificationNo;
    body['PatientMobileNumber'] = user.mobileNumber;
    body['PatientName'] = user.firstName! + " " + user.lastName!;
    body['ProjectName'] = projectName;
    body['ClinicName'] = clinicName;
    body['ProjectID'] = projectID;
    body['InvoiceNo'] = invoiceNo;
    body['InvoiceNo_VP'] = invoiceNo;
    body['PrintedByName'] = user.firstName! + " " + user.lastName!;

    dynamic response;

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