package com.agilex.healthcare.schedulingmanager.mdws.translator;


import java.util.Date;
import java.util.Iterator;

import com.agilex.healthcare.schedulingmanager.domain.WaitListEntries;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntry;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseException;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.EwlStatusTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.NearListByPatientTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.RecallListByPatientTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedEwlStatusArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedEwlStatusArrays;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedNearListByPatientArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedNearListByPatientArrays;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedRecallListByPatientArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedRecallListByPatientArrays;
import com.agilex.healthcare.utility.DateHelper;
import com.agilex.healthcare.utility.NullChecker;

public class WaitListTranslator {

	/**
	 * Populates waitListEntries with EwlStatus
	 * 
	 * @param reportDocuments Unlike the other translators which return a collection, this expects you to pass the collection
	 * into the method and it will then populate that collection for you.  Passing in null for this will result in
	 * an exception being thrown.
	 */
	public void add(WaitListEntries waitListEntries, TaggedEwlStatusArrays arr) {
		if (waitListEntries == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_INPUT, "waitListEntries was null");
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedEwlStatusArrays was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedEwlStatusArray() == null)
			return;

		Iterator<TaggedEwlStatusArray> itTagged = arr.getArrays().getTaggedEwlStatusArray().iterator();
		while (itTagged.hasNext()) {
			TaggedEwlStatusArray tagged = itTagged.next();
			//Perform Error Checking
			if (tagged.getFault() != null)
				throw new MdwsResponseException(tagged.getFault());

			if (tagged.getStatuses() == null || tagged.getStatuses().getEwlStatusTO() == null)
				continue;

			Iterator<EwlStatusTO> it = tagged.getStatuses().getEwlStatusTO().iterator();
			while (it.hasNext()) {
				EwlStatusTO ewlStatusTO = it.next();
				//Perform Error Checking
				if (ewlStatusTO.getFault() != null)
					throw new MdwsResponseException(ewlStatusTO.getFault());

				WaitListEntry waitListEntry = createWaitListEntry(ewlStatusTO);
				if (waitListEntry != null)
					waitListEntries.add(waitListEntry);
			}
		}
    }

    protected WaitListEntry createWaitListEntry(EwlStatusTO ewlStatusTO){
        String message = ewlStatusTO.getMessage();
		if (message != null && message.contains("PATIENT NOT ON THE EWL"))
            return null;
        
        String dfn = ewlStatusTO.getDfn();
		String stopCode = ewlStatusTO.getStopCode();
		String dateLastUpdated = ewlStatusTO.getDateLastUpdated();
		Date dtDateLastUpdated = DateHelper.parseVistaDateTime(dateLastUpdated);
		String patientName = ewlStatusTO.getPatientName();
		String patientSsn = ewlStatusTO.getPatientSsn();
		String clinicName = ewlStatusTO.getClinicName();
		String originatingDate = ewlStatusTO.getOriginatingDate();
		Date dtOriginatingDate = DateHelper.parseVistaDateTime(originatingDate);
		String desiredApptDate = ewlStatusTO.getDesiredApptDate();
		Date dtDesiredApptDate = DateHelper.parseVistaDateTime(desiredApptDate);
		String daysOnEwl = ewlStatusTO.getDaysOnEwl();
		String dateRemovedFromEwl = ewlStatusTO.getDateRemovedFromEwl();
		Date dtDateRemovedFromEwl = DateHelper.parseVistaDateTime(dateRemovedFromEwl);
		String patientZip = ewlStatusTO.getPatientZip();
		String svcConnectedPct = ewlStatusTO.getSvcConnectedPct();
		String enrolleeStatus = ewlStatusTO.getEnrolleeStatus();
		String priority = ewlStatusTO.getPriority();
		String clinicId = ewlStatusTO.getClinicId();
		String ewlien = ewlStatusTO.getEwlien();
		
		WaitListEntry waitListEntry = new WaitListEntry();
        waitListEntry.setDfn(dfn);
        waitListEntry.setStopCode(stopCode);
        if (NullChecker.isNotNullish(dateLastUpdated))
			waitListEntry.setDateLastUpdated(dtDateLastUpdated);
		waitListEntry.setPatientName(patientName);
        waitListEntry.setSsn(patientSsn);
        waitListEntry.setClinicName(clinicName);
        if (NullChecker.isNotNullish(originatingDate))
			waitListEntry.setOriginatingDate(dtOriginatingDate);
		if (NullChecker.isNotNullish(desiredApptDate))
			waitListEntry.setDesiredDate(dtDesiredApptDate);
		waitListEntry.setDaysOnList(daysOnEwl);
        if (NullChecker.isNotNullish(dateRemovedFromEwl))
			waitListEntry.setDateRemovedFromEwl(dtDateRemovedFromEwl);
		waitListEntry.setPatientZip(patientZip);
        waitListEntry.setServiceConnected(svcConnectedPct);
        waitListEntry.setEnrolleeStatus(enrolleeStatus);
        waitListEntry.setPriority(priority);
        waitListEntry.setClinicId(clinicId);
        waitListEntry.setEwlIen(ewlien);
        waitListEntry.setWaitListType("EWL");
        return waitListEntry;
    }

