package cdac.medinfo.sdk.hl7282.tool.hl7dump;

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

import cdac.medinfo.sdk.hl7282.baselibs.messages.Message;
import cdac.medinfo.sdk.hl7282.baselibs.messages.MessageFactory;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Parser;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.baselibs.segments.Segment;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegment;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.hl7model.messages.applicationmanagement.AppMgtRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.applicationmanagement.AppMgtSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.clinicallaboratoryautomation.ClinicalLabAutoRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.clinicallaboratoryautomation.ClinicalLabAutoSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.financialmanagement.FinancialRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.financialmanagement.FinancialSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.masterfiles.MasterFileRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.masterfiles.MasterFileSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.medicalrecords.MediRecRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.medicalrecords.MediRecSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.observationreporting.ObsvRepRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.observationreporting.ObsvRepSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.orderentry.OrderEntryRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.orderentry.OrderEntrySource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientadministration.ADTRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientadministration.ADTSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientcare.PatientCareRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientcare.PatientCareSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientreferral.PatientRefRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientreferral.PatientRefSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.personnelmanagement.PersonalMgmtRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.personnelmanagement.PersonalMgmtSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.query.QryRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.query.QrySource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.scheduling.SchedulingRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.scheduling.SchedulingSource;
import cdac.medinfo.sdk.hl7282.hl7streams.HL7FileStream;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
/**
 *	This Utility class will dump information of message like its segments, groups and its attribute values to MS document.
 */
public class HL7Dump 
{
	static int lengthOfOption = 18;
	static int lengthOfDetails = 79 - lengthOfOption;

	private HL7FileStream objIHL7Stream;
	private File objFileWrite;
	private FileOutputStream objFileOutputStream;

	public static void main(String[] args) 
	{
		String strLoggingPath = null;
		String strDestFolder = null;
		String strHL7File = null;
		HL7Dump objHL7Dump = new HL7Dump();

		
		/*
		 * If no parameters are provided then help need to be displayed.
		 */
		if(args == null || args.length ==0)
		{
			objHL7Dump.displayHelp();
			System.exit(0);
		}

		/*
		 * Initializing required parameters provided through cmd line arguments.
		 */
		for(int i=0; i<args.length; i++)
		{
			if(args[i].equalsIgnoreCase("-d") && args.length > i+1)
			{
				//Reading logging path information
				strLoggingPath = args[i+1];
				i++;
			}
			else if(args[i].equalsIgnoreCase("-s") && args.length > i+1)
			{
				//reading destination dir path where received requests will be dumped
				strDestFolder = args[i+1] ;
				i++;
			}
			else if(args[i].equalsIgnoreCase("-f") && args.length > i + 1)
			{
				strHL7File = args[i + 1];
				i++;
			}
			else
			{
				System.out.println("Not a valid command");
				objHL7Dump.displayHelp();
				System.exit(0);
			}
		}

		//HL7 message file is minimum parameter requirement
		if(strHL7File == null)
		{
			System.out.println("Not a valid command");
			objHL7Dump.displayHelp();
			System.exit(0);
		}

		try
		{
			HL7Config objHL7Config = HL7Config.createInstance();

			//Specifying logging directory.
			//User can specify any directory where log files will be created.
			//If no path is specified then logs will be generated in user's temp directory.
			objHL7Config.enableLogging(strLoggingPath);

			if(strHL7File != null)
			{

				if(strDestFolder != null)				
					objHL7Dump.dumpHL7Message(strHL7File, strDestFolder);
				else
					objHL7Dump.dumpHL7Message(strHL7File, System.getProperty("java.io.tmpdir")+"/CDAC-SDK/HL7-J/HL7Dump/");
			}
			else
			{			
				System.out.println("Not a valid command, Source HL7 file for dump is not specified.");
				objHL7Dump.displayHelp();
				System.exit(0);
			}
		}
		catch(Exception exception)
		{
			System.out.println("Operation failed :" + exception.getMessage());
		}
	}

	/**
	 * Displays the help.
	 */
	private void displayHelp()
	{
		System.out.println("\nHL7DUMP :: Creates MS Document having infromation of all attributes of HL7 \nmessage.");
		System.out.println("\nHL7DUMP [-d LogDir]  " +
		"-f HL7file [-s DestinationFolder] \n  " + "\n");

		printToConsole("-d", "Log directory");
		printToConsole("", "User's temp directory is taken as default.");
		printToConsole("-f", "Source HL7 file for dump.");
		printToConsole("-s", "Destination folder path to store converted MS documet file.");
		printToConsole("", "User's temp directory is taken as default.");
        
        System.out.println("\nExample :");

		System.out.println("\n1. HL7DUMP -f image.hl7 -s C:/HL7Content " +
		"\nDumps the information of HL7 message to MS document and stores same to folder \nC:/HL7Content.\n");	
	}

	/**
	 * Display Help of command on console in proper manner.
	 * @param strOption Option for command.
	 * @param strDetails Description of option.
	 */
	public static void printToConsole(String strOption, String strDetails)
	{
		String strBuffer = "";
		if (strOption != null)
			strBuffer = strBuffer + strOption ;
		while (lengthOfOption > strBuffer.length())
		{
			strBuffer = strBuffer + " ";
		}
		if (strDetails != null)
		{
			if (strDetails.length() < lengthOfDetails || strDetails.length() == lengthOfDetails)
			{
				strBuffer = strBuffer + strDetails;
				strDetails = null;
			}
			else
			{
				strBuffer = strBuffer + strDetails.substring(0, lengthOfDetails);
				strDetails = strDetails.substring(lengthOfDetails, (strDetails.length() ));
			}
		}

		System.out.println(strBuffer);
		if (strDetails == null)
			return;
		else
			printToConsole(null, strDetails);
	}

