package cdac.medinfo.sdk.hl7282.hl7net.auxiliaryprotocol.pubsub;

import java.util.Locale;

import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.baselibs.segments.QID;
import cdac.medinfo.sdk.hl7282.baselibs.segments.RCP;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegment;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidMessageException;
import cdac.medinfo.sdk.hl7282.hl7net.commons.IPublishAgent;
import cdac.medinfo.sdk.hl7282.utils.EnumMessageCode;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.EnumSubscriptionType;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Logger;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;
import cdac.medinfo.sdk.hl7282.utils.StandardMethods;

/**
 * The class PublishAgent is responsible for publishing the HL7 Messages to registered 
 * clients by matching UserParameters and response messagetype sent by subscriber to Publish Server.
 */
public class PublishAgent implements IPublishAgent
{		

	/**
	 * This will check that recieved message is subscription message or not.
	 * @param objSegmentSet object of Segment Set received.
	 * @return EnumSubscriptionType - true if it is subscription message yes else false.
	 */
	public EnumSubscriptionType getSubscriptionType(ISegmentSet objSegmentSet)
	{
		MSH objMSHSegment = null;
		QID objQIDSegment = null;
		RCP objRCPSegment =null;
		ISingleCollection<ISegment> objSegmentCollection = objSegmentSet.getList();
		if(objSegmentCollection!= null && objSegmentCollection.size()>0)
		{
			int iSegSetSize = objSegmentCollection.size();
			boolean isMessageHeaderSet = false;
			for(int iRead=0;iRead<iSegSetSize;iRead++)
			{
				ISegment objSegment = null;
				objSegment = objSegmentCollection.elementAt(iRead);
				if(objSegment == null)
					throw new InvalidMessageException(this.getClass().getSimpleName());
				EnumSegments enumSegments = (EnumSegments) objSegment.getName();
				switch(enumSegments)
				{
				case HL7_SEG_MSH:
					if(false == isMessageHeaderSet)
					{
						objMSHSegment = (MSH)objSegment;
						isMessageHeaderSet = true;
					}
					else
						throw new InvalidMessageException(this.getClass().getSimpleName());
					break;
				case HL7_SEG_QID:
					objQIDSegment = (QID)objSegment;
					break;
				case HL7_SEG_RCP:
					objRCPSegment = (RCP)objSegment;
					break;
				}
			}
		}
		// check for cancel subscription message.
		if(objQIDSegment!=null && EnumMessageCode.QSX == StandardMethods.getEnumMessageCode((String)objMSHSegment.getMessageType().getMessageCode().getValue()))
		{
			//Status Logging
			if(HL7Config.createInstance().isLoggingOn())
			{
				String strRecievedMessage = ResourceBundleWrapper.getValue("exceptions", "PubSub_CANCEL_SUBSCRIPTION",new Locale(HL7Config.createInstance().getLocale()));
				HL7Logger objHLogger = HL7Logger.getLogger();
				objHLogger.log(strRecievedMessage);
			}
			return EnumSubscriptionType.CANCEL_SUBSCRIPTION;
		}
		//If any event or Query is with RCP segment and its field is ModifyIndicator 
		//which is set to "M" or "N".
		if(objRCPSegment!=null)
		{
			if(objRCPSegment.getModifyIndicator().getValue().equals(EnumSubscriptionType.NEW_SUBSCRIPTION.getValue()))
			{
				//Status Logging
				if(HL7Config.createInstance().isLoggingOn())
				{
					String strRecievedMessage = ResourceBundleWrapper.getValue("exceptions", "PubSub_NEW_SUBSCRIPTION",new Locale(HL7Config.createInstance().getLocale()));
					HL7Logger objHLogger = HL7Logger.getLogger();
					objHLogger.log(strRecievedMessage);
				}
				return EnumSubscriptionType.NEW_SUBSCRIPTION;
			}
			else if(objRCPSegment.getModifyIndicator().getValue().equals(EnumSubscriptionType.MODIFIED_SUBSCRIPTION.getValue()))
			{
				//Status Logging
				if(HL7Config.createInstance().isLoggingOn())
				{
					String strRecievedMessage = ResourceBundleWrapper.getValue("exceptions", "PubSub_MODIFIED_SUBSCRIPTION",new Locale(HL7Config.createInstance().getLocale()));
					HL7Logger objHLogger = HL7Logger.getLogger();
					objHLogger.log(strRecievedMessage);
				}
				return EnumSubscriptionType.MODIFIED_SUBSCRIPTION;
			}
		}
		return null;
	}
}
