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



import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;


/**
* This item class contains methods to validate, set and get attributes in  Dimension Index Sequence. 
 */

public class DimensionIndexSequenceItem extends Item  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and with default TransferSyntax 
	 */
	DimensionIndexSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates Item with specified transferSyntax
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	DimensionIndexSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates Item with specified transferSyntax and specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	DimensionIndexSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in DimensionIndexSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule() throws InvalidItemException
	{
		/*Condition for Dimension Index Private Creator(0020,9213),
		 *Condition for Functional Group Pointer (0020,9167), 
		 *Condition for Functional Group Private Creator (0020,9238)
		 *not understood. PDF 3, Pg. No. 329
		 **/
		try
		{
			super.validateForModule();
			if(!contains(0x0020,0x9165))
				throw new MandatoryMissingException("0020,9165");
			if(!contains(0x0020,0x9164))
				throw new MandatoryMissingException("0020,9164");

		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch( MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(mme.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),mme); 
		}
		return true;
	}


	/**
	 * Sets the Data Element Tag that is used to identify the Attribute connected with the index. 
	 * @param objSingleCollection Collection of Tags (GroupNumber and ElementNumbers)
	 */
	public void setDimensionIndexPointer(ISingleCollection<ITag> objSingleCollection)
	{
		/*Accept Dimension Index Pointer in String format of type short array.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0020,0x9165, objSingleCollection, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020,0x9165, DicomConstants.DCM_DT_AT, objSingleCollection);
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the Data Element Tag that is used to identify the Attribute connected with the index. 
	 * @return ISingleCollection<ITag> - Collection of Tags (GroupNumber and ElementNumbers)
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<ITag> getDimensionIndexPointer()
	{
		Object objValue = getValue(0x0020,0x9165);
		if(objValue != null)
			return (ISingleCollection<ITag>)objValue;
		return null;
	}

	/**
	 * Sets identification of the creator of a group of private data elements.
	 * @param strDimensionIndexPrivateCreator String value of Dimension Index Private Creator.
	 */
	public void setDimensionIndexPrivateCreator(String strDimensionIndexPrivateCreator)
	{
		/*Accept the Dimension Index Private Creator in String format of type LO.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0020, 0x9213, strDimensionIndexPrivateCreator,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020, 0x9213,DicomConstants.DCM_DT_LO, strDimensionIndexPrivateCreator);
		//Add the DataElement to the Item
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves uniquely identifies a set of dimensions referenced within the containing SOP Instance.
	 * @return String - value of Dimension Index Private Creator.
	 */
	public String getDimensionIndexPrivateCreator()
	{
		Object objValue = getValue(0x0020,0x9213);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Data Element Tag of the Functional Group Sequence that contains
	 * the Attribute that is referenced by the Dimension Index Pointer (0020,9165). 
	 * @param objSingleCollection Collection of Tags (GroupNumber and ElementNumbers).
	 */
	public void setFunctionalGroupPointer(ISingleCollection<ITag> objSingleCollection)
	{
		/*Accept Functional Group Pointer in String format of type short array.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0020,0x9167, objSingleCollection, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020,0x9167, DicomConstants.DCM_DT_AT, objSingleCollection);
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the Data Element Tag of the Functional Group Sequence that contains
	 * the Attribute that is referenced by the Dimension Index Pointer (0020,9165). 
	 * @return ISingleCollection<ITag> - Collection of Tags (GroupNumber and ElementNumbers)
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<ITag> getFunctionalGroupPointer()
	{
		Object objValue = getValue(0x0020,0x9167);
		if(objValue != null)
			return (ISingleCollection<ITag>)objValue;
		return null;
	}

	/**
	 * Sets identification of the creator of a group of private data elements.
	 * @param strFunctionalGroupPrivateCreator String value of Functional Group Private Creator.
	 */
	public void setFunctionalGroupPrivateCreator(String strFunctionalGroupPrivateCreator)
	{
		/*Accept the Functional Group Private Creator in String format of type LO.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0020, 0x9238, strFunctionalGroupPrivateCreator,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020, 0x9238,DicomConstants.DCM_DT_LO, strFunctionalGroupPrivateCreator);
		//Add the DataElement to the Item
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves identification of the creator of a group of private data elements.
	 * @return String - value of Functional Group Private Creator.
	 */
	public String getFunctionalGroupPrivateCreator()
	{
		Object objValue = getValue(0x0020,0x9238);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/** 
	 * Sets uniquely identifies a set of dimensions referenced within the containing SOP Instance. 
	 * In particular the dimension described by this sequence item is associated with this Dimension
	 * Organization UID.
	 * @param strDimensionOrganizationUID string value uniquely identifies dimensions referenced within the 
	 * containing SOP Instance.
	 */
	public void setDimensionOrganizationUID(String strDimensionOrganizationUID)
	{
		/*Accept the Dimension Organization UID in String format of type UI.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0020, 0x9164, strDimensionOrganizationUID,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020, 0x9164,DicomConstants.DCM_DT_UI, strDimensionOrganizationUID);
		//Add the DataElement to the Item
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves identification of the creator of a group of private data elements.
	 * @return String - value uniquely identifies dimensions referenced within the 
	 * containing SOP Instance.
	 */
	public String getDimensionOrganizationUID()
	{
		Object objValue = getValue(0x0020,0x9164);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets DimensionDescriptionLabel.
	 * @param strDimensionDescriptionLabel String value of DimensionDescriptionLabel.
	 */
	public void setDimensionDescriptionLabel(String strRescaleType)
	{
		/* Accept the Dimension Description Label in String format of type LO.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0020,0x9421, strRescaleType,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0020,0x9421,DicomConstants.DCM_DT_LO, strRescaleType);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Dimension Description Label.
	 * @return String - value of DimensionDescriptionLabel.
	 */
	public String getDimensionDescriptionLabel()
	{
		Object objValue = getValue(0x0020,0x9421);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
