package ai.causalfoundry.rnsdk.chw_mgmt;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogChwModuleEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogCounselingEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogInvestigationEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogPrescriptionEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogSubmitAssessmentEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogSubmitEnrolmentEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogSubmitMedicalReviewEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogSubmitScreeningEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.builders.CfLogTreatmentPlanEvent;
import ai.causalfoundry.android.sdk.chw_mgmt.catalog.CfChwCatalog;


public class CfLogChwMgmt extends ReactContextBaseJavaModule {

    private final ReactApplicationContext reactContext;

    public CfLogChwMgmt(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "CfLogChwMgmt";
    }


    /**
     * logChwModuleEvent is to log the selection of the module selected by the user in the app.
     *
     * @param chwModuleType     the name of the module user is selecting.
     * @param meta              is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately is default set to true, you can use that to log events when the app
     *                          goes in the background or closed.
     */
    @ReactMethod
    public void logChwModuleEvent(String chwModuleType, String meta, Boolean updateImmediately) {
        new CfLogChwModuleEvent.Builder()
                .setChwModuleEvent(chwModuleType)
                .setMeta(meta)
                .updateImmediately(updateImmediately)
                .build();
    }

    /**
     * logSubmitScreeningEvent is to log the submission of screening event for the patient.
     *
     * @param patientId                  patient id in the system.
     * @param siteId                     site id in the system.
     * @param siteCategory               category for the site of screening in the system.
     * @param screeningType              type for the screening.
     * @param diagnosisVitalsList        list of the diagnosis value provided by the user for recording patient vitals.
     * @param diagnosisSymptomsList      list of the diagnosis Symptoms value provided by the user.
     * @param diagnosisQuestionnaireList list of the diagnosis Questionnaire value provided by the user.
     * @param diagnosisValueList         list of the diagnosis value provided by the user.
     * @param diagnosisResultList        list of the diagnosis result provided by the user.
     * @param pregnancyDetailObject      object for pregnancy diagnosis result provided by the user.
     * @param isReferredForAssessment    is the user referred for further assessment.
     * @param meta                       is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately          is default set to true, you can use that to log events when the app
     *                                   goes in the background or closed.
     */
    @ReactMethod
    public void logSubmitScreeningEvent(String patientId, String siteId,
                                        String siteCategory, String screeningType,
                                        String diagnosisVitalsList, String diagnosisSymptomsList,
                                        String diagnosisQuestionnaireList,
                                        String diagnosisValueList, String diagnosisResultList,
                                        String pregnancyDetailObject,
                                        Boolean isReferredForAssessment,
                                        String meta, Boolean updateImmediately) {

        CfLogSubmitScreeningEvent.Builder cfLogSubmitScreeningEvent =  new CfLogSubmitScreeningEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setSiteCategory(siteCategory)
                .setScreeningType(screeningType)
                .setDiagnosisVitalsList(diagnosisVitalsList)
                .setDiagnosisValueList(diagnosisValueList)
                .setDiagnosisResultList(diagnosisResultList)
                .isReferredForAssessment(isReferredForAssessment)
                .setMeta(meta)
                .updateImmediately(updateImmediately);

        if(diagnosisSymptomsList!= null && !diagnosisSymptomsList.isEmpty()){
            cfLogSubmitScreeningEvent.setDiagnosisSymptomList(diagnosisSymptomsList);
        }
        if(diagnosisQuestionnaireList != null && !diagnosisQuestionnaireList.isEmpty()){
            cfLogSubmitScreeningEvent.setDiagnosisQuestionnaireList(diagnosisQuestionnaireList);
        }
        if(pregnancyDetailObject != null && !pregnancyDetailObject.isEmpty()){
            cfLogSubmitScreeningEvent.setPregnancyObject(pregnancyDetailObject);
        }
        cfLogSubmitScreeningEvent.build();

    }

