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

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.exceptions.SerializeException;
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;




/**
 * This Class represents AssociateRJPDU.
 * An A-ASSOCIATE-RJ PDU shall be made of a sequence of mandatory fields.
 */
public class AssociateRJPDU extends PDU 
{


	/** The byte value of  result. */
	byte bResult;

	/** The byte value of  source. */
	byte bSource;

	/** The byte value of reason. */
	byte bReason;

	/**
	 * Gets the reason.
	 * @return byte - value of reason
	 */
	public byte getReason() 
	{
		return bReason;
	}

	/**
	 * Sets the reason.
	 * This field shall contain an integer value encoded as an unsigned binary
	 * 1 - no-reason-given
	 * 2 - application-context-name-not-supported
	 * 3 - calling-AE-title-not-recognized
	 * 4-6 - reserved
	 * 7 - called-AE-title-not-recognized
	 * 8-10 - reserved
	 * @param enumRejectReason byte value of the reason.
	 */
	public void setReason(EnumRejectReason enumRejectReason) 
	{
		this.bReason = enumRejectReason.getValue();
	}

	/**
	 * Gets the result.
	 * @return byte - byte value of the result.
	 */
	public byte getResult()
	{
		return bResult;
	}

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

	/**
	 * Gets the source.
	 * @return byte - value of source.
	 */
	public byte getSource() 
	{
		return bSource;
	}

	/**
	 * Sets the source. This Source field shall contain an integer value encoded as an
	 * unsigned binary number. One of the following values shall be used:
	 * 1 - DICOM UL service-user
	 * 2 - DICOM UL service-provider (ACSE related function)
	 * 3 - DICOM UL service-provider (Presentation related function)
	 * @param enumRejectSource enum value of source
	 */ 
	public void setSource(EnumRejectSource enumRejectSource) 
	{
		this.bSource = enumRejectSource.getValue();
	}

	/**
	 * This function gets the length of Associate RJ PDU
	 * @return int - Integer value of length of Associate RJ PDU.
	 */
	public int getLength() 
	{
		return (EnumLength.RESERVED_BYTE.getShortValue() * 1 
				+ EnumLength.RESULT_OR_REASON_LENGTH.getShortValue()
				+ EnumLength.RESULT_OR_REASON_LENGTH.getShortValue()
				+EnumLength.SOURCE_LENGTH.getIntValue());
	}

	/**
	 * This method parses this PDU from the given Stream.
	 * @param objIDicomStream Stream on which the DICOM data is available.
	 * @throws ParseException Signals that parse exception is occurred.
	 * @throws IOException Signals that IO Exception is occurred.
	 */
	public void parse(IDicomStream objIDicomStream) throws IOException, ParseException 
	{
		super.parse(objIDicomStream);
		objIDicomStream.readByte();
		bResult = objIDicomStream.readByte();
		bSource = objIDicomStream.readByte();
		bReason = objIDicomStream.readByte();
	}

	/**
	 * This method serialize this PDU from the given Stream.
	 * @param objIDicomStream Stream on which the DICOM data is to be written.
	 * @throws IOException Signals that IOException is occurred.
	 * @throws SerializeException Signals that Serialize Exception occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException, SerializeException 
	{
		super.serialize(objIDicomStream);
		objIDicomStream.writeByte((byte)0);
		objIDicomStream.writeByte(bResult);
		objIDicomStream.writeByte(bSource);
		objIDicomStream.writeByte(bReason);
	}
}
