/*******************************************************************************
 *  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.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumAmbientLightValueSource;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
* This item class contains methods to validate, set and get the attributes of Target Luminance Characteristic sSequence .
 */
public class TargetLuminanceCharacteristicsSequenceItem extends Item{

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

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

	/**
	 * This constructor creates FillStyleSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	TargetLuminanceCharacteristicsSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	/**
	 * Sets Number of significant bits within the LuminanceCharacteristicsID samples
	 * Number that identifies this Luminance Characteristics item.
	 * The value shall be unique within the Target Luminance Characteristics Sequence (0028,7008).
	 * @param shLuminanceCharacteristicsID short value of Number of significant bits
	 */
	public void setLuminanceCharacteristicsID(short shLuminanceCharacteristicsID)
	{
		/* Accept the WaveformBitsStored of type US.
		 * Number that identifies this Luminance Characteristics item.
		 * The value shall be unique within the Target Luminance Characteristics Sequence (0028,7008).
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0028,0x7009,shLuminanceCharacteristicsID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x7009,DicomConstants.DCM_DT_US, shLuminanceCharacteristicsID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of significant bits within the Luminance Characteristics ID
	 * @return short - short value of Number of significant bits
	 */
	public short getLuminanceCharacteristicsID()
	{
		Object objValue = getValue(0x0028,0x7009);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the DisplayFunctionType Defined in Terms 
	 * The type of Display Function used to define the target luminance curve.
	 * @param strDisplayFunctionType Specifies type of the value encoded in this Item
	 */
	public void setDisplayFunctionType(String strDisplayFunctionType)
	{
		/* Accept the Display Function Type in String format of type CS.
		 * The type of Display Function used to define the target luminance curve.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0028, 0x7019, strDisplayFunctionType,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028, 0x7019,DicomConstants.DCM_DT_CS, strDisplayFunctionType);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getDisplayFunctionType()
	{
		Object objValue = getValue(0x0028, 0x7019);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Ratio of Target Minimum Luminance,specified in one dimension.
	 * @param fTargetMinimumLuminance Float value of the Ratio of displayed pixels to source pixels
	 */
	public void setTargetMinimumLuminance( float fTargetMinimumLuminance)
	{
		/* Accept the TargetMinimumLuminance in String format of type FL.
		 * The minimum luminance value of the target luminance curve, in candelas per square meter (cd/m2).
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0028,0x701D, fTargetMinimumLuminance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x701D,DicomConstants.DCM_DT_FL, fTargetMinimumLuminance);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves The minimum luminance value of the target luminance curve, in candelas per square meter (cd/m2).
	 * @return float - Float value of the Ratio of Target Minimum Luminance to source pixels
	 */
	public float getTargetMinimumLuminance()
	{
		Object objValue = getValue(0x0028,0x701D);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	

	/**
	 * Sets  The maximum luminance value of the target luminance curve, in candelas per square meter (cd/m2).
	 * @param fTargetMaximumLuminance Float value of the Ratio of displayed pixels to source pixels
	 */
	public void setTargetMaximumLuminance( float fTargetMaximumLuminance)
	{
		/* Accept the TargetMaximumLuminance in String format of type FL.
		 * The maximum luminance value of the target luminance curve, in candelas per square meter (cd/m2).
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0028,0x701E, fTargetMaximumLuminance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x701E,DicomConstants.DCM_DT_FL, fTargetMaximumLuminance);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves The maximum luminance value of the target luminance curve, in candelas per square meter (cd/m2).
	 * @return float - Float value of the Ratio of TargetMaximumLuminance to source pixels
	 */
	public float getTargetMaximumLuminance()
	{
		Object objValue = getValue(0x0028,0x701E);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	

	/**
	 * Sets GammaValue The Gamma value for generating the target luminance curve.
	 * Required if the value of Display Function Type (0028,7019) is GAMMA.
	 * @param fGammaValue Float value of the Gamma Value
	 */
	public void setGammaValue( float fGammaValue)
	{
		/* Accept the The Gamma value for generating the target luminance curve.
	     * Required if the value of Display Function Type (0028,7019) is GAMMA.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0028,0x701A, fGammaValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x701A,DicomConstants.DCM_DT_FL, fGammaValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves GammaValue.The Gamma value for generating the target luminance curve.
	 * Required if the value of Display Function Type (0028,7019) is GAMMA.
	 * @return float - Float value of the Ratio of GammaValue to source pixels
	 */
	public float getGammaValue()
	{
		Object objValue = getValue(0x0028,0x701A);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	public void setNumberofLuminancePoints(short[] arrNumberofLuminancePoints)
	{
		/* Accept the Referenced NumberofLuminancePoints in Array of short format of type US.
		 * The number of luminance points in the user defined target luminance curve.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0028,0x701B, arrNumberofLuminancePoints,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x701B,DicomConstants.DCM_DT_US, arrNumberofLuminancePoints);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves one or more NumberofLuminancePoints.
	 * @return short Array -Number of Luminance Points.
	 */
	public short[] getNumberofLuminancePoints()
	{
		Object objValue = getValue(0x0028,0x701B);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}

	/**
	 * Retrieves pre-initialized object of Luminance Response Sequence.If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence.
	 * @return LuminanceResponseSequence - instance of LuminanceResponseSequence. 
	 */
	public LuminanceResponseSequence getPrimaryFluenceModeSequence()
	{
		LuminanceResponseSequence objLuminanceResponseSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0028, 0x701C);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objLuminanceResponseSequence = (LuminanceResponseSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objLuminanceResponseSequence = (LuminanceResponseSequence)objDicomV3Factory.createSequence(0x0028, 0x701C, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0028, 0x701C, objLuminanceResponseSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0028, 0x701C, DicomConstants.DCM_DT_SQ, objLuminanceResponseSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objLuminanceResponseSequence;
	}

	/**
	 * Sets Luminance Response Description.User defined text to describe the user defined target luminance curve.
	 * @param strLuminanceResponseDescription String value of Rescale Type.
	 */
	public void setLuminanceResponseDescription(String strLuminanceResponseDescription)
	{
		/* Accept the Luminance Response Description in String format of type LO.
		 * User defined text to describe the user defined target luminance curve.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0028,0x7020, strLuminanceResponseDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x7020,DicomConstants.DCM_DT_LO, strLuminanceResponseDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Luminance Response Description.
	 * @return String - value of LuminanceResponseDescription.
	 */
	public String getLuminanceResponseDescription()
	{
		Object objValue = getValue(0x0028,0x7020);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Ratio of CIExyWhitePoint.
	 * @param fCIExyWhitePoint Float value of the CIExy White Point
	 */
	public void setCIExyWhitePoint( float fCIExyWhitePoint)
	{
		/* Accept the CIExyWhitePoint in String format of type FL.
		 * A single measured White Point recorded for the Display Subsystem.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0028,0x7018, fCIExyWhitePoint,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x7018,DicomConstants.DCM_DT_FL, fCIExyWhitePoint);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Ratio of displayed pixels to source pixels,specified in one dimension.
	 * @return float - Float value of the Ratio of displayed pixels to source pixels
	 */
	public float getCIExyWhitePoint()
	{
		Object objValue = getValue(0x0028,0x7018);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Number of significant bits within the waveform samples
	 * @param shReflectedAmbientLight short value of Number of significant bits
	 */
	public void setReflectedAmbientLight(short shReflectedAmbientLight)
	{
		/* Accept the ReflectedAmbientLight of type US.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x2010,0x0160,shReflectedAmbientLight,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2010,0x0160,DicomConstants.DCM_DT_US, shReflectedAmbientLight);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of significant bits within the ReflectedAmbientLight samples
	 * @return short - short value of Number of significant bits
	 */
	public short getReflectedAmbientLight()
	{
		Object objValue = getValue(0x2010,0x0160);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Ambient Light Value Source Defined in Terms of TEXT,NUMERIC,CODE,DATE,TIME,PNAME
	 * @param enumAmbientLightValueSource Specifies type of the value encoded in this Item
	 */
	public void setValueType(EnumAmbientLightValueSource enumAmbientLightValueSource)
	{
		/* Accept the Value Type in String format of type CS.
		 * Source of the reflected ambient light value.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0028, 0x7025, enumAmbientLightValueSource,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028, 0x7025,DicomConstants.DCM_DT_CS, enumAmbientLightValueSource);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getAmbientLightValueSource()
	{
		Object objValue = getValue(0x0028, 0x7025);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

}
