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

import java.io.IOException;
import java.io.Serializable;

import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ItemException;
import cdac.medinfo.sdk.dcm30_04.utils.ICollectionIterable;
import cdac.medinfo.sdk.dcm30_04.utils.IPairedCollection;


/**
 * This interface needs to be implemented by all classes which implements the necessary operations that can be performed on the item.
 */
public interface IItem extends ICollectionIterable<IDataElement>, Serializable
{
	/**
	 * Sets the TransferSyntax of the item. 
	 * This TransferSyntax should be the same as per the TransferSyntax of the outside DataSet whose one of the data elements contain this sequence and item.	 	
	 * @param objITransferSyntax instance of TransferSyntax. 
	 */
	void setTransferSyntax(ITransferSyntax objITransferSyntax);

	/**
	 * Retrieves the TransferSyntax of the Item.
	 * @return ITransferSyntax - instance of TransferSyntax.
	 */
	ITransferSyntax getTransferSyntax();

	/**
	 * Reads the item class from the underlying stream
	 * @param objIDicomStream object of stream  from which the Item is to be read.
	 * @param iItemLength the length of the item upto which the data is to be read from the underlying stream
	 * @return int - shows status as success or failure.
	 * @throws ItemException Signals Item Validation is failed.
	 * @throws IOException Signals that an I/O exception of some sort has occurred.
	 */
	int read(IDicomStream objIDicomStream,int iItemLength) throws ItemException, IOException;

	/**
	 * Writes the item class over the specified stream
	 * @param objIDicomStream object of stream onto which the item information needs to be serialized
	 * @return int - shows status as success or failure.
	 * @throws IOException  Signals that an I/O exception of some sort has occurred.
	 */
	int write(IDicomStream objIDicomStream) throws IOException;

	/**
	 * Gives full length of the item i.e. length including major minor tag lengths as specified in the DICOM Standard
	 * @return int - integer value of length of the item.
	 */
	int getItemLength();

	/**
	 * Retrieves the length of the value of the item. Note that this length excludes the length of item major and minor tags
	 * @return int - integer value of length of the item.
	 */
	int getValueLength();

	/**
	 * Sets the DictionaryReader into the item class if the implicit DataElements are to be read/written in the item class
	 * @param objIDictionaryReader object of DictionaryReader
	 */
	void setDictionaryReader(IDictionaryReader objIDictionaryReader);

	/**
	 * Retrieves the DictionaryReader used by the item to read/write implicit DataElements 
	 * @return IDicitonaryReader - object of the DictionaryReader used by the Item
	 */
	IDictionaryReader getDictionaryReader();

	/**
	 * Retrieves the facility to retrieve the DataElements inside the item based on Group Numbers and Element Numbers. 
	 * @param iGroupNumber specifies the Group Number.
	 * @param iElementNumber specifies the Element Number.
	 * @return IDataElement - object of DataElement retrieved from the item collection.
	 */
	IDataElement getDataForTag(int iGroupNumber, int iElementNumber);

	/**
	 * Checks the presence/absence of the DataElement in the item collection
	 * @param iGroupNumber specifies the Group Number.
	 * @param iElementNumber specifies the Element Number.
	 * @return boolean - shows status as success or failure.
	 */
	boolean contains(int iGroupNumber, int iElementNumber);

	/**
	 * Validates the item according for specific to the DICOM standards
	 * @return int - shows status as success or failure.
	 * @throws InvalidItemException Signals Item Validation is failed.
	 */
	boolean validateForModule() throws InvalidItemException;
	
	/**
	 * Validates the data elements of item according to the rules of DICOM standard
	 * @return int - shows status as success or failure.
	 * @throws InvalidItemException Signals Item Validation is failed.
	 */
	boolean validate() throws InvalidItemException;

	/**
	 * Adds the DataElement to the collection of Items.
	 * @param objIDataElement object of DataElement to add.
	 * @return int - shows status as success or failure.
	 */
	int addDataElement(IDataElement objIDataElement);

	/**
	 * Sets the value of the item in terms of paired collection of Tag and DataElements
	 * @param objPairedCollection - paired collection of Tag and DataElement representing the collection of DataElements	
	 * @return int - shows status as success or failure.
	 */
	int setValue(IPairedCollection<ITag,IDataElement> objPairedCollection);
	
	/**
	 * Create similar object of this class.
	 * This does the deep copy and create a new instance of this class and return it.
	 * @return IItem - object of IItem.
	 */
	public Object clone();
	
	/**
	 *  Dumps the Items of a Sequence in readable form to the specified stream.
	 *  @param objIDicomStream - the Stream on which the Data is to be written.
	 *  @throws IOException - Exception is thrown indicating runtime failure of writing operation.
	 */
	public void dumpDICOMInfo(IDicomStream objIDicomStream) throws IOException;
	
	/**
	 * Method returns value of Item.
	 * @param iGroupNumber - Group Number of the DataElement
	 * @param iElementNumber -  Element Number of the DataElement
	 * @return Object - instance of IItem.
	 */
	Object getValue(int iGroupNumber, int iElementNumber);
}