//----------------------------------------------------------------------------------------------------------------------

	/**
	 * Populates waitListEntries with NearListByPatient
	 * 
	 * @param reportDocuments Unlike the other translators which return a collection, this expects you to pass the collection
	 * into the method and it will then populate that collection for you.  Passing in null for this will result in
	 * an exception being thrown.
	 */
	public void add(WaitListEntries waitListEntries, TaggedNearListByPatientArrays arr) {
		if (waitListEntries == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_INPUT, "waitListEntries was null");
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedNearListByPatientArrays was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedNearListByPatientArray() == null)
			return;

		Iterator<TaggedNearListByPatientArray> itTagged = arr.getArrays().getTaggedNearListByPatientArray().iterator();
		while (itTagged.hasNext()) {
			TaggedNearListByPatientArray tagged = itTagged.next();
			//Perform Error Checking
			if (tagged.getFault() != null)
				throw new MdwsResponseException(tagged.getFault());

			if (tagged.getStatuses() == null || tagged.getStatuses().getNearListByPatientTO() == null)
				continue;

			Iterator<NearListByPatientTO> it = tagged.getStatuses().getNearListByPatientTO().iterator();
			while (it.hasNext()) {
				NearListByPatientTO nearListByPatientTO = it.next();
				//Perform Error Checking
				if (nearListByPatientTO.getFault() != null)
					throw new MdwsResponseException(nearListByPatientTO.getFault());

				WaitListEntry waitListEntry = createWaitListEntry(nearListByPatientTO);
				if (waitListEntry != null)
					waitListEntries.add(waitListEntry);
			}
        }
    }

    private WaitListEntry createWaitListEntry(NearListByPatientTO nearListByPatientTO){
        String message = nearListByPatientTO.getMessage();
		if(message != null && message.contains("Patient not on the NEAR List")){
            return null;
        }
		String dfn = nearListByPatientTO.getDfn();
		String patientname = nearListByPatientTO.getPatientname();
		String patientssn = nearListByPatientTO.getPatientssn();
		String eligcodeexternal = nearListByPatientTO.getEligcodeexternal();
		String eligcodeinternal = nearListByPatientTO.getEligcodeinternal();
		String homephone = nearListByPatientTO.getHomephone();
		String cellphone = nearListByPatientTO.getCellphone();
		//String appointmentrequestdate = nearListByPatientTO.getAppointmentrequestdate();
        //Date dtappointmentrequestdate = DateHelper.parseVistaDateTime(appointmentrequestdate);
        String facilityid = nearListByPatientTO.getFacilityid();
		
        WaitListEntry waitListEntry = new WaitListEntry();
        waitListEntry.setDfn(dfn);
        waitListEntry.setPatientName(patientname);
        waitListEntry.setSsn(patientssn);
        waitListEntry.setEligibilityCodeExternal(eligcodeexternal);
        waitListEntry.setEligibilityCodeInternal(eligcodeinternal);
        waitListEntry.setHomePhone(homephone);
        waitListEntry.setCellPhone(cellphone);
        /*if (NullChecker.isNotNullish(appointmentrequestdate))
			waitListEntry.setAppointmentRequestDate(dtappointmentrequestdate);
		*/
        waitListEntry.setFacilityId(facilityid);
        waitListEntry.setMessage(message);
        waitListEntry.setWaitListType("NEAR");
        return waitListEntry;
    }

