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

import java.util.Date;

import com.agilex.healthcare.schedulingmanager.domain.CancelAppointment;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseException;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.CancelAppointmentTO;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.HospitalLocationTO;
import com.agilex.healthcare.utility.DateHelper;
import com.agilex.healthcare.utility.NullChecker;

public class CancelAppointmentTranslator {

    public CancelAppointment translate(CancelAppointmentTO cancelAppointmentTO){
    	if (cancelAppointmentTO == null)
    		throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "CancelAppointmentTO was null");
    	if (cancelAppointmentTO.getFault() != null)
    		throw new MdwsResponseException(cancelAppointmentTO.getFault());

    	HospitalLocationTO clinic = cancelAppointmentTO.getClinic();
    	String timestamp = cancelAppointmentTO.getTimestamp();
		String purpose = cancelAppointmentTO.getPurpose();
		String reason = cancelAppointmentTO.getReason();
		String remarks = cancelAppointmentTO.getRemarks();
		
    	CancelAppointment cancelAppointment = new CancelAppointment();
    	if (clinic != null) {
    		//Perform Error Checking
    		if (clinic.getFault() != null)
        		throw new MdwsResponseException(clinic.getFault());
	    	String id = clinic.getId();
			cancelAppointment.setClinicId(id);
    	}
    	
        if(NullChecker.isNotNullish(timestamp)) {
			Date dt = DateHelper.parseVistaDateTime(timestamp);
			cancelAppointment.setAppointmentTime(dt);
		}
        
        cancelAppointment.setCancelCode(purpose);
        cancelAppointment.setCancelReason(reason);
        cancelAppointment.setRemarks(remarks);

        return cancelAppointment;
    }
}
