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

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

	/**
	 * Creates Object of FinancialMessageFactory.
	 * @return FinancialMessageFactory object of FinancialMessageFactory
	 */
	public static FinancialMessageFactory createInstance()
	{
		if(objFinancialMessageFactory == null)
			objFinancialMessageFactory = new FinancialMessageFactory();
		objMessageMapReader = MessageMapReader.createInstance(EnumHL7System.HL7_SYSTEM_FINANCIALMANAGEMENT);
		return objFinancialMessageFactory;

	}

	/**
	 * 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 P01:
				objMessage = new BAR_P01(objMessageMap);
				break;
			case P02:
				objMessage = new BAR_P02(objMessageMap);
				break;
			case P05:
				objMessage = new BAR_P05(objMessageMap);
				break;
			case P06:
				objMessage = new BAR_P06(objMessageMap);
				break;
			case P10:
				objMessage = new BAR_P10(objMessageMap);
				break;
			case P12:
				objMessage = new BAR_P12(objMessageMap);
				break;
			case P03:
				objMessage = new DFT_P03(objMessageMap);
				break;
			case P11:
				objMessage = new DFT_P11(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.P01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P05)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P06)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P10)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P11)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P03)
				|| enumTriggerEvent.equals(EnumTriggerEvent.P12)
		)
			return true;
		else
			return false;
	}

	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.DFT || enumMessageCode == EnumMessageCode.BAR ||
				enumMessageCode == EnumMessageCode.DSR || enumMessageCode == EnumMessageCode.ACK ||
				enumMessageCode == EnumMessageCode.QRY)
			return true;
		
		return false;
	}
}
