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




/**
 * A sequence of Items each of which represents a single layer in which overlays,curves, graphics or text may be rendered.
 */
public class GraphicLayerSequenceItem extends Item  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	GraphicLayerSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	GraphicLayerSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Validates all the attributes in GraphicLayerSequenceItem 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(0x0070,0x0002))
				throw new MandatoryMissingException("0070,0002");
			if(!contains(0x0070,0x0062))
				throw new MandatoryMissingException("0070,0062");
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets the layer in which the graphics or text is to be rendered.
	 * @param strGraphicLayer String value of Graphic Layer
	 */
	public void setGraphicLayer(String strGraphicLayer)
	{
		/*Accept the Graphic Layer in String format of type CS.
		 * 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(0x0070,0x0002, strGraphicLayer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0002,DicomConstants.DCM_DT_CS, strGraphicLayer);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the layer in which the graphics or text is to be rendered.
	 * @return String - value of Graphic Layer
	 */
	public String getGraphicLayer()
	{
		Object objValue = getValue(0x0070,0x0002);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets An integer indicating the order in which it is recommended that the layer be rendered, if the display is capable of distinguishing.
	 * Lower numbered layers are to be rendered first.
	 * @param strGraphicLayerOrder String value of Graphic Layer Order
	 */
	public void setGraphicLayerOrder(String strGraphicLayerOrder)
	{
		/*Accept the Graphic Layer Order in String 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(0x0070,0x0062, strGraphicLayerOrder,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0062,DicomConstants.DCM_DT_IS, strGraphicLayerOrder);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves An integer indicating the order in which it is recommended that the layer be rendered, if the display is capable of distinguishing.
	 * @return String - value of Graphic Layer Order
	 */
	public String getGraphicLayerOrder()
	{
		Object objValue = getValue(0x0070,0x0062);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets A default single gray unsigned value in which it is recommended that the layer be rendered on a monochrome display.
	 * @param shGraphicLayerRecommendedDisplayGrayscaleValue short gray unsigned value 
	 */
	public void setGraphicLayerRecommendedDisplayGrayscaleValue(short shGraphicLayerRecommendedDisplayGrayscaleValue)
	{
		/*Accept the Graphic Layer Recommended Display Grayscale Value 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(0x0070,0x0066, shGraphicLayerRecommendedDisplayGrayscaleValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0066,DicomConstants.DCM_DT_US, shGraphicLayerRecommendedDisplayGrayscaleValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves A default single gray unsigned value in which it is recommended that the layer be rendered on a monochrome display.
	 * @return short - short gray unsigned value 
	 */
	public short getGraphicLayerRecommendedDisplayGrayscaleValue()
	{
		Object objValue = getValue(0x0070,0x0066);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets A triplet of unsigned RGB values in which it is recommended that the layer be rendered on a color display.
	 * @param arrshGraphicLayerRecommendedDisplayRGBValue short array of triplet of unsigned RGB values
	 */
	public void setGraphicLayerRecommendedDisplayRGBValue(short[] arrshGraphicLayerRecommendedDisplayRGBValue)
	{
		/*Accept the Graphic Layer Recommended Display RGB Value in short array 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(0x0070,0x0067, arrshGraphicLayerRecommendedDisplayRGBValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0067,DicomConstants.DCM_DT_US, arrshGraphicLayerRecommendedDisplayRGBValue);

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves A triplet of unsigned RGB values in which it is recommended that the layer be rendered on a color display.
	 * @return short[] - short array of triplet of unsigned RGB values
	 */
	public short[] getGraphicLayerRecommendedDisplayRGBValue()
	{
		Object objValue = getValue(0x0070,0x0067);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}

	/**
	 * Sets Graphic Layer Recommended Display CIELab Values
	 * @param arrGraphicLayerRecommendedDisplayCIELabValue
	 */
	public void setGraphicLayerRecommendedDisplayCIELabValue(short[] arrGraphicLayerRecommendedDisplayCIELabValue)
	{
		/* Accept the Graphic Layer Recommended Display CIELab Values in Array of short format of type US.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0070, 0x0401, arrGraphicLayerRecommendedDisplayCIELabValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070, 0x0401,DicomConstants.DCM_DT_US, arrGraphicLayerRecommendedDisplayCIELabValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves  Graphic Layer Recommended Display CIELab Values in Array of short format.
	 * @return short Array - Graphic Layer Recommended Display CIELab Values.
	 */
	public short[] getGraphicLayerRecommendedDisplayCIELabValue()
	{
		Object objValue = getValue(0x0070, 0x0401);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	
	/**
	 * Sets A free text description of the contents of this layer.
	 * @param strGraphicLayerDescription String value of text
	 */
	public void setGraphicLayerDescription(String strGraphicLayerDescription)
	{
		/*Accept the Graphic Layer Description 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(0x0070,0x0068, strGraphicLayerDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0068,DicomConstants.DCM_DT_LO, strGraphicLayerDescription);

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves A free text description of the contents of this layer.
	 * @return String - value of text
	 */
	public String getGraphicLayerDescription()
	{
		Object objValue = getValue(0x0070,0x0068);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
