/*******************************************************************************
 *  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.enums.EnumItemType;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.exceptions.SerializeException;
import cdac.medinfo.sdk.dcm30_04.utils.IPairedCollection;
import cdac.medinfo.sdk.dcm30_04.utils.PairedCollection;


/**
 * This class implements Association Request An A-ASSOCIATE-RQ PDU shall be made
 * of a sequence of mandatory fields followed by a variable length field The
 * variable field shall consist of one Application Context Item, one or more
 * Presentation Context Items, and one User Information Item.
 */
public class AssociateRQPDU extends AssociateRQOrACPDU 
{

//	/** The RQ presentation context Paired collection */
	IPairedCollection<Byte, RQPresContextItem> objRQPresContextItemCollection = null;

	/** The RQ User Info Item. */
	RQUserInfoItem objRQUserInfoItem = null;

	/**
	 * Instantiates a new Associate RQ PDU.
	 */
	public AssociateRQPDU()
	{
		super();
		objRQPresContextItemCollection = new PairedCollection<Byte, RQPresContextItem>();
	}

	/**
	 * Retrieves collection of RQPresContextItem.
	 * @return ISingleCollection<RQPresContextItem> - collection of RQPresContextItem.
	 */
	public IPairedCollection<Byte, RQPresContextItem> getRQPresContextItemCollection()
	{
		return objRQPresContextItemCollection;
	}

	/**
	 * Sets collection of RQPresContextItem.
	 * @param objRQPresContextItemCollection  collection of RQPresContextItem.
	 */
	public void setRQPresContextItemCollection(IPairedCollection<Byte, RQPresContextItem> objRQPresContextItemCollection) 
	{
		this.objRQPresContextItemCollection = objRQPresContextItemCollection;
	}

	/**
	 * Retrieves instance of Request User Info Item.
	 * @return RQUserInfoItem - instance of User Info Item
	 */
	public RQUserInfoItem getRQUserInfoItem() 
	{
		return objRQUserInfoItem;
	}

	/**
	 * Sets Request User Info Item.
	 * @param objRQUserInfoItem object of RQUserInfoItem
	 */
	public void setRQUserInfoItem(RQUserInfoItem objRQUserInfoItem) 
	{
		this.objRQUserInfoItem = objRQUserInfoItem;
	}

	/**
	 * Retrieves length of this PDU.
	 * @return short - value of length of this PDU.
	 */
	public int getLength() 
	{
		int iPDUCommonsLength = EnumLength.PROTOCOL_VERSION_LENGTH.getIntValue() +
		EnumLength.RESERVED_BYTE.getShortValue() * 2+ 
		EnumLength.CALLED_AE_TITLE_LENGTH.getIntValue() + 
		EnumLength.CALLING_AE_TITLE_LENGTH.getIntValue()+ 
		EnumLength.RESERVED_BYTE.getShortValue() * 32;

		if(objAppContextItem != null)
			iPDUCommonsLength += (short)(EnumLength.ITEM_HEADER_LENGTH.getShortValue() + objAppContextItem.getItemLength());

		short iItemLength = 0;

		for(RQPresContextItem objTempRQPresContextItemItem : objRQPresContextItemCollection)
		{
			iItemLength += (short)(objTempRQPresContextItemItem.getItemLength()+ EnumLength.ITEM_HEADER_LENGTH.getShortValue());
		}

		if(objRQUserInfoItem != null)
			iItemLength += (short)(EnumLength.ITEM_HEADER_LENGTH.getShortValue() + objRQUserInfoItem.getItemLength());

		return iPDUCommonsLength + iItemLength;
	}

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

		byte iItemTypeValue;

		//Parsing Multiple Items of PRESENTATION CONTEXT
		objIDicomStream.mark(1);
		iItemTypeValue = objIDicomStream.readByte();
		if(iItemTypeValue == EnumItemType.RQ_PRESENTATION_CONTEXT_ITEM.getValue())
		{
			int Length =0;
			do
			{
				objIDicomStream.reset();
				RQPresContextItem objRQPresContextItem = new RQPresContextItem();
				objRQPresContextItem.parse(objIDicomStream);
				objRQPresContextItemCollection.put(objRQPresContextItem.getPresContextId(), objRQPresContextItem);
				objIDicomStream.mark(1);
				iItemTypeValue = objIDicomStream.readByte();
				Length += objRQPresContextItem.getItemLength();
			}while((iItemTypeValue == EnumItemType.RQ_PRESENTATION_CONTEXT_ITEM.getValue()));

			objIDicomStream.reset();
		}
		objIDicomStream.mark(1);
		byte iTempItemValue = objIDicomStream.readByte();
		if(iTempItemValue == EnumItemType.USER_INFORMATION_ITEM.getValue())
		{
			objRQUserInfoItem = new RQUserInfoItem();
			objIDicomStream.reset();
			objRQUserInfoItem.parse(objIDicomStream);
		}
	}

	/**
	 * 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);
		//Serializing Multiple Items of PRESENTATION CONTEXT
		for(RQPresContextItem objRQPresContextItem : objRQPresContextItemCollection)
		{
			objRQPresContextItem.serialize(objIDicomStream);
		}
		if(objRQUserInfoItem != null)
			objRQUserInfoItem.serialize(objIDicomStream);
	}
}
