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

import java.io.IOException;

import cdac.medinfo.sdk.hl7282.baselibs.segments.SegmentSet;
import cdac.medinfo.sdk.hl7282.commoninterface.ICollectionIterator;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegment;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.SingleCollection;

/**
 * MessageStreamSplitter performs splitting operation on InputStream that it receives from Network Layer.
 * Splitting involves making chunks of Stream of Messages such as each chunk contains one message.
 */
public class MessageStreamSplitter   
{
	

	/**
	 * Instantiates MessageStreamSplitter
	 */
	public MessageStreamSplitter()
	{
	}

	/**
	 * Reads Byte Array specified and divides them into stream pieces containing single message.
	 * Returns collection of streams and each stream contains single message.
	 * @param buf Byte Array
	 * @return ISingleCollection<HL7BufferStream> - collection of streams. Each stream presents single unit of serialized HL7Objects (Message or Batch or BatchFile.).
	 * @throws IOException 
	 */
	public ISingleCollection<ISegmentSet> splitMessageBuffer(ISegmentSet objSegmentSet)
	{
		ISingleCollection<ISegmentSet> objSegmentSetList = new SingleCollection<ISegmentSet>();
		if(objSegmentSet != null)
		{
			ISegmentSet objTempSegmentSet = null;
			boolean isBatchRunning = false;
			boolean isFileRunning = false;

			ICollectionIterator<ISegment> objIterator = objSegmentSet.iterator();
			ISegment objSegment = null;
			while(objIterator.hasNext())
			{
				objSegment = objIterator.next();

				if(objSegment.getName() == EnumSegments.HL7_SEG_MSH)
				{
					if(objTempSegmentSet == null)
					{
						objTempSegmentSet = new SegmentSet();
						objTempSegmentSet.addSegment(objSegment);
					}
					else 
					{
						if(!isBatchRunning && !isFileRunning)
						{
							objSegmentSetList.add(objTempSegmentSet);
							objTempSegmentSet = new SegmentSet();
							objTempSegmentSet.addSegment(objSegment);
						}
						else
						{
							objTempSegmentSet.addSegment(objSegment);
						}
					}
				}
				else if(objSegment.getName() == EnumSegments.HL7_SEG_BHS)
				{
					if(objTempSegmentSet == null)
					{
						objTempSegmentSet = new SegmentSet();
						objTempSegmentSet.addSegment(objSegment);
						isBatchRunning = true;
					}
					else 
					{
						if(!isFileRunning)
						{
							objSegmentSetList.add(objTempSegmentSet);
							objTempSegmentSet = new SegmentSet();
							objTempSegmentSet.addSegment(objSegment);
							isBatchRunning = true;
						}
						else
						{
							objTempSegmentSet.addSegment(objSegment);
						}
					}
				}
				else if(objSegment.getName() == EnumSegments.HL7_SEG_FHS)
				{
					if(objTempSegmentSet == null)
					{
						objTempSegmentSet = new SegmentSet();
						objTempSegmentSet.addSegment(objSegment);
						isFileRunning = true;
					}
					else 
					{
						objSegmentSetList.add(objTempSegmentSet);
						objTempSegmentSet = new SegmentSet();
						objTempSegmentSet.addSegment(objSegment);
						isFileRunning = true;
					}
				}
				else if(objSegment.getName() == EnumSegments.HL7_SEG_BTS)
				{
					objTempSegmentSet.addSegment(objSegment);
					if(isBatchRunning)
					{
						isBatchRunning = false;
						objSegmentSetList.add(objTempSegmentSet);
						if(isFileRunning == false)
							objTempSegmentSet = null;
					}
				}
				else if(objSegment.getName() == EnumSegments.HL7_SEG_FTS)
				{
					objTempSegmentSet.addSegment(objSegment);
					if(isFileRunning)
					{
						isFileRunning = false;
						objSegmentSetList.add(objTempSegmentSet);
						objTempSegmentSet = null;
					}
				}
				else
				{
					objTempSegmentSet.addSegment(objSegment);
				}
			}
			
			if(objTempSegmentSet != null)
				objSegmentSetList.add(objTempSegmentSet);
		}

		return objSegmentSetList;
	}


}