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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Locale;

import cdac.medinfo.sdk.hl7282.hl7net.commons.IMLLP;
import cdac.medinfo.sdk.hl7282.hl7streams.HL7BufferStream;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Logger;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;

/**
 * Minimum Lower Layer Protocol (MLLP) class deals with auxiliary protocols.
 * This class works as a Utility class, if user want to send HL7 Messages, user have two options either by MLLP or by TCP/IP.
 */
public class MLLP implements IMLLP
{
	

	/* MLLP's Starting block*/
	final byte bHeader = 0X0B;
	/* MLLP's ending block*/
	final byte bTrailer = 0X1C;
	/* MLLP's Carriage return*/
	final byte bCarriageReturn = 0X0D;
	
	/**
	 * Writes MLLP's Component on to the byte array, which containing HL7 message.
	 * @param barray Containing HL7 message.
	 * @return byte[] - Return byte array containing HL7 message with MLLP's Component.
	 */
	public byte[] writeMLLPComponent(byte[] barray)
	{
		HL7BufferStream objHL7Stream = new HL7BufferStream();
		ByteArrayOutputStream tempOutStream = new ByteArrayOutputStream();
		objHL7Stream.setOutputStream(tempOutStream);

		objHL7Stream.writeByte(bHeader);
		objHL7Stream.writeBuffer(barray, 0, barray.length);
		objHL7Stream.writeByte(bTrailer);
		objHL7Stream.writeByte(bCarriageReturn);
		
		if(HL7Config.createInstance().isLoggingOn())
		{
			HL7Logger objhl7Logger = HL7Logger.getLogger();
			String strLogMessage = ResourceBundleWrapper.getValue("exceptions", "MLLP_WriteComponent",new Locale(HL7Config.createInstance().getLocale()));
			objhl7Logger.log(strLogMessage);
		}

		return objHL7Stream.toByteArray();
	}	
	
	/**
	 * Reads MLLP's Component from the byte array, which containing HL7 message.
	 * @param barray Containing HL7 message.
	 * @return byte[] - Return byte array containing HL7 message without MLLP's Component
	 */
	public byte[] readMLLPComponent(byte[] barray)
	{
		HL7BufferStream objHL7Stream = new HL7BufferStream();
		ByteArrayInputStream tempInputStream = new ByteArrayInputStream(barray);
		objHL7Stream.setInputStream(tempInputStream);
		
		HL7BufferStream objTempHL7Stream = new HL7BufferStream();
		ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
		objTempHL7Stream.setOutputStream(tempOutputStream);
		
		objHL7Stream.mark(1);
		byte bTempHeader = objHL7Stream.readByte();
		
		if(Byte.toString(bHeader).equals(Byte.toString(bTempHeader)))
		{
			for(int i = 1 ; i < barray.length ; i++)
			{
				byte b = barray[i];
				
				if(Byte.toString(b).equals(Byte.toString(bHeader)))
				{
					//Read the MLLP Header component					
				}
				else if(Byte.toString(b).equals(Byte.toString(bTrailer)))
				{
					++i;
				}
				else
				{
					objTempHL7Stream.writeByte(b);
				}
			}
			
			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger objhl7Logger = HL7Logger.getLogger();
				String strLogMessage = ResourceBundleWrapper.getValue("exceptions", "MLLP_ReadComponent",new Locale(HL7Config.createInstance().getLocale()));
				objhl7Logger.log(strLogMessage);
			}
			
			return objTempHL7Stream.toByteArray();
		}
		else
		{
			objHL7Stream.reset();
			return barray;
		}
	}
}
