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

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 PersonalMgmtMessageFactory implements IMessageFactory  
{
	private static PersonalMgmtMessageFactory objPersonalMgmtMessageFactory = null;
	private static IMessageMapReader objMessamapReader = null;
	private PersonalMgmtMessageFactory()
	{}

	/**
	 * Creates Object of PersonalMgmtMessageFactory.
	 * @return PersonalMgmtMessageFactory object of PersonalMgmtMessageFactory
	 */
	public static PersonalMgmtMessageFactory createInstance()
	{
		if(objPersonalMgmtMessageFactory == null)
		{
			objMessamapReader = MessageMapReader.createInstance(EnumHL7System.HL7_SYSTEM_PERSONNELMANAGEMENT);
			objPersonalMgmtMessageFactory = new PersonalMgmtMessageFactory();
		}
		return objPersonalMgmtMessageFactory;
	}

	/**
	 * 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 B01 : 
				objMessage = new PMU_B01(objMessageMap);
				break;
			case B02 : 
				objMessage = new PMU_B02(objMessageMap);
				break;
			case B03 : 
				objMessage = new PMU_B03(objMessageMap);
				break;
			case B04 : 
				objMessage = new PMU_B04(objMessageMap);
				break;
			case B05 : 
				objMessage = new PMU_B05(objMessageMap);
				break;
			case B06 : 
				objMessage = new PMU_B06(objMessageMap);
				break;
			case B07 : 
				objMessage = new PMU_B07(objMessageMap);
				break;
			case B08 : 
				objMessage = new PMU_B08(objMessageMap);
				break;
			case Q25:
				objMessage = new QBP_Q25(objMessageMap);
				break;
			case K25:
				objMessage = new RSP_K25(objMessageMap);
				break;
			}
		}
		return objMessage;
	}

	/**
	 * 
	 * @param enumMessageCode
	 * @param enumTriggerEvent
	 */
	private IMap getMessageMap(EnumMessageCode enumMessageCode, EnumTriggerEvent enumTriggerEvent)
	{
		IMap objMessageMap = null;
		
		switch(enumTriggerEvent)
		{
		case B01:
		case B02 :
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.B01);
			break;
		case B03:
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.B03);
			break;
		case B04:
		case B05:
		case B06:
			objMessageMap =objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.B04);
			break;
		case B07: 
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.B07);
			break;
		case B08:
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.B08);
			break;
		case Q25:
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.Q21);
			break;
		case K25 : 
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.K25);
			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.B01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B03)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B04)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B05)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B06)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B07)
				|| enumTriggerEvent.equals(EnumTriggerEvent.B08)
				|| enumTriggerEvent.equals(EnumTriggerEvent.K25)
				|| enumTriggerEvent.equals(EnumTriggerEvent.Q25)
		)
			return true;
		else 
			return false;
	}
	
	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.ACK || enumMessageCode == EnumMessageCode.RSP ||
				enumMessageCode == EnumMessageCode.PMU || enumMessageCode == EnumMessageCode.QBP)
			return true;
		
		return false;
	}
}
