/*******************************************************************************
 *  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.EnumCornealPointEstimated;
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 validate, set and get the attributes of Source Image Corneal Processed Data Sequence.
 */
public class SourceImageCornealProcessedDataSequenceItem extends Item{

	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	SourceImageCornealProcessedDataSequenceItem()
	{
		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
	 */
	SourceImageCornealProcessedDataSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Validates all the attributes in SourceImageCornealProcessedDataSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			
			if(!contains(0x0046,0x0247))
				throw new MandatoryMissingException("0046,0247") ;
			if(!contains(0x0046,0x0248))
				throw new MandatoryMissingException("0046,0248") ;
			if(!contains(0x0046,0x0249))
				throw new MandatoryMissingException("0046,0249") ;
			if(!contains(0x0046,0x0250))
				throw new MandatoryMissingException("0046,0250") ;
			if(!contains(0x0046,0x0251))
				throw new MandatoryMissingException("0046,0251") ;
			if(!contains(0x0046,0x0252))
				throw new MandatoryMissingException("0046,0252") ;
			if(!contains(0x0046,0x0253))
				throw new MandatoryMissingException("0046,0253") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		
		return true;
	}
	
	/**
	 * Sets Corneal Point Location
	 * The x, y, and z coordinate of a point in the image within the cornea, in mm. 
	 * Where the origin is conveyed by the Attribute Corneal Vertex Location (0046,0202).
	 * @param arrCornealPointLocation pair of floating point numbers
	 */
	public void setCornealPointLocation(float[] arrCornealPointLocation)
	{
		/* Accept the Mask Sub-pixel Shift in short format of type FL.
		 * The x, y, and z coordinate of a point in the image within the cornea, in mm. 
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0046,0x0247, arrCornealPointLocation,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046,0x0247,DicomConstants.DCM_DT_FL, arrCornealPointLocation);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Corneal Point Location.
	 * @return float[] - Float array of Mask Sub Pixel Shift.
	 */
	public float[] getCornealPointLocation()
	{
		Object objValue = getValue(0x0046,0x0247);
		if(objValue != null)
			return (float[])objValue;
		return null;
	}
	/**
	 * Sets the Corneal Point Estimated Defined in Terms of Enum
	 * Enumerated Values:
	 * Y	Yes
	 * N	No
	 * @param enumCornealPointEstimated Specifies type of the value encoded in this Item
	 */
	public void setCornealPointEstimated(EnumCornealPointEstimated enumCornealPointEstimated)
	{
		/* Accept the CornealPointEstimated in enum format of type CS.
		 * The point defined in Attribute Corneal Point Location (0046,0247) is an estimated point 
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0046, 0x0248, enumCornealPointEstimated,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046, 0x0248,DicomConstants.DCM_DT_CS, enumCornealPointEstimated);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getValueType()
	{
		Object objValue = getValue(0x0046, 0x0248);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Ratio of displayed pixels to source pixels,specified in one dimension.
	 * Conveys corneal shape characteristics, in diopters. It is determined from the curvature, r in mm,
	 *  of the equivalent sphere with the same slope at a given point on the surface. Axial power = Km/r, where Km is the Keratometric Index.
	 * @param fAxialPower Float value of the Ratio of displayed pixels to source pixels
	 */
	public void setAxialPower( float fAxialPower)
	{
		/* Accept the AxialPower in String format of type FL.
		 * Ratio of displayed pixels to source pixels,specified in one dimension.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0046,0x0249, fAxialPower,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046,0x0249,DicomConstants.DCM_DT_FL, fAxialPower);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Axial Power
	 * @return float - Float value of the AxialPower of 
	 */
	public float getAxialPower()
	{
		Object objValue = getValue(0x0046,0x0249);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Determined from the curvature (2nd derivative = d2y/dx2) of a point on the cornea, 
	 * in diopters; also called Instantaneous Power. Emphasizes irregular astigmatism 
	 * @param TangentialPower Float value of the Ratio of displayed pixels to source pixels
	 */
	public void setTangentialPower( float fTangentialPower)
	{
		/* Sets Determined from the curvature (2nd derivative = d2y/dx2) of a point on the cornea, 
	     * in diopters; also called Instantaneous Power. Emphasizes irregular astigmatism
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0046,0x0250, fTangentialPower,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046,0x0250,DicomConstants.DCM_DT_FL, fTangentialPower);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Tangential Power
	 * @return float - Float value of the TangentialPower of 
	 */
	public float getTangentialPower()
	{
		Object objValue = getValue(0x0046,0x0250);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets RefractivePower
	 * Conveys the optical refraction properties of the cornea, in  diopters.
	 * It is determined from ray tracing using Snell's Law to calculate the true refractive power.
	 * @param shRefractivePower short value of Rotation of right image.
	 */
	public void setRefractivePower(short shRefractivePower)
	{
		/* Accept the RefractivePower of right image of type FL.
		 * Conveys the optical refraction properties of the cornea, in  diopters.
		 * It is determined from ray tracing using Snell's Law to calculate the true refractive power.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0046,0x0251,shRefractivePower,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046,0x0251,DicomConstants.DCM_DT_FL, shRefractivePower);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  RRefractivePower
	 * @return short - short value of RefractivePower of right image.
	 */
	public short getRefractivePower()
	{
		Object objValue = getValue(0x0046,0x0251);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets RelativeElevation
	 * Relative elevation, in micrometers, is calculated from the corneal surface distance from a reference surface (i.e., a sphere or ellipsoid) 
	 * that intersects the corneal vertex
	 * @param shRefractivePower short value of Rotation of right image.
	 */
	public void setRelativeElevation(short shRelativeElevation)
	{
		/* Accept the RelativeElevation of right image of type FL.
		 * Relative elevation, in micrometers, is calculated from the corneal surface distance from a reference surface 
		 * (i.e., a sphere or ellipsoid) that intersects the corneal vertex
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0046,0x0252,shRelativeElevation,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046,0x0252,DicomConstants.DCM_DT_FL, shRelativeElevation);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  RRefractivePower
	 * @return short - short value of RefractivePower of right image.
	 */
	public short getRelativeElevation()
	{
		Object objValue = getValue(0x0046,0x0252);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets CornealWavefront.
	 * @param fCornealWavefront Float value of the Ratio of displayed pixels to source pixels
	 */
	public void setCornealWavefront( float fCornealWavefront)
	{
		/* Corneal wavefront, in micrometers, is calculated from the elevation data by fitting with the Zernike Series expansion.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0046,0x0253, fCornealWavefront,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0046,0x0253,DicomConstants.DCM_DT_FL, fCornealWavefront);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Corneal Wave front.
	 * @return float - Float value of the Corneal Wave front
	 */
	public float getCornealWavefront()
	{
		Object objValue = getValue(0x0046,0x0253);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}