//----------------------------------------------------------------------------------------------------------------------
	
	/**
	 * Populates waitListEntries with RecallListByPatient
	 * 
	 * @param reportDocuments Unlike the other translators which return a collection, this expects you to pass the collection
	 * into the method and it will then populate that collection for you.  Passing in null for this will result in
	 * an exception being thrown.
	 */
	public void add(WaitListEntries waitListEntries, TaggedRecallListByPatientArrays arr) {
		if (waitListEntries == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_INPUT, "waitListEntries was null");
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedRecallListByPatientArrays was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedRecallListByPatientArray() == null)
			return;

		Iterator<TaggedRecallListByPatientArray> itTagged = arr.getArrays().getTaggedRecallListByPatientArray().iterator();
		while (itTagged.hasNext()) {
			TaggedRecallListByPatientArray tagged = itTagged.next();
			//Perform Error Checking
			if (tagged.getFault() != null)
				throw new MdwsResponseException(tagged.getFault());

			if (tagged.getStatuses() == null || tagged.getStatuses().getRecallListByPatientTO() == null)
				continue;

			Iterator<RecallListByPatientTO> it = tagged.getStatuses().getRecallListByPatientTO().iterator();
			while (it.hasNext()) {
				RecallListByPatientTO recallListByPatientTO = it.next();
				//Perform Error Checking
				if (recallListByPatientTO.getFault() != null)
					throw new MdwsResponseException(recallListByPatientTO.getFault());

				WaitListEntry waitListEntry = createWaitListEntry(recallListByPatientTO);
				if (waitListEntry != null)
					waitListEntries.add(waitListEntry);
	        }
	    }
    }

    private WaitListEntry createWaitListEntry(RecallListByPatientTO recallListByPatientTO) {
        String message = recallListByPatientTO.getMessage();
		if (message != null && message.contains("Patient not on Recall List"))
            return null;
        
		String dfn = recallListByPatientTO.getDfn();
		String clinicid = recallListByPatientTO.getClinicid();
		String accessionno = recallListByPatientTO.getAccessionno();
		String provider = recallListByPatientTO.getProvider();
		String recalldate = recallListByPatientTO.getRecalldate();
        Date dtRecallDate = DateHelper.parseVistaDateTime(recalldate);
        String comment = recallListByPatientTO.getComment();
		String fastnonfasting = recallListByPatientTO.getFastnonfasting();
		String apptlength = recallListByPatientTO.getApptlength();
		String dateremindersent = recallListByPatientTO.getDateremindersent();
        Date dtReminderSent = DateHelper.parseVistaDateTime(dateremindersent);
        String user = recallListByPatientTO.getUser();
		String recalldate1 = recallListByPatientTO.getRecalldate1();
        Date dtRecallDate1 = DateHelper.parseVistaDateTime(recalldate1);
		String secondprintdate = recallListByPatientTO.getSecondprintdate();
		Date dtSecondPrintDate = DateHelper.parseVistaDateTime(secondprintdate);
		String patientname = recallListByPatientTO.getPatientname();
		String patientSSN = recallListByPatientTO.getPatientSSN();
		String clinicName = recallListByPatientTO.getClinicName();
		
		WaitListEntry waitListEntry = new WaitListEntry();
        waitListEntry.setDfn(dfn);
        waitListEntry.setClinicId(clinicid);
        waitListEntry.setAccessionno(accessionno);
        waitListEntry.setProvider(provider);
        if (NullChecker.isNotNullish(recalldate))
			waitListEntry.setRecallDate(dtRecallDate);
		waitListEntry.setComment(comment);
		waitListEntry.setFastNonFasting(fastnonfasting);
		waitListEntry.setAppointmentLength(apptlength);
		if (NullChecker.isNotNullish(dateremindersent))
			waitListEntry.setDateReminderSent(dtReminderSent);
		waitListEntry.setUser(user);
		if (NullChecker.isNotNullish(recalldate1))
			waitListEntry.setRecallDate1(dtRecallDate1);
		if (NullChecker.isNotNullish(secondprintdate))
			waitListEntry.setSecondPrintDate(dtSecondPrintDate);
		waitListEntry.setPatientName(patientname);
		waitListEntry.setSsn(patientSSN);
		waitListEntry.setClinicName(clinicName);
        waitListEntry.setWaitListType("RECALL");
        return waitListEntry;
    }
}
