/*******************************************************************************
 *  Copyright 2017-2018 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.dcm30_04.services.association;

import java.util.Locale;

import cdac.medinfo.sdk.dcm30_04.enums.EnumPDUType;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumRejectReason;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumRejectResult;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumRejectSource;
import cdac.medinfo.sdk.dcm30_04.services.pdu.AssociateRJPDU;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * A-AssociateRJ shall be used in rejection of Association request.
 * This Class implements the setter and getter for required attributes of AssociationRJ.  
 */
public class AssociateRJ implements IDIMSEAssociation 
{

	/** enum value of rejection result. */
	EnumRejectResult enumResult;

	/** enum value of rejection source. */
	EnumRejectSource enumSource;

	/** enum value of rejection reason. */
	EnumRejectReason enumReason;

	/**
	 * Retrieves the PDU Type.
	 * @return EnumPDUType - Enum value of PDU type.
	 */
	public EnumPDUType getPDUType()
	{
		return EnumPDUType.ASSOCIATE_RJ;
	}

	/**
	 * Retrieves the reason of rejection.
	 * @return enumReason - value of reason of rejection
	 */
	public EnumRejectReason getReason() 
	{
		return enumReason;
	}

	/**
	 * Sets the reason of rejection.
	 * @param enumReason enum value of reason of rejection
	 */
	public void setReason(EnumRejectReason enumReason) 
	{
		this.enumReason = enumReason;
	}

	/**
	 * Retrieves the result of rejection.
	 * @return EnumRejectResult - value of result of rejection.
	 */
	public EnumRejectResult getResult()
	{
		return enumResult;
	}

	/**
	 * Sets the result of rejection.
	 * @param enumResult enum value of result of rejection.
	 */
	public void setResult(EnumRejectResult enumResult) 
	{
		this.enumResult = enumResult;
	}

	/**
	 * Retrieves the source of rejection
	 * @return EnumRejectSource - value of source of rejection
	 */
	public EnumRejectSource getSource() 
	{
		return enumSource;
	}

	/**
	 * Sets the source of rejection
	 * @param enumSource enum value of source of rejection
	 */
	public void setSource(EnumRejectSource enumSource) 
	{
		this.enumSource = enumSource;
	}

	/**
	 * Initializes the AssociateRJ according to the given AssociateRJPDU
	 * @param objAssociateRJPDU instance of AssociateRJPDU
	 */
	public void setAssociateRJPDU(AssociateRJPDU objAssociateRJPDU)
	{
		byte bReason = objAssociateRJPDU.getReason();
		byte bResult = objAssociateRJPDU.getResult();
		byte bSource = objAssociateRJPDU.getSource();

		switch(bSource)
		{
		case (byte)1:
		{
			enumSource = EnumRejectSource.SERVICE_USER;
			switch(bReason)
			{
			case (byte)1:
				enumReason = EnumRejectReason.NO_REASON;
			break;
			case (byte)2:
				enumReason = EnumRejectReason.APPLICATION_CONTEXT_NOT_SUPPORTED;
			break;
			case (byte)3:
				enumReason = EnumRejectReason.CALLING_AE_TITLE_NOT_RECOGNIZED;
			break;
			case (byte)7:
				enumReason = EnumRejectReason.CALLED_AE_TITLE_NOT_RECOGNIZED;
			}
			break;
		}
		case (byte)2:
		{
			enumSource = EnumRejectSource.SERVICE_PROVIDER_ACSE;
			switch(bReason)
			{
			case (byte)1:
				enumReason = EnumRejectReason.NO_REASON;
			break;
			case (byte)2:
				enumReason = EnumRejectReason.SP_ACSE_PROTOCOL_VERSION_NOT_SUPPORTED;
			}
			break;
		}
		case (byte)3:
		{
			enumSource = EnumRejectSource.SERVICE_PROVIDER_PRESENTATION;
			switch(bReason)
			{
			case (byte)1:
				enumReason = EnumRejectReason.SP_PRESENTATION_TEMP_CONGESTION;
			break;
			case (byte)2:
				enumReason = EnumRejectReason.SP_PRESENTATION_LIMIT_EXCEEDED;
			}
			break;
		}
		}

		switch(bResult)
		{
		case (byte)1:
			enumResult = EnumRejectResult.REJECT_PERMANENT;
		break;
		case (byte)2:
			enumResult = EnumRejectResult.REJECT_TRANSIENT;
		}
	}

	/**
	 * Initializes the AssociateRJ PDU 
	 * @return AssociateRJPDU - instance of AssociateRJ PDU
	 */
	public AssociateRJPDU getAssociateRJPDU()
	{
		AssociateRJPDU objAssociateRJPDU = new AssociateRJPDU();
		objAssociateRJPDU.setPDUType(EnumPDUType.ASSOCIATE_RJ);
		objAssociateRJPDU.setReason(this.getReason());
		objAssociateRJPDU.setResult(this.getResult());
		objAssociateRJPDU.setSource(this.getSource());
		return objAssociateRJPDU;
	}
	
	/**
	 * Validates Association Reject primitive for mandatory attributes
	 * @return boolean - shows status as true or false
	 * @throws InvalidAssociationPrimitiveException if validation fails
	 */
	public boolean validate()throws InvalidAssociationPrimitiveException
	{
		if(enumReason == null || enumResult == null || enumSource == null)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("exceptions", "AssociationException.InvalidAttribute", new Locale(DicomConfig.createInstance().getLocale())));
			}
			throw new InvalidAssociationPrimitiveException(ResourceBundleWrapper.getValue("exceptions", "AssociationException.InvalidAttribute", new Locale(DicomConfig.createInstance().getLocale())));
		}
		
		return true;
	}
}