    /**
     * logSubmitAssessmentEvent is to submit the assessment report for the patient. It requires
     * some values as required by the logs to segregate the patient category.
     *
     * @param patientId                     patient id in the system.
     * @param siteId                        site id in the system.
     * @param medicalAdherence              the string about patient's adherence to medical treatment.
     * @param diagnosisVitalsList           list of the diagnosis value provided by the user for recording patient vitals.
     * @param diagnosisQuestionnaireList    list of the diagnosis Questionnaire value provided by the user.
     * @param diagnosisValueList            list of the diagnosis values provided by the user.
     * @param diagnosisResultList           list of the diagnosis results provided by the user.
     * @param diagnosisSymptomList          list of the diagnosis symptoms provided by the user.
     * @param isReferredForAssessment       is the user referred for further assessment.
     * @param pregnancyDetails         object for pregnancy diagnosis result provided by the user.
     * @param meta                          is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately             is default set to true, you can use that to log events when the app
     *                                      goes in the background or closed.
     */
    @ReactMethod
    public void logSubmitAssessmentEvent(String patientId, String siteId,
                                         String category, String type,
                                         String medicalAdherence,
                                         String diagnosisVitalsList, String diagnosisQuestionnaireList,
                                         String diagnosisValueList, String diagnosisResultList,
                                         String diagnosisSymptomList,
                                         String pregnancyDetails,
                                         Boolean isReferredForAssessment,
                                         String meta, Boolean updateImmediately) {

        CfLogSubmitAssessmentEvent.Builder cfLogSubmitAssessmentEvent = new CfLogSubmitAssessmentEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setDiagnosisVitalsList(diagnosisVitalsList)
                .setDiagnosisValueList(diagnosisValueList)
                .setDiagnosisResultList(diagnosisResultList)
                .setDiagnosisSymptomList(diagnosisSymptomList)
                .isReferredForAssessment(isReferredForAssessment)
                .setMeta(meta)
                .updateImmediately(updateImmediately);


        if(medicalAdherence != null && !medicalAdherence.equals("")){
            cfLogSubmitAssessmentEvent.setMedicationAdherence(medicalAdherence);
        }
        if(pregnancyDetails != null && !pregnancyDetails.equals("")){
            cfLogSubmitAssessmentEvent.setPregnancyObject(pregnancyDetails);
        }
        if(diagnosisQuestionnaireList != null){
            cfLogSubmitAssessmentEvent.setDiagnosisQuestionnaireList(diagnosisQuestionnaireList);
        }
        if(category != null && !category.equals("")){
            cfLogSubmitAssessmentEvent.setSiteCategory(category);
        }
        if(type != null && !type.equals("")){
            cfLogSubmitAssessmentEvent.setScreeningType(type);
        }

        cfLogSubmitAssessmentEvent.build();

    }


    /**
     * logSubmitEnrolmentEvent is to submit the enrollment values for the patient to enroll in the system.
     *
     * @param patientId                     patient id in the system.
     * @param siteId                        site id in the system.
     * @param enrollmentAction              enrollment Action for the submission, add or edit.
     * @param diagnosisVitalsList           list of the diagnosis value provided by the user for recording patient vitals.
     * @param diagnosisValueList            list of the diagnosis values provided by the user.
     * @param diagnosisResultList           list of the diagnosis results provided by the user.
     * @param patientStatusList             list of the patient stats provided by the user.
     * @param treatmentPlanList             list of the treatment plan provided by the user.
     * @param diagnosisQuestionnaireList    list of the diagnosis Questionnaire value provided by the user.
     * @param pregnancyDetails         object for pregnancy diagnosis result provided by the user.
     * @param meta                          is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately             is default set to true, you can use that to log events when the app
     *                            goes in the background or closed.
     */
    @ReactMethod
    public void logSubmitEnrolmentEvent(String patientId, String siteId,
                                        String enrollmentAction,
                                        String diagnosisVitalsList,
                                        String diagnosisValueList, String diagnosisResultList,
                                        String patientStatusList, String treatmentPlanList,
                                        String diagnosisQuestionnaireList, String pregnancyDetails,
                                        String meta, Boolean updateImmediately) {

        CfLogSubmitEnrolmentEvent.Builder cfLogSubmitEnrolmentEvent = new CfLogSubmitEnrolmentEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setAction(enrollmentAction)
                .setDiagnosisVitalsList(diagnosisVitalsList)
                .setPatientStatusList(patientStatusList)
                .setMeta(meta)
                .updateImmediately(updateImmediately);

        if(diagnosisValueList != null && !diagnosisValueList.equals("")){
            cfLogSubmitEnrolmentEvent.setDiagnosisValueList(diagnosisValueList);
        }
        if(diagnosisResultList != null && !diagnosisResultList.equals("")){
            cfLogSubmitEnrolmentEvent.setDiagnosisResultList(diagnosisResultList);
        }
        if(treatmentPlanList != null && !treatmentPlanList.equals("")){
            cfLogSubmitEnrolmentEvent.setTreatmentPlanList(treatmentPlanList);
        }
        if(pregnancyDetails != null && !pregnancyDetails.equals("")){
            cfLogSubmitEnrolmentEvent.setPregnancyObject(pregnancyDetails);
        }
        if(diagnosisQuestionnaireList != null){
            cfLogSubmitEnrolmentEvent.setDiagnosisQuestionnaireList(diagnosisQuestionnaireList);
        }

        cfLogSubmitEnrolmentEvent.build();

    }


