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

/**
* This item class contains methods to set,get and validate the attributes in the Histogram Sequence.
 */
public class HistogramSequenceItem extends Item  
{
	        

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

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

	/**
	 * This constructor creates the item with default TransferSyntax.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax   
	 */
	HistogramSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	HistogramSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in HistogramSequenceItem 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
	{
		try{
			if(!contains(0x0060,0x3002))
				throw new MandatoryMissingException("0060,3002");
			if(!contains(0x0060,0x3004))
				throw new MandatoryMissingException("0060,3004");
			if(!contains(0x0060,0x3006))
				throw new MandatoryMissingException("0060,3006");
			if(!contains(0x0060,0x3008))
				throw new MandatoryMissingException("0060,3008");
			if(!contains(0x0060,0x3020))		
				throw new MandatoryMissingException("0060,3020");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets the number of �bins� (entries) in the histogram.
	 * @param shHistogramNumberOfBins short value of Histogram Number of Bins.
	 */
	public void setHistogramNumberOfBins(short shHistogramNumberOfBins)
	{
		/*Accept the Histogram Number of Bins in String format of type US.
		 * 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(0x0060, 0x3002, shHistogramNumberOfBins,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0060, 0x3002,DicomConstants.DCM_DT_US, shHistogramNumberOfBins);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of �bins� (entries) in the histogram.
	 * @return short - short value of Histogram Number of Bins.
	 */
	public short getHistogramNumberOfBins()
	{
		Object objValue = getValue(0x0060, 0x3002);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the stored pixel value corresponding to the lowest pixel value counted in the first bin.
	 * All image pixel values less than this value are not included in the histogram.
	 * @param shHistogramFirstBinValue short value of Histogram First Bin Value.
	 */
	public void setHistogramFirstBinValue(short shHistogramFirstBinValue)
	{
		/*Accept the Histogram First Bin Value in String format of type US.
		 * 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(0x0060, 0x3004, shHistogramFirstBinValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0060, 0x3004,DicomConstants.DCM_DT_US, shHistogramFirstBinValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the stored pixel value corresponding to the lowest pixel value counted in the first bin.
	 * @return short - short value of Histogram Number of Bins.
	 */
	public short getHistogramFirstBinValue()
	{
		Object objValue = getValue(0x0060, 0x3004);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the stored pixel value corresponding to the highest pixel value counted in the last bin.
	 * All image pixel values greater than this value are not included in the histogram.
	 * @param shHistogramLastBinValue short value of Histogram Last Bin Value.
	 */
	public void setHistogramLastBinValue(short shHistogramLastBinValue)
	{
		/*Accept the Histogram Last Bin Value in String format of type US.
		 * 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(0x0060, 0x3006, shHistogramLastBinValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0060, 0x3006,DicomConstants.DCM_DT_US, shHistogramLastBinValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the stored pixel value corresponding to the highest pixel value counted in the last bin.
	 * @return short - short value of Histogram Last Bin Value.
	 */
	public short getHistogramLastBinValue()
	{
		Object objValue = getValue(0x0060, 0x3006);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the number of consecutive stored pixel values included in a bin. All bins shall be of
	 * equal width.
	 * @param shHistogramBinWidth  short value of Histogram Bin Width.
	 */
	public void setHistogramBinWidth(short shHistogramBinWidth)
	{
		/*Accept the Histogram Bin Width in String format of type US.
		 * 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(0x0060, 0x3008, shHistogramBinWidth,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0060, 0x3008,DicomConstants.DCM_DT_US, shHistogramBinWidth);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of consecutive stored pixel values included in a bin. All bins shall be of
	 * equal width.
	 * @return short - short value of Histogram Bin Width.
	 */
	public short getHistogramBinWidth()
	{
		Object objValue = getValue(0x0060, 0x3008);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets free form text explanation of the meaning of the LUT.
	 * @param strInterpretationExplanation String value of Interpretation Explanation.
	 */
	public void setInterpretationExplanation(String strInterpretationExplanation)
	{
		/*Accept Interpretation Explanation in String format of type DA.
		 * 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(0x0060,0x3010,strInterpretationExplanation,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0060,0x3010, DicomConstants.DCM_DT_LO,strInterpretationExplanation);
		addDataElement(objIDataElement);
	}

	/**
	 * Retreives free form text explanation of the meaning of the LUT.
	 * @return String - value of Interpretation Explanation.
	 */
	public String getInterpretationExplanation()
	{
		Object objValue = getValue(0x0060,0x3010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Histogram Data encoded as 32 bit unsigned counts of the number of pixel values in each bin.
	 * @param arrIntInterpretationData Integer array of Interpretation Data.
	 */
	public void setInterpretationData(int[] arrIntInterpretationData)
	{
		/*Accept Interpretation Data in String format of type UL.
		 * 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(0x0060,0x3020,arrIntInterpretationData,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0060,0x3020, DicomConstants.DCM_DT_UL,arrIntInterpretationData);
		addDataElement(objIDataElement);
	}

	/**
	 * Retreives Histogram Data encoded as 32 bit unsigned counts of the number of pixel values in each bin.
	 * @return int[] - Integer array of Interpretation Data.
	 */
	public int[] getInterpretationData()
	{
		Object objValue = getValue(0x0060,0x3010);
		if(objValue != null)
			return (int[])objValue;
		return null;
	}
}
