/*******************************************************************************
 *  Copyright 2017 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.hl7282.baselibs.messages;

import java.io.IOException;
import java.util.Locale;

import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Parser;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.commoninterface.IHL7Stream;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegment;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidMessageException;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Logger;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;

/**
 *	Validator class to validate the HL7 message file. 
 */
public final class HL7MessageValidator 
{
	
	
	private MessageFactory objMessageFactory = null;
	
	/**
	 * Sets Message Factory.
	 * @param objFactory instance of MessageFactory
	 */
	public void setMessageFactory(MessageFactory objFactory)
	{
		this.objMessageFactory = objFactory;
	}
	
	/**
	 * Retrieves Message Factory.
	 * @return MessageFactory - instance of MessageFactory
	 */
	public MessageFactory getMessageFactory()
	{
		return this.objMessageFactory;
	}
	
	/**
	 * Validates the HL7 message file.
	 * @param strFilePath String value of absolute path of HL7 message file
	 * @return True - if file is valide HL7 message file
	 * @throws IOException throws I/O Exception if parsing failed
	 * @throws InvalidMessageException throws Invalid Message Exception if validation fails
	 */
	public boolean validate(String strFilePath) throws IOException, InvalidMessageException
	{
		HL7Parser objHL7Parser = new HL7Parser();
		ISegmentSet objSegmentSet = objHL7Parser.parse(strFilePath);

		Message objMessage = getMessage(objSegmentSet); 

		//Validate message
		if(objMessage != null)
			return objMessage.validate();
		else
			return false;
	}

	/**
	 * Validates the stream of HL7 message.
	 * @param objIHL7Stream Object of HL7Stream
	 * @return True - if file is valid HL7 message file
	 * @throws IOException throws I/O Exception if parsing failed
	 * @throws InvalidMessageException throws Invalid Message Exception if validation fails
	 */
	public boolean validate(IHL7Stream objIHL7Stream) throws IOException, InvalidMessageException
	{
		HL7Parser objHL7Parser = new HL7Parser();
		ISegmentSet objSegmentSet = objHL7Parser.parse(objIHL7Stream);

		Message objMessage = getMessage(objSegmentSet); 

		//Validate message
		if(objMessage != null)
			return objMessage.validate();
		else
			return false;
	}

	/**
	 * Gives the populated message.
	 * @param objSegmentSet Object of ISegmentSet
	 * @return Object of Message
	 */
	private Message getMessage(ISegmentSet objSegmentSet)
	{
		Message objMessage = null;

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

		if(strSegment.equals(EnumSegments.HL7_SEG_MSH.getValue()))
		{
			//Retrieve TriggerEvent and MessageCode
			String strTriggerEvent = (String)((MSH)objSegment).getMessageType().getTriggerEvent().getValue();
			String strMessageCode = (String)((MSH)objSegment).getMessageType().getMessageCode().getValue();
			
			objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objSegmentSet);
			
			if(objMessage == null)
			{
				objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
				if(objMessage != null)
					objMessage.populate(objSegmentSet);
			}
		}
		else
		{
			if(HL7Config.createInstance().isLoggingOn())
			{
				String strMessage = ResourceBundleWrapper.getValue("warnings", "MSHMissing",new Locale(HL7Config.createInstance().getLocale()));
				HL7Logger objHLogger = HL7Logger.getLogger();
				objHLogger.log(strMessage);
			}
		}
		return objMessage;
	}
}
