/*******************************************************************************
 *  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.services.dicomnetcommons.EnumSCUSCPRole;




/**
 * This Class represents SCUSCPRoleSelectionItem. 
 * The SCP/SCU Role Selection Sub-Item shall be made of a sequence of mandatory fields.
 * This Sub-Item is optional and if supported, one or more SCP/SCU Role Selection Sub-Items may be present 
 * in the User Data Item of the A-ASSOCIATE-AC.
 */
public class SCUSCPRoleSelectionItem extends AssociationItem 
{

	/** The UID length. */
	short shUIDlength;

	/** The String SOP Class UID. */
	String strSOPclassuid;

	/** The enum SCU/SCP role. */
	EnumSCUSCPRole enumSCUSCPRole;

	/**
	 * Instantiates a new SCU/SCP role selection item.
	 */
	public SCUSCPRoleSelectionItem()
	{
		super(EnumItemType.USER_INFO_SCU_SCP_ROLE_SELECTION_ITEM.getValue());
	}

	/**
	 * Instantiates a new SCU/SCP role selection item.
	 * @param enumSCUSCPRole enum value representing the role i.e. SCU Role, SCP Role or both SCU and SCP role.
	 */
	public SCUSCPRoleSelectionItem(EnumSCUSCPRole enumSCUSCPRole)
	{
		super(EnumItemType.USER_INFO_SCU_SCP_ROLE_SELECTION_ITEM.getValue());
		this.enumSCUSCPRole = enumSCUSCPRole;
	}

	/**
	 * Gets the role.
	 * @return EnumSCUSCPRole - enum value representing the role i.e. SCU Role, SCP Role or both SCU and SCP role.
	 */
	public EnumSCUSCPRole getRole() 
	{
		return enumSCUSCPRole;
	}

	/**
	 * Sets the role.
	 * @param EnumSCUSCPRole enum value representing the role i.e. SCU Role, SCP Role or both SCU and SCP role.
	 */
	public void setRole(EnumSCUSCPRole EnumSCUSCPRole) 
	{
		this.enumSCUSCPRole = EnumSCUSCPRole;
	}

	/**
	 * Gets UID Length . The number of bytes from the first byte of the following field to the last byte of the SOP-class-uid field.
	 * @return short - value of UID Length.
	 */
	public short getUIDlength() 
	{
		return shUIDlength;
	}

	/**
	 * Sets the UID Length. The number of bytes from the first byte of the following field to the last byte of the SOP-class-uid field.
	 * @param shUIDlength short value of UID Length.
	 */
	public void setUIDlength(short shUIDlength) 
	{
		this.shUIDlength = shUIDlength;
	}

	/**
	 * Gets the the SOP Class UID or Meta SOP Class UID which may be used to identify the
	 * corresponding Abstract Syntax for which this Sub-Item pertains.
	 * @return String  - value of  SOP Class UID or Meta SOP Class UID .
	 */
	public String getSOPclassuid() 
	{
		return strSOPclassuid;
	}

	/**
	 * Sets SOP Class UID or Meta SOP Class UID which may be used to identify the
	 * corresponding Abstract Syntax for which this Sub-Item pertains.
	 * @param strSOPclassuid String value of SOP Class UID or Meta SOP Class UID .
	 */
	public void setSOPclassuid(String strSOPclassuid) 
	{
		this.strSOPclassuid = strSOPclassuid;
	}

	public short getItemLength() 
	{
		if(strSOPclassuid != null)
			return (short)(2 + strSOPclassuid.length() + (1 * 2));
		else
			return (short)(2 + (1 * 2));
	}

	/**
	 * Parses SCU/SCP Role Selection Item.
	 * @param objIDicomStream The stream containing Dicom Data.
	 * @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();
		byte bSCURole,bSCPRole;
		shUIDlength = objIDicomStream.readShort();
		strSOPclassuid = objIDicomStream.readString(shUIDlength);
		bSCURole = objIDicomStream.readByte();
		bSCPRole = objIDicomStream.readByte();
		if(bSCURole == 1 && bSCPRole == 1)
			enumSCUSCPRole = EnumSCUSCPRole.SCU_SCP_BOTH;
		else if(bSCURole == 1)
			enumSCUSCPRole = EnumSCUSCPRole.SCU_ONLY;
		else if(bSCPRole == 1)
			enumSCUSCPRole = EnumSCUSCPRole.SCP_ONLY;
		//else

		//throw new ParseException("SCU or SCP Role is not present");
	}

	/**
	 * This method writes the Data Value to the given Stream.
	 * @param objIDicomStream  object of the stream onto which the SCU/SCP Role Selection Item is to be written.
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws SerializeException Signals that Serialize exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException,SerializeException
	{
		super.serialize(objIDicomStream);
		objIDicomStream.writeShort(getItemLength());
		objIDicomStream.writeShort(shUIDlength);
		if(strSOPclassuid != null)
			objIDicomStream.writeString(strSOPclassuid);

		switch(enumSCUSCPRole)
		{
		case SCU_SCP_BOTH :
			objIDicomStream.writeByte((byte)1);
			objIDicomStream.writeByte((byte)1);
			break;
		case SCP_ONLY :
			objIDicomStream.writeByte((byte)1);
			objIDicomStream.writeByte((byte)0);
			break;
		case SCU_ONLY :
			objIDicomStream.writeByte((byte)0);
			objIDicomStream.writeByte((byte)1);
			break;
		}
	}
}
