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.EwlByFacilityTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.NearListByFacilityTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.RecallListByFacilityTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedEwlByFacilityArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedEwlByFacilityArrays;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedNearListByFacilityArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedNearListByFacilityArrays;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedRecallListByFacilityArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedRecallListByFacilityArrays;
import com.agilex.healthcare.utility.DateHelper;
import com.agilex.healthcare.utility.NullChecker;

public class FacilityWaitListTranslator {
	// EWL WaitList
	public WaitListEntries translate(TaggedEwlByFacilityArrays arr){
		WaitListEntries retValue = new WaitListEntries();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedEwlByFacilityArrays was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedEwlByFacilityArray() == null)
			return retValue;

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

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

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

				WaitListEntry waitListEntry = createWaitListEntry(ewlByFacilityTO);
				retValue.add(waitListEntry);
			}
		}

		return retValue;
	}

	protected WaitListEntry createWaitListEntry(EwlByFacilityTO ewlByFacilityTO) {
		String dfn = ewlByFacilityTO.getDfn();
		String dssstopcode = ewlByFacilityTO.getDssstopcode();
		String lastupdatedate = ewlByFacilityTO.getLastupdatedate();
		String patientname = ewlByFacilityTO.getPatientname();
		String patientssn = ewlByFacilityTO.getPatientssn();
		String clinicname = ewlByFacilityTO.getClinicname();
		String originatingdate = ewlByFacilityTO.getOriginatingdate();
		String desireddateofappt = ewlByFacilityTO.getDesireddateofappt();
		String daysonewl = ewlByFacilityTO.getDaysonewl();
		String dateremovedfromewl = ewlByFacilityTO.getDateremovedfromewl();
		String patientzipcode = ewlByFacilityTO.getPatientzipcode();
		String serviceconnectedpercentage = ewlByFacilityTO.getServiceconnectedpercentage();
		String enrolleestatus = ewlByFacilityTO.getEnrolleestatus();
		String priority = ewlByFacilityTO.getPriority();
		String clinicId = ewlByFacilityTO.getClinicId();
		String ewlien = ewlByFacilityTO.getEwlien();
		
		WaitListEntry waitListEntry = new WaitListEntry();
		waitListEntry.setDfn(dfn);
		waitListEntry.setStopCode(dssstopcode);
		if(NullChecker.isNotNullish(lastupdatedate)) {
			Date dtLastUpdate = DateHelper.parseVistaDateTime(lastupdatedate);
			waitListEntry.setDateLastUpdated(dtLastUpdate);
		}
		waitListEntry.setPatientName(patientname);
		waitListEntry.setSsn(patientssn);
		waitListEntry.setClinicName(clinicname);
		if(NullChecker.isNotNullish(originatingdate)) {
			Date dtOriginating = DateHelper.parseVistaDateTime(originatingdate);
			waitListEntry.setOriginatingDate(dtOriginating);
		}
		if(NullChecker.isNotNullish(desireddateofappt)) {
			Date dtDesired = DateHelper.parseVistaDateTime(desireddateofappt);
			waitListEntry.setDesiredDate(dtDesired);
		}
		waitListEntry.setDaysOnList(daysonewl);
		if(NullChecker.isNotNullish(dateremovedfromewl)) {
			Date dtRemoved = DateHelper.parseVistaDateTime(dateremovedfromewl);
			waitListEntry.setDateRemovedFromEwl(dtRemoved);
		}
		waitListEntry.setPatientZip(patientzipcode);
		waitListEntry.setServiceConnected(serviceconnectedpercentage);
		waitListEntry.setEnrolleeStatus(enrolleestatus);
		waitListEntry.setPriority(priority);
		waitListEntry.setClinicId(clinicId);
		waitListEntry.setEwlIen(ewlien);
		waitListEntry.setWaitListType("EWL");
		return waitListEntry;
	}
	
