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



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import cdac.medinfo.sdk.hl7282.baselibs.messages.IMessageRecepient;
import cdac.medinfo.sdk.hl7282.baselibs.messages.IMessageSource;
import cdac.medinfo.sdk.hl7282.baselibs.messages.Message;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Parser;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Serializer;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.commoninterface.IDelimiter;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.hl7streams.HL7FileStream;
import cdac.medinfo.sdk.hl7282.utils.EnumMessageCode;
import cdac.medinfo.sdk.hl7282.utils.EnumTriggerEvent;
import cdac.medinfo.sdk.hl7282.utils.HL7Constants;

public class MessageTest 
{
	private HL7FileStream objIHL7Stream;

	private File objFileWrite;
	private FileOutputStream objFileOutputStream;
	private FileInputStream objFileInputStream;
	
	protected Populate_Test objPopulate_Test;
	protected IDelimiter objIDelimiter;
	protected MSH objMSH;
	protected MSH objNewMSH; 
	private IMessageSource source;
	private IMessageRecepient recepient;
	private String strOutputdirectory;

	public MessageTest()
	{
		strOutputdirectory = "e:\\Test\\"; 
		File testDir =  new File(strOutputdirectory);
		testDir.mkdirs(); 
			
		objPopulate_Test = new Populate_Test();
		
		objMSH = new MSH();
		objMSH.setFieldSeparator('|');//ST
		objMSH.setEncodingChars('^','&','~','\\','#');//ST
		objIDelimiter = objMSH.getDelimiter();
		objMSH.setSendingApplication("NID", "Universal Id", "x400");//HD
		objMSH.setSendingFacility("NID", "Universal Id", "x400");//HD
		objMSH.setReceivingApplication("NID", "Universal Id", "x400");//HD
		objMSH.setReceivingFacility("NID", "Universal Id", "x400");//HD
		objMSH.setDateTimeOfMessage("20000320");//TS
		objMSH.setSecurity("Security");//ST
		objMSH.setMessageControlID("MSG0001");//ST
		objMSH.setProcessingID("T", "T");//PT
		objMSH.setVersionID("2.5", null, null);//VID
		objMSH.setSequenceNumber("123");//NM
		objMSH.setContinuationPointer("ContinuationPointer");//ST
		objMSH.setAcceptAcknowledgmentType("AL");//ID
		objMSH.setApplicationAcknowledgmentType("AL");//ID
		objMSH.setCountryCode("CCD");//ID
		objMSH.addCharacterSet("ASCII");//ID
		objMSH.setPrincipalLanguageOfMessage(PopulateDataType.populateCWE(HL7Constants.HL7_COMPONENT));//CE
		objMSH.setAlternateCharacterSetHandlingScheme("ISO 2022-1994");//ID
		objMSH.addMessageProfileIdentifier("Entity Identifier", "NAMESPACE", "UniversalId", "x500");//EI
		
		
		objNewMSH = new MSH();
		objNewMSH.setFieldSeparator(objMSH.getFieldSeparator());//ST
		objNewMSH.setEncodingChars(objMSH.getEncodingChar());//ST
		objNewMSH.setSendingApplication(objMSH.getSendingApplication());//HD
		objNewMSH.setSendingFacility(objMSH.getSendingApplication());//HD
		objNewMSH.setReceivingApplication(objMSH.getReceivingApplication());//HD
		objNewMSH.setReceivingFacility(objMSH.getReceivingFacility());//HD
		objNewMSH.setDateTimeOfMessage(objMSH.getDateTimeOfMessage());//TS
		objNewMSH.setSecurity(objMSH.getSecurity());//ST
		objNewMSH.setMessageControlID(objMSH.getMessageControlID());//ST
		objNewMSH.setProcessingID(objMSH.getProcessingID());//PT
		objNewMSH.setVersionID(objMSH.getVersionID());//VID
		objNewMSH.setSequenceNumber(objMSH.getSequenceNumber());//NM
		objNewMSH.setContinuationPointer(objMSH.getContinuationPointer());//ST
		objNewMSH.setAcceptAcknowledgmentType(objMSH.getAcceptAcknowledgmentType());//ID
		objNewMSH.setApplicationAcknowledgmentType(objMSH.getApplicationAcknowledgmentType());//ID
		objNewMSH.setCountryCode(objMSH.getCountryCode());//ID
		objNewMSH.setCharacterSet(objMSH.getCharacterSet());//ID
		objNewMSH.setPrincipalLanguageOfMessage(objMSH.getPrincipalLanguageOfMessage());//CE
		objNewMSH.setAlternateCharacterSetHandlingScheme(objMSH.getAlternateCharacterSetHandlingScheme());//ID
		objNewMSH.setMessageProfileIdentifier(objMSH.getMessageProfileIdentifier());//EI
	}
	
	public MSH getMSH()
	{
		return objMSH;
	}

	public void setMessageSource(IMessageSource source)
	{
		this.source = source;
	}

	public void setMessageRecepient(IMessageRecepient recepient)
	{
		this.recepient = recepient;
	}

	public void getFileForSerialization(String strTriggerEvent,String strMessageCode,String strPrefix)
	{
		objIHL7Stream = new HL7FileStream();
		try
		{
			String strPath = null;
			if(strPrefix != null)
				strPath = strOutputdirectory+strPrefix + "_" + strMessageCode+"_"+strTriggerEvent+".HL7";
			else
				strPath = strOutputdirectory+strMessageCode+"_"+strTriggerEvent+".HL7";
			objFileWrite = new File(strPath);
			objFileWrite.createNewFile();
			objFileOutputStream = new FileOutputStream(objFileWrite);	
			objIHL7Stream.setOutputStream(objFileOutputStream);
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}

	}

	public void doPostPopulateOperations(Message objMessage,String strTriggerEvent,String strMessageCode)
	{
		try{
			if(objMessage.validate())
			{
				ISegmentSet objSegmentSet = objMessage.getSegmentSet();
				HL7Serializer objHL7Serializer = new HL7Serializer();
				objHL7Serializer.serialize(objSegmentSet,objIHL7Stream);
				System.out.print("Message serialized successfully\n");
			}
			objFileOutputStream.close();

			objFileInputStream = new FileInputStream(new File(strOutputdirectory+strMessageCode+"_"+strTriggerEvent+".HL7"));
			objIHL7Stream.setInputStream(objFileInputStream);
			objIHL7Stream.setOutputStream(objFileOutputStream);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private Message createMessage(String strTriggerEvent,String strMessageCode)
	{
		EnumTriggerEvent event; 
		EnumMessageCode code;
		Message objMessage = null;
		try{
			event = EnumTriggerEvent.valueOf(strTriggerEvent);
			code = EnumMessageCode.valueOf(strMessageCode);

			if(source != null)
				objMessage = source.createMessage(code,event);
			else
				objMessage = recepient.createMessage(code,event);

		}
		catch(IllegalArgumentException iae)
		{
			iae.printStackTrace();
		}
		return objMessage;
	}

	public Message endingTestcode(String strTriggerEvent,String strMessageStructure)
	{
		Message objMessage = null;
		try
		{
			objMessage = createMessage(strTriggerEvent, strMessageStructure);
			HL7Parser objHL7Parser = new HL7Parser();
			ISegmentSet objSegmentSet = objHL7Parser.parse(objIHL7Stream);
			if(objMessage.populate(objSegmentSet) == HL7Constants.HL7_FUNCTION_SUCCESS)
			{
				System.out.print("Message parsed successfully");
			}	
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return objMessage;
	}
}

