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

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 AppMgtMessageFactory implements IMessageFactory
{
	private static AppMgtMessageFactory objAppMgtMessageFactory = null;
	
	static IMessageMapReader objMessageMapReader;

	/**
	 * Creates Object of AppMgtMessageFactory.
	 * @return ADTMessageFactory object of ADTMessageFactory
	 */
	public static AppMgtMessageFactory createInstance()
	{
		if(objAppMgtMessageFactory == null)
			objAppMgtMessageFactory = new AppMgtMessageFactory();
		objMessageMapReader = MessageMapReader.createInstance(EnumHL7System.HL7_SYSTEM_APPLICATIONMANAGEMENT);
		return objAppMgtMessageFactory;
	}
	
	/**
	 * 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 = objMessageMapReader.getMessageMap(enumMessageCode,enumTriggerEvent);

		if(enumMessageCode == EnumMessageCode.ACK)
		{
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, null);
			if(isEventSupported(enumTriggerEvent))
				objMessage = new ACK(enumTriggerEvent, objMessageMap);
			else
				throw new MessageNotSupportedException(enumTriggerEvent.getValue());
		}
		else
		{
			switch(enumTriggerEvent)
			{

			case N02:
				objMessage = new NMD_N02(objMessageMap);
				break;
			}
		}
		return objMessage;
	}

	/**
	 * 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.N02))
			return true;
		else 
			return false;
	}
	
	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.NMR || enumMessageCode == EnumMessageCode.ACK || enumMessageCode == EnumMessageCode.NMD || enumMessageCode == EnumMessageCode.NMQ)
			return true;
		
		return false;
	}
}
