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



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 MasterFileMessageFactory  implements IMessageFactory
{
	private static MasterFileMessageFactory objMasterFileMessageFactory = null;
	private static IMessageMapReader objMessageMapReader = null;
	private MasterFileMessageFactory()
	{}

	/**
	 * Creates Object of MasterFileMessageFactory.
	 * @return MasterFileMessageFactory object of MasterFileMessageFactory
	 */
	public static MasterFileMessageFactory createInstance()
	{
		if(objMasterFileMessageFactory == null)
			objMasterFileMessageFactory = new MasterFileMessageFactory();
		objMessageMapReader = MessageMapReader.createInstance(EnumHL7System.HL7_SYSTEM_MASTERFILES);
		return objMasterFileMessageFactory;
	}

	/**
	 * 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 = objMessageMapReader.getMessageMap(enumMessageCode, null);
			if(isEventSupported(enumTriggerEvent))
				objMessage = new ACK(enumTriggerEvent, objMessageMap);
			else
				throw new MessageNotSupportedException(enumTriggerEvent.getValue());
		}
		else
		{
			switch(enumTriggerEvent)
			{
			case M02:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M02(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M02(objMessageMap);
					break;
				}
				break;

			case M04:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M04(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M04(objMessageMap);
					break;
				}
				break;
			case M05:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M05(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M05(objMessageMap);
					break;
				}
				break;
			case M06:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M06(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M06(objMessageMap);
					break;
				}
				break;
			case M07:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M07(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M07(objMessageMap);
					break;
				}
				break;
			case M08:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M08(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M08(objMessageMap);
					break;
				}
				break;
			case M09:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M09(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M09(objMessageMap);
					break;
				}
				break;
			case M10:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M10(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M10(objMessageMap);
					break;
				}
				break;
			case M11:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M11(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M11(objMessageMap);
					break;
				}
				break;
			case M12:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M12(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M12(objMessageMap);
					break;
				}
				break;
			case M13:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M13(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M13(objMessageMap);
					break;
				}
				break;
			case M14:
				switch(enumMessageCode)
				{
				case MFK:
					objMessage = new MFK_M14(objMessageMap);
					break;
				}
				break;
			case M15:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M15(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M15(objMessageMap);
					break;
				}
				break;
			case M16:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M16(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M16(objMessageMap);
					break;
				}
				break;
			case M17:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M17(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M17(objMessageMap);
					break;
				}
				break;
			case M18:
				switch(enumMessageCode)
				{
				case MFN:
					objMessage = new MFN_M18(objMessageMap);
					break;
				case MFK:
					objMessage = new MFK_M18(objMessageMap);
					break;
				}
				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.M02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M04)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M05)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M06)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M07)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M08)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M09)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M10)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M11)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M12)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M13)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M14)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M15)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M16)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M17)
				|| enumTriggerEvent.equals(EnumTriggerEvent.M18)	
		)
			return true;
		else 
			return false;
	}

	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.MFR || enumMessageCode == EnumMessageCode.MFK ||
				enumMessageCode == EnumMessageCode.MFN || enumMessageCode == EnumMessageCode.ACK ||
				enumMessageCode == EnumMessageCode.MFQ)
			return true;

		return false;
	}
	//This method returns the Object of MessageMap.
	private IMap getMessageMap(EnumMessageCode enumMessageCode,EnumTriggerEvent enumTriggerEvent)
	{
		IMap objMessageMap =null;
		if(enumMessageCode == EnumMessageCode.ACK)
		{
			objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
		}
		else
		{
			switch(enumMessageCode)
			{
			case MFN:
				switch(enumTriggerEvent)
				{
				case M02:
				case M04:
				case M05:
				case M06:
				case M07:
				case M08:
				case M09:
				case M10:
				case M11:
				case M12:
				case M13:
				case M15:
				case M16:
				case M17:
				case M18:
					return	objMessageMapReader.getMessageMap(enumMessageCode,enumTriggerEvent);
				}
				break;
			case MFK:
				switch(enumTriggerEvent)
				{
				case M02:
				case M04:
				case M05:
				case M06:
				case M07:
				case M08:
				case M09:
				case M10:
				case M11:
				case M12:
				case M13:
				case M14:
				case M15:
				case M16:
				case M17:
				case M18:
					return objMessageMapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.M01);
				}
				
			}
		}
		return objMessageMap;
	}
}
