/*******************************************************************************
 *  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.enums.EnumBitsAllocated;
import cdac.medinfo.sdk.dcm30_04.enums.EnumBitsStored;
import cdac.medinfo.sdk.dcm30_04.enums.EnumHighBit;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPhotometricInterpretation;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPixelRepresentation;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

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


/**
 * This item class contains methods to validate, set and get the attributes in Basic Grayscale Image Sequence.
 */
public class BasicGrayscaleImageSequenceItem extends Item  
{
	


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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax. 
	 */
	BasicGrayscaleImageSequenceItem()
	{
		super();
	}

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax.
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	BasicGrayscaleImageSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Validates all the attributes in CodeSequenceMacro 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
	{
		/*Since this Sequence is part of a Normalized IOD Module the validation of attributes 
		 * cannot be done. Documents does not describe the type of the attributes in the Sequence.
		 */
		return super.validateForModule();
	}

	/**
	 * Samples per Pixel (0028,0002) is the number of separate planes in the image. One, three, and
	 * four image planes are defined. For monochrome (gray scale) and palette color images, the number of planes is 1. For RGB and
	 * other three vector color models, the value of this attribute is 3. For four vector color models, the
	 * value of this attribute is 4.
	 * @param sSamplesPerPixel -specifies the samples per pixel in the image.
	 */
	public void setSamplesPerPixel(short sSamplesPerPixel)
	{
		/*Accept Number of samples (planes) in this image in short 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(0x0028, 0x0002,sSamplesPerPixel,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0002, DicomConstants.DCM_DT_US,sSamplesPerPixel);
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the samples per pixel.
	 * @return short - returns the samples per pixel.
	 */
	public short getSamplesPerPixel()
	{
		Object objValue = getValue(0x0028, 0x0002);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the intended interpretation of the pixel data.
	 * @param enumPhotometricInterpretation- Enum value Photometric Interpretation.
	 */
	public void setPhotometricInterpretation(EnumPhotometricInterpretation enumPhotometricInterpretation)
	{
		/*Accept Specifies the intended interpretation of the pixel data in String format of type ST.
		 * 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(0x0028, 0x0004,enumPhotometricInterpretation.getValue(),objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0004, DicomConstants.DCM_DT_CS,enumPhotometricInterpretation.getValue());
		}	
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the photometric interpretation.
	 * @return String - returns the photometric interpretation.
	 */
	public String getPhotometricInterpretation()
	{
		Object objValue = getValue(0x0028, 0x0004);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets number of rows in the image.This describes the images's height.
	 * @param sRows -specifies the number of rows.
	 */
	public void setRows(short sRows)
	{
		/*Accept Number of Rows in this image in short 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(0x0028, 0x0010,sRows,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0010, DicomConstants.DCM_DT_US,sRows);
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of rows.
	 * @return short - returns the number of rows.
	 */
	public short getRows()
	{
		Object objValue = getValue(0x0028, 0x0010);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}


	/**
	 * Sets number of columns in the image. This describes the images's width.
	 * @param sColumns -specifies the number of columns.
	 */
	public void setColumns(short sColumns)
	{
		/*Accept Number of Columns in this image in short 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(0x0028, 0x0011,sColumns,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0011, DicomConstants.DCM_DT_US,sColumns);
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of columns.
	 * @return short - returns the number of columns.
	 */
	public short getColumns()
	{
		Object objValue = getValue(0x0028, 0x0011);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Ratio of the vertical size and horizontal size of the pixels in the image specified by
	 * a pair of integer values where the first value is the vertical pixel size.
	 * @param sPixelAspectRatio- specifies the pixel aspect ratio. 
	 */
	public void setPixelAspectRatio(String[] strArrPixelAspectRatio)
	{
		/*Accept Pixel Aspect Ratio in this image in short format of type IS.
		 * 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(0x0028, 0x0034,strArrPixelAspectRatio,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0034, DicomConstants.DCM_DT_IS,strArrPixelAspectRatio);
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the pixel aspect ratio.
	 * @return short - returns the pixel aspect ratio.
	 */
	public String[] getPixelAspectRatio()
	{
		if(getDataForTag(0x0028, 0x0034)!=null)
		{
			if(getDataForTag(0x0028, 0x0034).getValue()!=null)
			{
				Object objValue = getDataForTag(0x0028, 0x0034).getValue();
				if(objValue!=null)
				{
					if(objValue instanceof String)
					{
						String strTemp = (String)objValue;
						String[] strArr = new String[1];
						strArr[0] = strTemp;
						return strArr;
					}
					else
						return (String[])objValue;
				}
			}
		}
		return null;
	}

	/**
	 *	Number of bits allocated for each pixel sample. Each sample shall have the same
	 *	number of bits allocated. 
	 *	@param enumBitsAllocated -specifies the number of bits allocated.
	 */
	public void setBitsAllocated(EnumBitsAllocated enumBitsAllocated)
	{
		/*Accept bits allocated for each pixel in this image in short 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(0x0028, 0x0100,enumBitsAllocated.getValue(),objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0100, DicomConstants.DCM_DT_US,enumBitsAllocated.getValue());
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of bits allocated.
	 * @return short - returns the number of bits allocated.
	 */
	public short getBitsAllocated()
	{
		Object objValue = getValue(0x0028, 0x0100);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Number of bits stored for each pixel sample. Each sample shall have the same
	 * number of bits stored.
	 * @param enumBitsStored -specifies the number of bits stored.
	 */
	public void setBitsStored(EnumBitsStored enumBitsStored)
	{
		/*Accept bits stored for each pixel in this image in short 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(0x0028, 0x0101,enumBitsStored.getValue(),objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0101, DicomConstants.DCM_DT_US,enumBitsStored.getValue());
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of bits stored.
	 * @return short - returns the number of bits stored.
	 */
	public short getBitsStored()
	{
		Object objValue = getValue(0x0028, 0x0101);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Most significant bit for pixel sample data. Each sample shall have the same high bit.
	 * @param enumHighBit -specifies the high bit.
	 */
	public void setHighBit(EnumHighBit enumHighBit)
	{
		/*Accept Most significant bit for pixel in this image in short 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(0x0028, 0x0102,enumHighBit.getValue(),objIDictionaryReader);			
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0102, DicomConstants.DCM_DT_US,enumHighBit.getValue());
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the high bit.
	 * @return short - returns the high bit.
	 */
	public short getHighBit()
	{
		Object objValue = getValue(0x0028, 0x0102);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Data representation of the pixel samples. Each sample shall have the same pixel
	 * representation. 
	 * Enumerated Values: 
	 * 1) 0000H = unsigned integer. 
	 * 2) 0001H = 2's complement.
	 * @param enumPixelRepresentation -specifies the pixel representation.
	 */
	public void setPixelRepresentation(EnumPixelRepresentation enumPixelRepresentation)
	{
		/*Accept Data representation of pixel in this image in short 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(0x0028, 0x0103,enumPixelRepresentation.getValue(),objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0103, DicomConstants.DCM_DT_US,enumPixelRepresentation.getValue());
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the pixel representation .
	 * @return short - returns the pixel representation .
	 */
	public short getPixelRepresentation()
	{
		Object objValue = getValue(0x0028, 0x0103);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * A data stream of the pixel samples that comprise the Image.
	 * @param objCollectionOfPixelData -specifies the collection of pixel data
	 */ 
	public void setPixelData(ISingleCollection<byte[]> objCollectionOfPixelData)
	{
		/*Accept Pixel Data in this image in short format of type OB/OW.
		 * 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(0x7fe0, 0x0010, objCollectionOfPixelData, objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x7fe0, 0x0010, DicomConstants.DCM_DT_OW, objCollectionOfPixelData);
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the collection of pixel data.
	 * @return collection of byte[] - returns the collection pixel data.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getPixelData()
	{
		Object objValue = getValue(0x7fe0, 0x0010);
		if(objValue != null)
			return (ISingleCollection<byte[]>)objValue;
		return null;
	}
}
