/*******************************************************************************
 *  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.messagedictionary.MessageMapReader;
import cdac.medinfo.sdk.hl7282.baselibs.messages.ACK;
import cdac.medinfo.sdk.hl7282.baselibs.messages.IMessageFactory;
import cdac.medinfo.sdk.hl7282.baselibs.messages.Message;
import cdac.medinfo.sdk.hl7282.exceptions.MessageNotSupportedException;
import cdac.medinfo.sdk.hl7282.utils.EnumHL7System;
import cdac.medinfo.sdk.hl7282.utils.EnumMessageCode;
import cdac.medinfo.sdk.hl7282.utils.EnumTriggerEvent;
import cdac.medinfo.sdk.hl7282.utils.IMap;
import cdac.medinfo.sdk.hl7282.utils.IMessageMapReader;

class MediRecMessageFactory implements IMessageFactory
{
	private static MediRecMessageFactory objMediRecMessageFactory = null;
	private static IMessageMapReader objMessamapReader = null;

	private MediRecMessageFactory()
	{}

	/**
	 * Creates Object of ADTMessageFactory.
	 * @return ADTMessageFactory object of ADTMessageFactory
	 */
	public static MediRecMessageFactory createInstance()
	{
		if(objMediRecMessageFactory == null)
		{
			objMessamapReader = MessageMapReader.createInstance(EnumHL7System.HL7_SYSTEM_MEDICALRECORDS);
			objMediRecMessageFactory = new MediRecMessageFactory();
		}
		return objMediRecMessageFactory;
	}


	/**
	 * Creates Message with specified enumerations of Trigger Event & Message Structure
	 * @param enumMessageCode enumerated value of Message Code
	 * @param enumTriggerEvent enumerated value of Trigger Event
	 * @return Message instance of Message
	 */
	public Message createMessage(EnumMessageCode enumMessageCode,EnumTriggerEvent enumTriggerEvent)
	{
		Message objMessage = null;

		IMap objMessageMap = getMessageMap(enumMessageCode, enumTriggerEvent);

		if(enumMessageCode == EnumMessageCode.ACK)
		{
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode, null);
			if(isEventSupported(enumTriggerEvent))
				objMessage = new ACK(enumTriggerEvent, objMessageMap);
			else
				throw new MessageNotSupportedException(enumTriggerEvent.getValue());
		}
		else
		{
			switch(enumTriggerEvent)
			{
			case T01 : 
				objMessage = new MDM_T01(objMessageMap);
				break; 
			case T02 : 
				objMessage = new MDM_T02(objMessageMap);
				break; 
			case T03 : 
				objMessage = new MDM_T03(objMessageMap);
				break; 
			case T04 : 
				objMessage = new MDM_T04(objMessageMap);
				break; 
			case T05 : 
				objMessage = new MDM_T05(objMessageMap);
				break; 
			case T06 : 
				objMessage = new MDM_T06(objMessageMap);
				break; 
			case T07 : 
				objMessage = new MDM_T07(objMessageMap);
				break; 
			case T08 : 
				objMessage = new MDM_T08(objMessageMap);
				break; 
			case T09 : 
				objMessage = new MDM_T09(objMessageMap);
				break; 
			case T10 : 
				objMessage = new MDM_T10(objMessageMap);
				break;
			case T11 : 
				objMessage = new MDM_T11(objMessageMap);
				break; 

			}
		}

		return objMessage;
	}

	/**
	 * Creates Message with specified enumerations of Trigger Event & Message Structure
	 * @param enumMessageCode enumerated value of Message Code
	 * @param enumTriggerEvent enumerated value of Trigger Event
	 * @return Message instance of Message
	 */
	private IMap getMessageMap(EnumMessageCode enumMessageCode,EnumTriggerEvent enumTriggerEvent)
	{
		IMap objMessageMap = null;

		switch(enumTriggerEvent)
		{
		case T01 : 
		case T03 :
		case T05 :
		case T07 :
		case T09 :
		case T11 :
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode, EnumTriggerEvent.T01);
			break; 
		case T02 : 
		case T04 : 
		case T06 : 
		case T08 : 
		case T10 : 
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode, EnumTriggerEvent.T02);
			break;
	
		}

		return objMessageMap;
	}

	/**
	 * Checks whether Trigger Event specified in Enumeration is Supported.
	 * @param enumTriggerEvent enumerated value of Trigger Event
	 * @return boolean shows status as success or failure
	 */
	public boolean isEventSupported(EnumTriggerEvent enumTriggerEvent) 
	{
		if(enumTriggerEvent.equals(EnumTriggerEvent.T01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T03)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T04)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T05)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T06)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T07)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T08)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T09)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T10)
				|| enumTriggerEvent.equals(EnumTriggerEvent.T11)
		)
			return true;
		else 
			return false;
	}

	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.DOC || enumMessageCode == EnumMessageCode.MDM ||
				enumMessageCode == EnumMessageCode.QRY || enumMessageCode == EnumMessageCode.ACK)
			return true;

		return false;
	}
}
