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

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

import org.joda.time.DateTime;
import org.joda.time.Days;

import com.agilex.healthcare.schedulingmanager.domain.SMConsult;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultDetail;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultService;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultServices;
import com.agilex.healthcare.schedulingmanager.domain.SMConsults;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseException;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.ConsultDetailTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.ConsultTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.ConsultingServiceTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedConsultDetailArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedConsultingServicesArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.TaggedConsultsArray;
import com.agilex.healthcare.utility.DateHelper;

public class ConsultTranslator {
	//translate consults
	public SMConsults translate(TaggedConsultsArray arr) {
		SMConsults retValue = new SMConsults();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedConsultArray was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getConsults() == null || arr.getConsults().getConsultTO() == null)
			return retValue;

		Iterator<ConsultTO> itTagged = arr.getConsults().getConsultTO().iterator();
		while (itTagged.hasNext()) {
			ConsultTO consultTO = itTagged.next();
			//Perform Error Checking
			if (consultTO.getFault() != null)
				throw new MdwsResponseException(consultTO.getFault());

			SMConsult smConsult = createSMConsult(consultTO);
			retValue.add(smConsult);
		}

		return retValue;
	}

	private SMConsult createSMConsult(ConsultTO consultTO) {
		String consultIen = consultTO.getConsultIen();
		String status = consultTO.getStatus();
		String serviceIen = consultTO.getServiceIen();
		String serviceName = consultTO.getServiceName();
		String patientDfn = consultTO.getPatientDfn();
		String patientName = consultTO.getPatientName();
		String sendingProviderDuz = consultTO.getSendingProviderDuz();
		String sendingProviderName = consultTO.getSendingProviderName();
		String requestDate = consultTO.getRequestDate();
		
		SMConsult smConsult = new SMConsult();
		smConsult.setConsultIen(consultIen);
		smConsult.setStatus(status);
		smConsult.setServiceIen(serviceIen);
		smConsult.setServiceName(serviceName);
		smConsult.setPatientDfn(patientDfn);
		smConsult.setPatientName(patientName);
		smConsult.setSendingProviderDuz(sendingProviderDuz);
		smConsult.setSendingProviderName(sendingProviderName);
		if (requestDate != null) {
			Date dtRequest = DateHelper.parseVistaDateTime(requestDate);
			DateTime today = new DateTime();
			smConsult.setRequestDate(dtRequest);
			int daysOnList = Days.daysBetween(new DateTime(dtRequest), today).getDays();
			smConsult.setDaysOnList(Integer.toString(daysOnList));
		}
		return smConsult;
	}
	
//----------------------------------------------------------------------------------------------------------------------

	//Translate consult services
	public SMConsultServices translate(TaggedConsultingServicesArray arr) {
		SMConsultServices retValue = new SMConsultServices();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedConsultingServiceArray was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getServices() == null || arr.getServices().getConsultingServiceTO() == null)
			return retValue;

		Iterator<ConsultingServiceTO> itConsultingServiceTO = arr.getServices().getConsultingServiceTO().iterator();
		while (itConsultingServiceTO.hasNext()) {
			ConsultingServiceTO consultingServiceTO = itConsultingServiceTO.next();
			//Perform Error Checking
			if (consultingServiceTO.getFault() != null)
				throw new MdwsResponseException(consultingServiceTO.getFault());

			if(consultingServiceTO.isHasChildren() == false) {
				SMConsultService smConsultService = createSMConsultService(consultingServiceTO);
				retValue.add(smConsultService);
			}
		}

		return retValue;
	}

	private SMConsultService createSMConsultService(ConsultingServiceTO consultingServiceTO) {
		String serviceIen = consultingServiceTO.getServiceIen();
		String serviceName = consultingServiceTO.getServiceName();
		String parent = consultingServiceTO.getParent();
		boolean hasChildren = consultingServiceTO.isHasChildren();
		String serviceUsage = consultingServiceTO.getServiceUsage();
		String orderableItem = consultingServiceTO.getOrderableItem();
		
		SMConsultService smConsultService = new SMConsultService();
		smConsultService.setServiceIen(serviceIen);
		smConsultService.setServiceName(serviceName);
		smConsultService.setParent(parent);
		smConsultService.setHasChildren(hasChildren);
		smConsultService.setServiceUsage(serviceUsage);
		smConsultService.setOrderableItem(orderableItem);

		return smConsultService;
	}
	
//----------------------------------------------------------------------------------------------------------------------
	
	//Consult Details
	public SMConsultDetail translate(TaggedConsultDetailArray arr){
		SMConsultDetail retValue = new SMConsultDetail();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "TaggedConsultDetailArray was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getLines() == null || arr.getLines().getConsultDetailTO() == null)
			return retValue;

		StringBuffer stringBuffer = new StringBuffer();
		Iterator<ConsultDetailTO> itConsultDetailTO = arr.getLines().getConsultDetailTO().iterator();
		while (itConsultDetailTO.hasNext()) {
			ConsultDetailTO consultDetailTO = itConsultDetailTO.next();
			//Perform Error Checking
			if (consultDetailTO.getFault() != null)
				throw new MdwsResponseException(consultDetailTO.getFault());

			String text = consultDetailTO.getText();
			if(stringBuffer.length() == 0)
				stringBuffer.append(text);
			else
				stringBuffer.append("\n").append(text);
			
		}

		String text = stringBuffer.toString();
		retValue.setText(text);
		return retValue;
	}
}