//----------------------------------------------------------------------------------------------------------------------

	//RECALL Wait List
	public WaitListEntries translate(TaggedRecallListByFacilityArrays arr){
		WaitListEntries retValue = new WaitListEntries();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedRecallListByFacilityArrays was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedRecallListByFacilityArray() == null)
			return retValue;

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

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

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

				WaitListEntry waitListEntry = createWaitListEntry(recallListByFacilityTO);
				retValue.add(waitListEntry);
			}
		}

		return retValue;
	}

	private WaitListEntry createWaitListEntry(RecallListByFacilityTO recallListByFacilityTO) {
		String dfn = recallListByFacilityTO.getDfn();
		String clinicid = recallListByFacilityTO.getClinicid();
		String accessionno = recallListByFacilityTO.getAccessionno();
		String provider = recallListByFacilityTO.getProvider();
		String recalldate = recallListByFacilityTO.getRecalldate();
		String comment = recallListByFacilityTO.getComment();
		String fastnonfasting = recallListByFacilityTO.getFastnonfasting();
		String apptlength = recallListByFacilityTO.getApptlength();
		String dateremindersent = recallListByFacilityTO.getDateremindersent();
		String user = recallListByFacilityTO.getUser();
		String recalldate1 = recallListByFacilityTO.getRecalldate1();
		String secondprintdate = recallListByFacilityTO.getSecondprintdate();
		//String patientname = recallListByFacilityTO.getPatientname();
		//String patientSSN = recallListByFacilityTO.getPatientSSN();
		String clinicName = recallListByFacilityTO.getClinicName();
		
		WaitListEntry waitListEntry = new WaitListEntry();
		waitListEntry.setDfn(dfn);
		waitListEntry.setClinicId(clinicid);
		waitListEntry.setAccessionno(accessionno);
		waitListEntry.setProvider(provider);
		if(NullChecker.isNotNullish(recalldate)) {
			Date dtRecall = DateHelper.parseVistaDateTime(recalldate);
			waitListEntry.setRecallDate(dtRecall);
		}
		waitListEntry.setComment(comment);
		waitListEntry.setFastNonFasting(fastnonfasting);
		waitListEntry.setAppointmentLength(apptlength);
		if(NullChecker.isNotNullish(dateremindersent)) {
			Date dtReminder = DateHelper.parseVistaDateTime(dateremindersent);
			waitListEntry.setDateReminderSent(dtReminder);
		}
		waitListEntry.setUser(user);
		if(NullChecker.isNotNullish(recalldate1)) {
			Date dtRecall = DateHelper.parseVistaDateTime(recalldate1);
			waitListEntry.setRecallDate1(dtRecall);
		}
		if(NullChecker.isNotNullish(secondprintdate)) {
			Date dtSecondPrint = DateHelper.parseVistaDateTime(secondprintdate);
			waitListEntry.setSecondPrintDate(dtSecondPrint);
		}
		//waitListEntry.setPatientName(patientname);
		//waitListEntry.setSsn(patientSSN);
		waitListEntry.setClinicName(clinicName);
		waitListEntry.setWaitListType("RECALL");
		return waitListEntry;
	}
	
//----------------------------------------------------------------------------------------------------------------------

	//NEAR List

	public WaitListEntries translate(TaggedNearListByFacilityArrays arr){
		WaitListEntries retValue = new WaitListEntries();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedNearListByFacilityArrays was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedNearListByFacilityArray() == null)
			return retValue;

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

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

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

				WaitListEntry waitListEntry = createWaitListEntry(nearListByFacilityTO);
				retValue.add(waitListEntry);
			}
		}

		return retValue;
	}


	private WaitListEntry createWaitListEntry(NearListByFacilityTO nearListByFacilityTO) {
		String dfn = nearListByFacilityTO.getDfn();
		String patientname = nearListByFacilityTO.getPatientname();
		String patientssn = nearListByFacilityTO.getPatientssn();
		String eligcodeinternal = nearListByFacilityTO.getEligcodeinternal();
		String eligcodeexternal = nearListByFacilityTO.getEligcodeexternal();
		String homephone = nearListByFacilityTO.getHomephone();
		String cellphone = nearListByFacilityTO.getCellphone();
		//String appointmentrequestdate = nearListByFacilityTO.getAppointmentrequestdate();
		String facilityid = nearListByFacilityTO.getFacilityid();
		
		WaitListEntry waitListEntry = new WaitListEntry();
		waitListEntry.setDfn(dfn);
		waitListEntry.setPatientName(patientname);
		waitListEntry.setSsn(patientssn);
		waitListEntry.setEligibilityCodeInternal(eligcodeinternal);
		waitListEntry.setEligibilityCodeExternal(eligcodeexternal);
		waitListEntry.setHomePhone(homephone);
		waitListEntry.setCellPhone(cellphone);
		/*if(NullChecker.isNotNullish(appointmentrequestdate)) {
			Date dtAppointment = DateHelper.parseVistaDateTime(appointmentrequestdate);
			waitListEntry.setAppointmentRequestDate(dtAppointment);
		}*/
		waitListEntry.setFacilityId(facilityid);
		waitListEntry.setWaitListType("NEAR");
		return waitListEntry;
	}
	
//----------------------------------------------------------------------------------------------------------------------

	//Verify Remove Recall call
	/*public boolean isRecallEntryRemovedSuccessFully(TaggedRemoveFromRecallListArrays arr){
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "XXX was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getArrays() == null || arr.getArrays().getTaggedRemoveFromRecallListArray() == null)
			return false;

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

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

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

				String message = removeFromRecallListTOs.getMessage();
				if (message != null && message.equals("DELETED") == true)
					return true;
			}
		}

		return false;
	}*/
}
