/*******************************************************************************
 *  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.hl7model.messages.query;

import cdac.medinfo.sdk.hl7282.baselibs.messages.IMessageRecepient;
import cdac.medinfo.sdk.hl7282.baselibs.messages.Message_Source_Recepient;
import cdac.medinfo.sdk.hl7282.utils.EnumMessageCode;
import cdac.medinfo.sdk.hl7282.utils.EnumTriggerEvent;

/**
 * QryRecepient provides methods to parse message from Given HL7stream object.
 * It supports check for Query messages.
 * Unlike Message Processor it does not support creation of Message.
 * With implementor of this interface you can not trigger event.
 * But you can process event.It also has support for creation of Queries. 
 */
public final class QryRecepient extends Message_Source_Recepient implements IMessageRecepient 
{
	

	/**
	 * Constructs QryRecepient
	 */
	public QryRecepient()
	{
		super(QryMessageFactory.createInstance());
	}

	/**
	 * Checks whether underlying Message Recipient supports extension of message for provided message code and trigger event.
	 * For Local trigger events enum value must be null.
	 * @param enumMsgCode specified message code.
	 * @param enumTriggerEvent specified trigger event
	 * @return boolean status as true or false
	 */
	public boolean isMessageExtensionSupported(EnumMessageCode enumMsgCode,EnumTriggerEvent enumTriggerEvent)
	{
		return true;
	}

	/**
	 * Checks whether underlying Message Recipient can send specified Message or not.
	 * @param enumMessageCode specified message code.
	 * @param enumTriggerEvent specified trigger event
	 * @return boolean status as true or false
	 */
	public boolean canSend(EnumMessageCode enumMessageCode,EnumTriggerEvent enumTriggerEvent)
	{
		/*
		 * If enum trigger event is null then provided message code belongs to a locally extended message
		 * It simply checks whether this message code is supported or not.
		 */
		if(enumTriggerEvent == null)
			return canSend(enumMessageCode);
		
		switch(enumMessageCode)
		{
		case ACK:
		{
			if(objMessageFactory.isEventSupported(enumTriggerEvent))
				return true;
		}
		break;
		case RSP:
		{
			switch(enumTriggerEvent)
			{
			case K11:
			case Z82:
			case Z84:
			case Z86:
			case Z88:
			case Z90:
				return true;			
			}
		}
		break;
		case RDY:
			return true;
		case RTB:
		{
			switch(enumTriggerEvent)
			{
			case K13:
			case Z76:
			case Z78:
			case Z92:
			case Z94:
			case Z96:
				return true;			
			}
		}
		break;
	
		}
		return false;
	}

	/**
	 * Checks whether underlying Message Recipient can receive specified Message or not.
	 * @param enumMessageCode specified message code.
	 * @param enumTriggerEvent specified trigger event
	 * @return boolean status as true or false
	 */
	public boolean canReceive(EnumMessageCode enumMessageCode,EnumTriggerEvent enumTriggerEvent)
	{
		/*
		 * If enum trigger event is null then provided message code belongs to a locally extended message
		 * It simply checks whether this message code is supported or not.
		 */
		if(enumTriggerEvent == null)
			return canReceive(enumMessageCode);
		
		switch(enumMessageCode)
		{
		case ACK:
		{
			if(objMessageFactory.isEventSupported(enumTriggerEvent))
				return true;
		}
		break;
		case QBP:
		{
			switch(enumTriggerEvent)
			{
			case Q11:
			case Q13:
			case Q15:
			case Q16:
			case Q17:
			case Z75:
			case Z77:
			case Z79:
			case Z81:
			case Z85:
			case Z87:
			case Z89:
			case Z91:
			case Z93:
			case Z95:
			case Z97:
			case Z99:
				return true;
			}
		}
		break;
		case QSB:
		case QSX:
		case QVR:
			return true;
		case QCN:
		{
			switch(enumTriggerEvent)
			{
			case J01:
				return true;			
			}
		}
		break;
		case UDM:
			return true;
		}
		
		return false;
	}

	/*
	 * Checks whether underlying Message Recepient can send specified Message or not. 
	 * This method is specifically written for handling custom messages.
	 * When custom messages are to be sent, the decision is based on message code only.
	 * @param enumMessageCode specified message code.
	 * @return boolean status as true or false
	 */
	private boolean canSend(EnumMessageCode enumMessageCode)
	{
		switch(enumMessageCode)
		{
		case ACK:
		case RSP:
		case RDY:
		case RTB:
			return true;
		}
		return false;
	}

	/*
	 * Checks whether underlying Message Recepient can receive specified Message or not. 
	 * This method is specifically written for handling custom messages.
	 * When custom messages are to be sent, the decision is based on message code only.
	 * @param enumMessageCode specified message code.
	 * @return boolean status as true or false
	 */
	private boolean canReceive(EnumMessageCode enumMessageCode)
	{
		switch(enumMessageCode)
		{
		case ACK:
		case QBP:
		case QSB:
		case QSX:
		case QCN:
		case QVR:
		case UDM:
			return true;
		}
		return false;
	}
	
}
