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

/**
 * This abstract Class represents Presentation Context Item.
 * Both RQ and AC Presentation context item are child of Presentation context item.
 */
public abstract class PresContextItem extends AssociationItem 
{
	/** The presentation context id. */
	byte bPresContextId;

	/**
	 * Instantiates a new presentation context item.
	 * @param bItemType byte value representing item type.
	 */
	public PresContextItem(byte bItemType)
	{
		super(bItemType);
	}
	
	/**
	 * Instantiates a new presentation context item.
	 * @param bItemType byte value representing item type.
	 */
	public PresContextItem(byte bItemType,byte bContextID)
	{
		super(bItemType);
		this.bPresContextId = bContextID;
	}

	/**
	 * Gets the presentation context id.
	 * @return byte - byte value of presentation context id.
	 */
	public byte getPresContextId() 
	{
		return bPresContextId;
	}

	/**
	 * Sets the presentation context id.
	 * @param bPresContextId byte value of presentation context id.
	 */
	public void setPresContextId(byte bPresContextId) 
	{
		this.bPresContextId = bPresContextId;
	}

	public short getItemLength()
	{
		return 1 + 3;
	}

	/**
	 * Parses the Presentation Context Item.
	 * @param objIDicomStream Stream on which Dicom Data resides.
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws ParseException Signals that Parse exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream)throws IOException,ParseException
	{
		super.parse(objIDicomStream);
		objIDicomStream.readShort();
		bPresContextId = objIDicomStream.readByte();
	}

	/**
	 * This method writes the Data Value to the given Stream.
	 * @param objIDicomStream object of the stream onto which the Presentation Context Item is to be  written.
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SerializeException  Signals that Serialization exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException,SerializeException
	{
		super.serialize(objIDicomStream);
		objIDicomStream.writeShort(getItemLength());
		objIDicomStream.writeByte(bPresContextId);
	}

	/**
	 * Populate Presentation context Item.
	 * @param bContextID byte value of Presentation context id.
	 */
	public void populate(byte bContextID)
	{
		bPresContextId = bContextID;
	}
}
