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



/**
 * The Class PDU.
 */
public abstract class PDU  
{

	/** The enum pdu type. */
	EnumPDUType enumPDUType ;

	/**
	 * Instantiates a new pDU.
	 */
	public PDU()
	{

	}
	/**
	 * This constructor sets the PDU type.
	 * @param enumPDUType   type of PDU
	 */
	public PDU(EnumPDUType enumPDUType)
	{
		this.enumPDUType = enumPDUType;
	}

	/**
	 * Retrieves PDU type.
	 * @return enumPDUType type of PDU
	 */
	public EnumPDUType getPDUType() 
	{
		return enumPDUType;
	}

	/**
	 * Sets PDU Type. 
	 * @param enumPDUType type of PDU
	 */
	public void setPDUType(EnumPDUType enumPDUType) 
	{
		this.enumPDUType = enumPDUType;
	}

	/**
	 * Parse the PDU from the stream available.
	 * @param objIDicomStream Stream on which dicom data is available.
	 * @throws IOException  Signals that an I/O exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream)throws IOException
	{
		byte bPDUType = objIDicomStream.readByte();
		switch(bPDUType)
		{
		case 1 : enumPDUType = EnumPDUType.ASSOCIATE_RQ;
		break;
		case 2 : enumPDUType = EnumPDUType.ASSOCIATE_AC;
		break;
		case 3 : enumPDUType = EnumPDUType.ASSOCIATE_RJ;
		break;
		case 4 : enumPDUType = EnumPDUType.P_DATA_TF;
		break;
		case 5 : enumPDUType = EnumPDUType.RELEASE_RQ;
		break;
		case 6 : enumPDUType = EnumPDUType.RELEASE_RP;
		break;
		case 7 : enumPDUType = EnumPDUType.ABORT;
		break;
		default : //throw InvalidPDUException();
		}
		objIDicomStream.readByte();//Read Reserved Byte
		objIDicomStream.readInt();//Read PDU Length
	}

	/**
	 * This method writes the Data Value to the given Stream.
	 * @param objIDicomStream object of the stream onto which the PDU is to be written.
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException
	{
		objIDicomStream.setEndianNess(DicomConstants.DCM_ENDIANNESS_BIG_ENDIAN);
		objIDicomStream.writeByte(enumPDUType.getValue());
		objIDicomStream.writeByte((byte)0);
		objIDicomStream.writeInt(getLength());
	}

	/**
	 * Gets the length.
	 * @return int - the length of PDU.
	 */
	public abstract int getLength();
}
