/*******************************************************************************
 *  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.medicalrecords;

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


/**
 * MediRecProcessor provides methods to parse medical records message from Given HL7stream object.
 * It supports check for medical records Query messages.
 * Unlike MediRecRecepient it supports creation of medical records messages.
 */
public final class MediRecSource extends Message_Source_Recepient implements IMessageSource  
{
	
	/**
	 * Constructs MediRecProcessor
	 */
	public MediRecSource()
	{
		super(MediRecMessageFactory.createInstance());
	}
	
	/**
	 * Checks whether underlying Message Source 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 source 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 QRY:
		{
			if(objMessageFactory.isEventSupported(enumTriggerEvent))
				return true;
		}
		break;
		}
		return false;
	}
	
	/*
	 * Checks whether underlying Message Source 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:
			return true;
		case QRY:
			return true;
		}
		return false;
	}
	
	/**
	 * Checks whether underlying Message source 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 MDM:
			return true;
		case DOC:
			return true;
		case ACK:
		{
			if(objMessageFactory.isEventSupported(enumTriggerEvent))
				return true;
		}
		break;
		}
		return false;
	}
	
	/*
	 * Checks whether underlying Message Source 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 MDM:
		case DOC: 
		case ACK:
			return true;
		}
		return false;
	}

}
