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

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

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


	/**
	 * 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 S01:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S01(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S01(objMessageMap);
					break;
				}
				break;
			case S02:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S02(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S02(objMessageMap);
					break;
				}
				break;
			case S03:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S03(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S03(objMessageMap);
					break;
				}
				break;
			case S04:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S04(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S04(objMessageMap);
					break;
				}
				break;
			case S05:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S05(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S05(objMessageMap);
					break;
				}
				break;
			case S06:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S06(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S06(objMessageMap);
					break;
				}
				break;
			case S07:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S07(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S07(objMessageMap);
					break;
				}
				break;
			case S08:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S08(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S08(objMessageMap);
					break;
				}
				break;
			case S09:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S09(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S09(objMessageMap);
					break;
				}
				break;
			case S10:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S10(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S10(objMessageMap);
					break;
				}
				break;
			case S11:
				switch(enumMessageCode)
				{
				case SRM:
					objMessage = new SRM_S11(objMessageMap);
					break;
				case SRR:
					objMessage = new SRR_S11(objMessageMap);
					break;
				}
				break;
			case S12:
				objMessage = new SIU_S12(objMessageMap);
				break;
			case S13:
				objMessage = new SIU_S13(objMessageMap);
				break;
			case S14:
				objMessage = new SIU_S14(objMessageMap);
				break;
			case S15:
				objMessage = new SIU_S15(objMessageMap);
				break;
			case S16:
				objMessage = new SIU_S16(objMessageMap);
				break;
			case S17:
				objMessage = new SIU_S17(objMessageMap);
				break;
			case S18:
				objMessage = new SIU_S18(objMessageMap);
				break;
			case S19:
				objMessage = new SIU_S19(objMessageMap);
				break;
			case S20:
				objMessage = new SIU_S20(objMessageMap);
				break;
			case S21:
				objMessage = new SIU_S21(objMessageMap);
				break;
			case S22:
				objMessage = new SIU_S22(objMessageMap);
				break;
			case S23:
				objMessage = new SIU_S23(objMessageMap);
				break;
			case S24:
				objMessage = new SIU_S24(objMessageMap);
				break;

			case S26:
				objMessage = new SIU_S26(objMessageMap);
				break;
			case S27:
				objMessage = new SIU_S27(objMessageMap);
			}
		}
		return objMessage;
	}

	private IMap getMessageMap(EnumMessageCode enumMessageCode, EnumTriggerEvent enumTriggerEvent)
	{
		IMap objMessageMap = null;
		
		switch(enumTriggerEvent)
		{
		case S01:
		case S02:
		case S03:
		case S04:
		case S05:
		case S06:
		case S07:
		case S08:
		case S09:
		case S10:
		case S11:
			switch(enumMessageCode)
			{
			case SRM:
				objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.S01);
				break;
			case SRR:
				objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.S01);
				break;
			}
			break;
		case S12 :
		case S13 :
		case S14 :
		case S15 :
		case S16 :
		case S17 :
		case S18 :
		case S19 :
		case S20 :
		case S21 :
		case S22 :
		case S23 :
		case S24 :
		case S26 :
		case S27 :
			objMessageMap = objMessamapReader.getMessageMap(enumMessageCode,EnumTriggerEvent.S12);
			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.S01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S03)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S04)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S05)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S06)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S07)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S08)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S09)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S10)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S11)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S12)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S13)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S14)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S15)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S16)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S17)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S18)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S19)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S20)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S21)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S22)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S23)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S24)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S26)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S27)
		)
			return true;
		else 
			return false;
	}
	
	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.ACK || enumMessageCode == EnumMessageCode.SRR ||
				enumMessageCode == EnumMessageCode.SQR || enumMessageCode == EnumMessageCode.SRM ||
				enumMessageCode == EnumMessageCode.SQM || enumMessageCode == EnumMessageCode.SIU)
			return true;
		
		return false;
	}
}
