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


/**
 * This Class represents Associate AC PDU.
 */
public class AssociateACPDU extends AssociateRQOrACPDU 
{

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

	/** The user info item. */
	UserInfoItem objUserInfoItem = null;

	/**
	 * Instantiates a new Associate AC PDU.
	 */
	public AssociateACPDU()
	{
		super();
		objACPresContextItemCollection = new PairedCollection<Byte, ACPresContextItem>();
	}

	/**
	 * Gets the AC pres context item collection.
	 * @return ISingleCollection<ACPresContextItem> - AC presentation context item collection.
	 */
	public IPairedCollection<Byte, ACPresContextItem> getACPresContextItemCollection() 
	{
		return objACPresContextItemCollection;
	}

	/**
	 * Sets the AC pres context item collection.
	 * @param objACPresContextItemCollection Collection of AC Presentation Context Item.
	 */
	public void setACPresContextItemCollection(IPairedCollection<Byte, ACPresContextItem> objACPresContextItemCollection) 
	{
		this.objACPresContextItemCollection = objACPresContextItemCollection;
	}

	/**
	 * Gets the user info item.
	 * @return UserInfoItem - instance of user info item.
	 */
	public UserInfoItem getUserInfoItem()
	{
		return objUserInfoItem;
	}

	/**
	 * Sets user info item.
	 * @param objUserInfoItem object of User Info Item.
	 */
	public void setUserInfoItem(UserInfoItem objUserInfoItem) 
	{
		this.objUserInfoItem = objUserInfoItem;
	}

	/**
	 * Gets the length of  Associate RQ or AC PDU.
	 *  @return int - value of Associate AC PDU length.
	 */
	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(ACPresContextItem objTempACPresContextItem : objACPresContextItemCollection)
		{
			iItemlength += (short)(objTempACPresContextItem.getItemLength()+ EnumLength.ITEM_HEADER_LENGTH.getShortValue());
		}

		if(objUserInfoItem != null)
			iItemlength += (short)(EnumLength.ITEM_HEADER_LENGTH.getShortValue() + objUserInfoItem.getItemLength());

		return iPDUCommonsLength + iItemlength;
	}

	/**
	 * Parses Associate AC PDU.
	 * @param objIDicomStream Dicom Stream on which Dicom Data resides.
	 * @throws IOException Signals that an IO Exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream)throws IOException
	{
		super.parse(objIDicomStream);

		byte iItemTypeValue;

		//Parsing Multiple Items of PRESENTATION CONTEXT
		objIDicomStream.mark(1);
		iItemTypeValue = objIDicomStream.readByte();
		if(iItemTypeValue == EnumItemType.AC_PRESENTATION_CONTEXT_ITEM.getValue())
		{
			do
			{
				objIDicomStream.reset();
				ACPresContextItem objRSPPresContextItem = new ACPresContextItem();
				objRSPPresContextItem.parse(objIDicomStream);
				objACPresContextItemCollection.put(objRSPPresContextItem.getPresContextId(), objRSPPresContextItem);
				objIDicomStream.mark(1);
				iItemTypeValue = objIDicomStream.readByte();
			}while((iItemTypeValue == EnumItemType.AC_PRESENTATION_CONTEXT_ITEM.getValue()));
			objIDicomStream.reset();
		}
		objIDicomStream.mark(1);
		byte iTempItemValue = objIDicomStream.readByte();
		if(iTempItemValue == EnumItemType.USER_INFORMATION_ITEM.getValue())
		{
			objUserInfoItem = new UserInfoItem();
			objIDicomStream.reset();
			objUserInfoItem.parse(objIDicomStream);
		}
	}

	/**
	 * This method writes the Data Value to the given Stream.
	 * @param objIDicomStream object of stream onto which this PDU is to be written.
	 * @throws IOException Signals that IO Exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException
	{
		super.serialize(objIDicomStream);
		//Serializing Multiple Items of PRESENTATION CONTEXT
		for(ACPresContextItem objACPresContextItem : objACPresContextItemCollection)
		{
			objACPresContextItem.serialize(objIDicomStream);
		}
		if(objUserInfoItem != null)
			objUserInfoItem.serialize(objIDicomStream);
	}
}
