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

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

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


	/**
	 * 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 U01:
				objMessage = new ESU_U01(objMessageMap);
				break;
			case U02:
				objMessage = new ESR_U02(objMessageMap);
				break;
			case U03:
				objMessage = new SSU_U03(objMessageMap);
				break;
			case U04:
				objMessage = new SSR_U04(objMessageMap);
				break;
			case U05:
				objMessage = new INU_U05(objMessageMap);
				break;
			case U06:
				objMessage = new INR_U06(objMessageMap);
				break;
			case U07:
				objMessage = new EAC_U07(objMessageMap);
				break;
			case U08:
				objMessage = new EAR_U08(objMessageMap);
				break;
			case U09:
				objMessage = new EAN_U09(objMessageMap);
				break;
			case U10:
				objMessage = new TCU_U10(objMessageMap);
				break;
			case U11:
				objMessage = new TCR_U11(objMessageMap);
				break;
			case U12:
				objMessage = new LSU_U12(objMessageMap);
				break;
			case U13:
				objMessage = new LSR_U13(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.U01)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U02)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U03)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U04)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U05)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U06)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U07)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U08)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U09)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U10)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U11)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U12)
				|| enumTriggerEvent.equals(EnumTriggerEvent.U13)
		)
			return true;
		else 
			return false;
	}
	
	/*
	 * Checks whether Message Code specified in Enumeration is Supported.
	 */
	boolean isCodeSupported(EnumMessageCode enumMessageCode)
	{
		if(enumMessageCode == EnumMessageCode.ESU || enumMessageCode == EnumMessageCode.ESR || enumMessageCode == EnumMessageCode.SSU || enumMessageCode == EnumMessageCode.SSR ||
				enumMessageCode == EnumMessageCode.INU || enumMessageCode == EnumMessageCode.INR || enumMessageCode == EnumMessageCode.EAC || enumMessageCode == EnumMessageCode.EAR ||
				enumMessageCode == EnumMessageCode.EAN || enumMessageCode == EnumMessageCode.TCU || enumMessageCode == EnumMessageCode.TCR || enumMessageCode == EnumMessageCode.LSU ||
				enumMessageCode == EnumMessageCode.LSR || enumMessageCode == EnumMessageCode.ACK)
			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 U01:
                case U02:
                case U03:
                case U04:
                case U05:
                case U06:
                case U07:
                case U08:
                case U09:
                    objMessageMap = objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
                    break;
                case U10:
                case U11:
                    objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.TCU, EnumTriggerEvent.U10);
                    break;
                case U12:
                case U13:
                    objMessageMap = objMessageMapReader.getMessageMap(EnumMessageCode.LSU, EnumTriggerEvent.U12);
                    break;
            }
        }
        return objMessageMap;
    }
}
