package cdac.medinfo.sdk.hl7282.hl7net.upperlayer;

import java.util.Locale;

import cdac.medinfo.sdk.hl7282.hl7net.commons.IHL7Batch;
import cdac.medinfo.sdk.hl7282.hl7net.commons.IHL7BatchFile;
import cdac.medinfo.sdk.hl7282.baselibs.ST;
import cdac.medinfo.sdk.hl7282.baselibs.messages.ACK;
import cdac.medinfo.sdk.hl7282.baselibs.messages.AbstractQuery;
import cdac.medinfo.sdk.hl7282.baselibs.messages.Message;
import cdac.medinfo.sdk.hl7282.baselibs.segments.BHS;
import cdac.medinfo.sdk.hl7282.baselibs.segments.FHS;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSA;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Logger;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;


class StatusLogger 
{

	private static String getLogString(Message objMessage)
	{
		String strCode = objMessage.getMessageCode();
		String strTriggerEvent = objMessage.getTriggerEvent();
		String strMessageControlId = null;
		MSH objMsh = objMessage.getHeader();
		ST stControlID = objMsh.getMessageControlID();
		if(stControlID!=null)
			strMessageControlId = (String)stControlID.getValue();
		String strQueryID = null;
		String strAcknowledgedFor = null;

		if (objMessage instanceof AbstractQuery)
		{
			AbstractQuery objQueryMessage = (AbstractQuery) objMessage;
			ST objQueryID = objQueryMessage.getQueryIdentifier();
			if(objQueryID!=null)
				strQueryID = (String)objQueryID.getValue();
		}

		if (objMessage instanceof ACK)
		{
			ACK objACK = (ACK) objMessage;
			MSA objMsa = objACK.getMessageAcknowledgmentSegment();
			ST AcknowledgedFor = objMsa.getMessageControlID();
			if(AcknowledgedFor!=null)
				strAcknowledgedFor = (String)AcknowledgedFor.getValue();
		}
		
		StringBuffer objStringBuffer = new StringBuffer();
		objStringBuffer.append(ResourceBundleWrapper.getValue("status","MessageInfo",new Locale(HL7Config.createInstance().getLocale())));
		objStringBuffer.append('\n');
		objStringBuffer.append(ResourceBundleWrapper.getValue("status","MessageCode",new Locale(HL7Config.createInstance().getLocale()))+strCode);
		objStringBuffer.append('\n');
		objStringBuffer.append(ResourceBundleWrapper.getValue("status","TriggerEvent",new Locale(HL7Config.createInstance().getLocale()))+strTriggerEvent);
		objStringBuffer.append('\n');
		objStringBuffer.append(ResourceBundleWrapper.getValue("status","MessageControlID",new Locale(HL7Config.createInstance().getLocale()))+strMessageControlId);
		objStringBuffer.append('\n');
		if(strQueryID!=null)
		{
			objStringBuffer.append(ResourceBundleWrapper.getValue("status","QueryID",new Locale(HL7Config.createInstance().getLocale()))+strQueryID);
			objStringBuffer.append('\n');
		}
		if(strAcknowledgedFor!=null)
		{
			objStringBuffer.append(ResourceBundleWrapper.getValue("status","AckFor",new Locale(HL7Config.createInstance().getLocale()))+strAcknowledgedFor);
			objStringBuffer.append('\n');
		}
		return objStringBuffer.toString();
	}
	
	public static void logMessage(Message objMessage)
	{
		HL7Logger objHL7Logger = HL7Logger.getLogger();
		objHL7Logger.log(getLogString(objMessage));
	}

	public static void logBatchFile(IHL7BatchFile objBatchFile)
	{
		StringBuffer objStringBuffer = new StringBuffer();
		
		String strFileID =null; 
		FHS objFhs = objBatchFile.getBatchFileHeader();
		if(objFhs!=null)
		{
			ST objSt = objFhs.getFileControlID();
			if(objSt!=null)
				strFileID = (String)objSt.getValue();
		}

		if(strFileID!=null)
		{
			objStringBuffer.append(ResourceBundleWrapper.getValue("status","BatchFileInfo",new Locale(HL7Config.createInstance().getLocale())));
			objStringBuffer.append('\n');
			objStringBuffer.append(ResourceBundleWrapper.getValue("status","FileControlID",new Locale(HL7Config.createInstance().getLocale()))+strFileID);
			objStringBuffer.append('\n');
		}
		ISingleCollection<IHL7Batch> objBatchCollection = objBatchFile.getHL7BatchCollection();
		if(objBatchCollection!=null && objBatchCollection.size()>0)
		{
			for(IHL7Batch objBatch : objBatchCollection)
			{
				String strBatchID = null;
				BHS objBhs = objBatch.getBatchHeader();
				if(objBhs!=null)
				{
					ST objSt = objBhs.getBatchControlID();
					if(objSt!=null)
						strBatchID = (String)objSt.getValue();
				}
				objStringBuffer.append(ResourceBundleWrapper.getValue("status","BatchInfo",new Locale(HL7Config.createInstance().getLocale())));
				objStringBuffer.append('\n');
				if(strBatchID!=null)
				{
					objStringBuffer.append(ResourceBundleWrapper.getValue("status","BatchControlID",new Locale(HL7Config.createInstance().getLocale()))+strBatchID);
					objStringBuffer.append('\n');
				}

				ISingleCollection<Message> objMessageCollection = objBatch.getMessageCollection();
				if(objMessageCollection!=null)
				{
					for(Message objMessage : objMessageCollection)
					{
						objStringBuffer.append(getLogString(objMessage));
						objStringBuffer.append('\n');
					}
				}
			}
		}
		
		HL7Logger objHL7Logger = HL7Logger.getLogger();
		objHL7Logger.log(objStringBuffer.toString());
	}

}