	/**
	 * Display Help of command on console in proper manner.
	 * @param strAttribute Attribute for option.
	 * @param strDesc Description of attributes.
	 */
	public static void printToConsole(String strAttribute, String[] strDesc)
	{
		if (strDesc.length > 1)
			printToConsole(strAttribute, strDesc[0]);
		for(int i = 1;i<strDesc.length;i++)
			printToConsole(null, strDesc[i]);
	}


	/**
	 * Creates the MS document for HL7 file.
	 * @param strHL7File HL7 File path.
	 * @param strDestinationPath  Destination folder to store created document.
	 */
	private void dumpHL7Message(String strHL7File,String strDestinationPath) throws Exception
	{
		HL7Parser objParser = new HL7Parser();
		ISegmentSet objSegmentSet = objParser.parse(strHL7File);

		ISegment objSegment = objSegmentSet.getSegment(0);
		String strSegment = objSegment.getName().getValue();
		strSegment = strSegment.trim();

		if(strSegment.equals(EnumSegments.HL7_SEG_MSH.getValue()))
		{
			String strMessageCode =null;
			String strTriggerEvent = null;
			Segment objTempSegment = (Segment)objSegmentSet.getSegment(0);
			if(objTempSegment.getName().equals(EnumSegments.HL7_SEG_MSH))
			{
				MSH objMSH = (MSH)objTempSegment;
				strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();
				strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
			}
			Message objMessage = getMessage(strMessageCode, strTriggerEvent, objSegmentSet);
			
			//Create Dump folder and file
			getFileForDump(strTriggerEvent,strMessageCode,strDestinationPath);

			//Call Dump
			if(objMessage != null)
				objMessage.dumpMessage(objIHL7Stream);

			System.out.println("\nDump of HL7 message is stored at " + strDestinationPath);
		}
		else
		{
			System.out.println("\nOperation supported for HL7 messages only. ");
		}
	}

	/*
	 * Populates message through provided SegmentSet,MessageCode and TriggerEvent
	 */
	private Message getMessage(String strMessageCode,String strTriggerEvent,ISegmentSet objSegmentSet)
	{
		Message objMessage = null;

		MessageFactory objMessageFactory = getMessageFactory();

		objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objSegmentSet);

		if(objMessage != null)
		{
			objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
			if(objMessage != null)
				objMessage.populate(objSegmentSet);
		}

		return objMessage;
	}

	/*
	 * Populates Message Factory
	 */
	private MessageFactory getMessageFactory()
	{
		MessageFactory objMessageFactory = MessageFactory.createInstance();

		objMessageFactory.addMessageSource(new ObsvRepSource());
		objMessageFactory.addMessageRecepient(new ObsvRepRecepient());
		objMessageFactory.addMessageSource(new AppMgtSource());
		objMessageFactory.addMessageRecepient(new AppMgtRecepient());
		objMessageFactory.addMessageSource(new ClinicalLabAutoSource());
		objMessageFactory.addMessageRecepient(new ClinicalLabAutoRecepient());
		objMessageFactory.addMessageSource(new FinancialSource());
		objMessageFactory.addMessageRecepient(new FinancialRecepient());
		objMessageFactory.addMessageSource(new MasterFileSource());
		objMessageFactory.addMessageRecepient(new MasterFileRecepient());
		objMessageFactory.addMessageSource(new MediRecSource());
		objMessageFactory.addMessageRecepient(new MediRecRecepient());
		objMessageFactory.addMessageSource(new OrderEntrySource());
		objMessageFactory.addMessageRecepient(new OrderEntryRecepient());
		objMessageFactory.addMessageSource(new ADTSource());
		objMessageFactory.addMessageRecepient(new ADTRecepient());
		objMessageFactory.addMessageSource(new PatientCareSource());
		objMessageFactory.addMessageRecepient(new PatientCareRecepient());
		objMessageFactory.addMessageSource(new PatientRefSource());
		objMessageFactory.addMessageRecepient(new PatientRefRecepient());
		objMessageFactory.addMessageSource(new PersonalMgmtSource());
		objMessageFactory.addMessageRecepient(new PersonalMgmtRecepient());
		objMessageFactory.addMessageSource(new QrySource());
		objMessageFactory.addMessageRecepient(new QryRecepient());
		objMessageFactory.addMessageSource(new SchedulingSource());
		objMessageFactory.addMessageRecepient(new SchedulingRecepient());

		return objMessageFactory;
	}

	/**
	 * Generates file for dumping
	 * @param strTriggerEvent String value for TriggerEvent
	 * @param strMessageCode String value for MessageCode
	 * @param strDestinationPath String value Destination Dolder Path
	 */
	private void getFileForDump(String strTriggerEvent,String strMessageCode, String strDestinationPath)
	{
		objIHL7Stream = new HL7FileStream();
		try
		{
			File testDir =  new File(strDestinationPath);
			testDir.mkdir();
			String strPathForDump = null;
			strPathForDump = strDestinationPath + "\\" +  strMessageCode+"_"+strTriggerEvent+".doc";
			objFileWrite = new File(strPathForDump);
			objFileWrite.createNewFile();
			objFileOutputStream = new FileOutputStream(objFileWrite);	
			objIHL7Stream.setOutputStream(objFileOutputStream);
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}


