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

import java.util.Iterator;

import com.agilex.healthcare.mobilehealthplatform.domain.Patient;
import com.agilex.healthcare.mobilehealthplatform.domain.PatientIdentifier;
import com.agilex.healthcare.mobilehealthplatform.domain.Patients;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseException;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.PatientTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.PatientArray;
import com.agilex.healthcare.utility.NullChecker;

public class PatientArrayTranslator {
	//private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(PatientArrayTranslator.class);
	
	public static Patients translate(PatientArray arr, String siteCode) {
		Patients retValue = new Patients();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "PatientArray was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getPatients() == null || arr.getPatients().getPatientTO() == null)
			return retValue;
		
		Iterator<PatientTO> it = arr.getPatients().getPatientTO().iterator();
		while (it.hasNext()) {
			PatientTO p = it.next();
			//Perform Error Checking
			if (p.getFault() != null)
				throw new MdwsResponseException(p.getFault());
			
			String localPid                      = p.getLocalPid();
			int age                              = p.getAge();
			String cwad                          = p.getCwad();
			String gender                        = p.getGender();
			String name                          = p.getName();
			String ssn                           = p.getSsn();
			boolean inpatient                    = p.isInpatient();
			boolean serviceConnected             = p.isServiceConnected();
			
			
			Patient pat = new Patient();
			pat.setAge(Integer.toString(age));
			pat.setCwad(cwad);
			pat.setDisplayName(name);
			
			attemptToSetFirstMiddleLastName(pat, name);
			
			pat.setGender(gender);
			pat.setInpatient(inpatient);
			
			String assigningAuthority = "dfn-"+siteCode;
			String uniqueId = localPid;
			PatientIdentifier patientIdentifier = new PatientIdentifier(assigningAuthority, uniqueId);
			pat.setPatientIdentifier(patientIdentifier);
			
			pat.setServiceConnected(serviceConnected);
			pat.setSsn(ssn);

			retValue.add(pat);
		}
		
		return retValue;
	}

	private static void attemptToSetFirstMiddleLastName(Patient patient, String patientName) {
		if (NullChecker.isNotNullish(patientName)) {
			String[] splitNames = patientName.trim().split(",");
			patient.setLastName(splitNames[0]);
			String[] firstAndMiddle = splitNames[1].toString().trim().split(" ");

			switch (firstAndMiddle.length) {
                case 1:
                    patient.setFirstName(firstAndMiddle[0]);
                    break;
                case 2:
                    patient.setFirstName(firstAndMiddle[0]);
                    patient.setMiddleName(firstAndMiddle[1]);
                    break;
                case 3: // rare, but possible
                    patient.setFirstName(firstAndMiddle[0]);
                    patient.setMiddleName(firstAndMiddle[1] + " " + firstAndMiddle[2]);
                    break;
                case 4: // even more rare, but possible
                    patient.setFirstName(firstAndMiddle[0]);
                    patient.setMiddleName(firstAndMiddle[1] + " " + firstAndMiddle[2] + " " + firstAndMiddle[3]);
                    break;
                default:
                    patient.setFirstName(firstAndMiddle[0]); // first name is required by vista;
			}
		}
	}
}
