package cdac.medinfo.sdk.hl7282.hl7model.messages.materialsmanagement;

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

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

	/**
	 * 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 S28:
				objMessage = new SLR_S28(objMessageMap);
				break;
			case S29:
				objMessage = new SLR_S29(objMessageMap);
				break;
			case S30: 
				objMessage = new STI_S30(objMessageMap);
				break;
			case S31:
				objMessage = new SDR_S31(objMessageMap);
				break;
			case S32:
				objMessage = new SMD_S32(objMessageMap);
				break;
			case S33:
				objMessage = new STC_S33(objMessageMap);
				break;
			case S34:
				objMessage = new SLN_S34(objMessageMap);
				break;
			case S35:
				objMessage = new SLN_S35(objMessageMap);
				break;
			case S36:
				objMessage = new SDN_S36(objMessageMap);
				break;
			case S37:
				objMessage = new SCN_S37(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.S28)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S29)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S30)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S31)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S32)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S33)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S34)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S35)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S36)
				|| enumTriggerEvent.equals(EnumTriggerEvent.S37)
		)
			return true;
		else 
			return false;
	}

	/**
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.SLR || enumMessageCode == EnumMessageCode.STI 
				|| enumMessageCode == EnumMessageCode.SDR || enumMessageCode == EnumMessageCode.SMD ||
				enumMessageCode == EnumMessageCode.STC || enumMessageCode == EnumMessageCode.SLN || 
				enumMessageCode == EnumMessageCode.SDN || enumMessageCode == EnumMessageCode.SCN )
			return true;
		
		return false;
	}
	
	
	private IMap getMessageMap(EnumMessageCode enumMessageCode, EnumTriggerEvent enumTriggerEvent)
    {
        IMap objMessageMap =null;
        if (enumMessageCode == EnumMessageCode.ACK)
        {
            objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
        }
        else
        {
            switch (enumTriggerEvent)
            {
                case S28:
                	 objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
                     break;
                case S29:
                	 objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, EnumTriggerEvent.S28);
                     break;
                case S30:
                	 objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.SLR, EnumTriggerEvent.S28);
                     break;
                case S31:
                 	 objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
                     break;
                case S32:
                	 objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.SDR, EnumTriggerEvent.S32);
                     break;
                case S33:
                	 objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
                	 break;
                case S34:
                  	 objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.SLR, EnumTriggerEvent.S28);
                     break;
                case S35:
                 	 objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.SLR, EnumTriggerEvent.S28);
                    break;
                case S36:
                    objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.SDR, EnumTriggerEvent.S31);
                    break;
                case S37:
                    objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.SDR, EnumTriggerEvent.S32);
                    break;
            }
        }
        return objMessageMap;
    }

}