    /**
     * logCounselingEvent is to add and update lifestyle related events for patients.
     *
     * @param patientId          patient id in the system.
     * @param siteId             site id in the system.
     * @param counselingId       counseling id in the system.
     * @param counselingType     counseling type, if it is lifestyle or psychological.
     * @param counselingPlanList list of the counseling plan provided by the user.
     * @param meta               is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately  is default set to true, you can use that to log events when the app
     *                           goes in the background or closed.
     */
    @ReactMethod
    public void logCounselingEvent(String patientId, String siteId,
                                   String counselingId,
                                   String counselingType,
                                   String counselingPlanList,
                                   String meta, Boolean updateImmediately) {

        new CfLogCounselingEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setCounselingId(counselingId)
                .setCounselingType(counselingType)
                .setCounselingPlanList(counselingPlanList)
                .setMeta(meta)
                .updateImmediately(updateImmediately)
                .build();

    }


    /**
     * logInvestigationEvent to log the investigation related events of the patient treatment.
     *
     * @param patientId           patient id in the system.
     * @param siteId              site id in the system.
     * @param investigationId     investigation id in the system.
     * @param prescribedTestsList list of the investigation items provided by the user.
     * @param meta                is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately   is default set to true, you can use that to log events when the app
     *                            goes in the background or closed.
     */
    @ReactMethod
    public void logInvestigationEvent(String patientId, String siteId,
                                      String investigationId,
                                      String prescribedTestsList,
                                      String meta, Boolean updateImmediately) {

        new CfLogInvestigationEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setInvestigationId(investigationId)
                .setInvestigationList(prescribedTestsList)
                .setMeta(meta)
                .updateImmediately(updateImmediately)
                .build();

    }

    /**
     * logTreatmentPlanEvent to log the investigation related events of the patient treatment.
     *
     * @param patientId         patient id in the system.
     * @param siteId            site id in the system.
     * @param treatmentPlanId   treatment plan id in the system.
     * @param treatmentPlanList list of the treatment plan items provided by the user.
     * @param meta              is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately is default set to true, you can use that to log events when the app
     *                          goes in the background or closed.
     */
    @ReactMethod
    public void logTreatmentPlanEvent(String patientId, String siteId,
                                      String treatmentPlanId,
                                      String treatmentPlanList,
                                      String meta, Boolean updateImmediately) {

        new CfLogTreatmentPlanEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setTreatmentPlanId(treatmentPlanId)
                .setTreatmentPlanList(treatmentPlanList)
                .setMeta(meta)
                .updateImmediately(updateImmediately)
                .build();

    }

    /**
     * logPrescriptionEvent is to log additions, updates or deletions in prescription
     * related events for the patient.
     *
     * @param patientId         patient id in the system.
     * @param siteId            site id in the system.
     * @param prescriptionId    prescription id in the system.
     * @param prescriptionList  list of the prescriptions items provided by the user.
     * @param meta              is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately is default set to true, you can use that to log events when the app
     *                          goes in the background or closed.
     */
    @ReactMethod
    public void logPrescriptionEvent(String patientId, String siteId,
                                     String prescriptionId,
                                     String prescriptionList,
                                     String meta, Boolean updateImmediately) {

        new CfLogPrescriptionEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setPrescriptionId(prescriptionId)
                .setPrescriptionList(prescriptionList)
                .setMeta(meta)
                .updateImmediately(updateImmediately)
                .build();

    }

    /**
     * logSubmitMedicalReviewEvent is to submit the medical review details about the patient.
     *
     * @param patientId           patient id in the system.
     * @param siteId              site id in the system.
     * @param medicalReviewObject is the details about the medical review.
     * @param meta                is to send any other data you want to send with the ingest, can be null.
     * @param updateImmediately   is default set to true, you can use that to log events when the app
     *                            goes in the background or closed.
     */
    @ReactMethod
    public void logSubmitMedicalReviewEvent(String patientId, String siteId,
                                            String medicalReviewObject,
                                            String meta, Boolean updateImmediately) {

        new CfLogSubmitMedicalReviewEvent.Builder()
                .setPatientId(patientId)
                .setSiteId(siteId)
                .setMedicalReviewObject(medicalReviewObject)
                .setMeta(meta)
                .updateImmediately(updateImmediately)
                .build();

    }

    /**
     * Chw catalog values
     *
     * @param chwId           chw id in the system
     * @param chwCatalogModel props for the chw
     */
    @ReactMethod
    public void updateChwCatalog(String chwId, String chwCatalogModel) {

        CfChwCatalog.INSTANCE.updateChwCatalog(chwId, chwCatalogModel);
    }

    /**
     * Site catalog values
     *
     * @param siteId              site id in the system
     * @param chwSiteCatalogModel props for the site
     */
    @ReactMethod
    public void updateChwSiteCatalog(String siteId, String chwSiteCatalogModel) {

        CfChwCatalog.INSTANCE.updateChwSiteCatalog(siteId, chwSiteCatalogModel);
    }

    /**
     * Patient catalog values
     *
     * @param patientId        patient id in the system
     * @param sitePatientModel props for the patient
     */
    @ReactMethod
    public void updatePatientCatalog(String patientId, String sitePatientModel) {

        CfChwCatalog.INSTANCE.updatePatientCatalog(patientId, sitePatientModel);
    }


}
