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

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 ClaimsAndReimbursementMessageFactory implements IMessageFactory
{

	private static ClaimsAndReimbursementMessageFactory objEClaimsMessageFactory = null;
	private static IMessageMapReader objMessageMapReader = null;
	private ClaimsAndReimbursementMessageFactory()
	{}
	
	/**
	 * Creates Object of EClaimsMessageFactory.
	 * @return objEClaimsMessageFactory object of EClaimsMessageFactory.
	 */
	public static ClaimsAndReimbursementMessageFactory createInstance()
	{
		if(objEClaimsMessageFactory == null)
			objEClaimsMessageFactory = new ClaimsAndReimbursementMessageFactory();
		objMessageMapReader = MessageMapReader.createInstance(EnumHL7System.HL7_SYSTEM_CLAIMSANDREIMBURSEMENT);
		return objEClaimsMessageFactory;
	}
	
	/**
	 * 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 E01:
				objMessage = new EHC_E01(objMessageMap);
				break;
			case E02:
				objMessage = new EHC_E02(objMessageMap);
				break;
			case E03:
				switch(enumMessageCode)
				{
				case QBP:
					objMessage = new QBP_E03(objMessageMap);
					break;
				case RSP:
					objMessage = new RSP_E03(objMessageMap);
					break;
				}
				break;
			case E04:
				objMessage = new EHC_E04(objMessageMap);
				break;
			case E10:
				objMessage = new EHC_E10(objMessageMap);
				break;
			case E12:
				objMessage = new EHC_E12(objMessageMap);
				break;
			case E13:
				objMessage = new EHC_E13(objMessageMap);
				break;
			case E15:
				objMessage = new EHC_E15(objMessageMap);
				break;
			case E20:
				objMessage = new EHC_E20(objMessageMap);
				break;
			case E21:
				objMessage = new EHC_E21(objMessageMap);
				break;
			case E22:
				switch(enumMessageCode)
				{
				case QBP:
					objMessage = new QBP_E22(objMessageMap);
					break;
				case RSP:
					objMessage = new RSP_E22(objMessageMap);
					break;
				}
				break;
			case E24:
				objMessage = new EHC_E24(objMessageMap);
				break;
			default:
				break;
			}
		}
		return objMessage;
	}

	/**
	 * 
	 * @param enumMessageCode
	 * @param enumTriggerEvent
	 */
	private IMap getMessageMap(EnumMessageCode enumMessageCode, EnumTriggerEvent enumTriggerEvent)
	{
		IMap objMessageMap = null;
		
		switch(enumTriggerEvent)
		{
		case E01:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E02:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E03:
			switch(enumMessageCode)
			{
			case QBP:
				objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
				break;
			case RSP:
				objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
				break;
			}
			break;
		case E04:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E10:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E12:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E13:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E15:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E20:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E21:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		case E22:
			switch(enumMessageCode)
			{
			case QBP:
				objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
				break;
			case RSP:
				objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
				break;
			}
			break;
		case E24:
			objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
			break;
		default:
			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.E01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E03)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E04)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E10)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E12)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E13)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E15)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E20)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E21)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E22)
				|| enumTriggerEvent.equals(EnumTriggerEvent.E24)
		)
			return true;
		else 
			return false;
	}

	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.EHC ||enumMessageCode == EnumMessageCode.RSP || enumMessageCode == EnumMessageCode.QBP)
			return true;
		
		return false;
	}
}
