/*******************************************************************************
 *  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.EnumGeneralizedDefectCorrectedSensitivityDeviationFlag;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
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 Visual Field Test Point Normals Sequence {@link VisualFieldTestPointNormalsSequence s}.
 */


public class VisualFieldTestPointNormalsSequenceItem extends Item
{
	

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	VisualFieldTestPointNormalsSequenceItem()
	{
		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
	 */
	VisualFieldTestPointNormalsSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates VisualFieldTestPointNormalsSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	VisualFieldTestPointNormalsSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	/**
	 * Sets the Difference between the patient's local sensitivity and the age correctednormal sensitivity, in dB.
	 * @param fAgeCorrectedSensitivityDeviationValue Float value of the Age Corrected Sensitivity Deviation Value
	 */
	public void setAgeCorrectedSensitivityDeviationValue( float fAgeCorrectedSensitivityDeviationValue)
	{
		/*
		 * Accept the AgeCorrectedSensitivityDeviationValue in String format of type FL.
		 * 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(0x0024,0x0092, fAgeCorrectedSensitivityDeviationValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0092,DicomConstants.DCM_DT_FL, fAgeCorrectedSensitivityDeviationValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Difference between the patient's local sensitivity and the age correctednormal sensitivity, in dB.
	 * @return float - Float value of the Age Corrected Sensitivity Deviation Value
	 */
	public float getAgeCorrectedSensitivityDeviationValue()
	{
		Object objValue = getValue(0x0024,0x0092);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the percentile of the age corrected sensitivity deviation within thenormal population of visual field, in percent.
	 * @param fAgeCorrectedSensitivityDeviationProbabilityValue Float value of the Age Corrected Sensitivity Deviation Probability Value
	 */
	public void setAgeCorrectedSensitivityDeviationProbabilityValue( float fAgeCorrectedSensitivityDeviationProbabilityValue)
	{
		/*
		 * Accept the AgeCorrectedSensitivityDeviationProbabilityValue in String format of type FL.
		 * 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(0x0024,0x0100, fAgeCorrectedSensitivityDeviationProbabilityValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0100,DicomConstants.DCM_DT_FL, fAgeCorrectedSensitivityDeviationProbabilityValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the percentile of the age corrected sensitivity deviation within thenormal population of visual field, in percent.
	 * @return float - Float value of the Age Corrected Sensitivity Deviation Probability Value
	 */
	public float getAgeCorrectedSensitivityDeviationProbabilityValue()
	{
		Object objValue = getValue(0x0024,0x0100);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets whether generalized defect corrected data are available for this point.
	 *Enumerated Values:
	 *YES
	 *NO
	 * @param enumGeneralizedDefectCorrectedSensitivityDeviationFlag enum value of Generalized Defect Corrected Sensitivity Deviation Flag
	 */
	public void setGeneralizedDefectCorrectedSensitivityDeviationFlag(EnumGeneralizedDefectCorrectedSensitivityDeviationFlag enumGeneralizedDefectCorrectedSensitivityDeviationFlag)
	{
		/* Accept the Generalized Defect Corrected Sensitivity Deviation Flag 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(0x0024,0x0102, enumGeneralizedDefectCorrectedSensitivityDeviationFlag.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0102,DicomConstants.DCM_DT_CS, enumGeneralizedDefectCorrectedSensitivityDeviationFlag.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return Generalized Defect Corrected Sensitivity Deviation Flag
	 * @return String - value of Generalized Defect Corrected Sensitivity Deviation Flag
	 */
	public String getGeneralizedDefectCorrectedSensitivityDeviationFlag()
	{
		Object objValue = getValue(0x0024,0x0102);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the age corrected sensitivity deviation after correction for theGeneralized Defect, in dB. Generalized defect is proportional to theloss in sensitivity shared by all points in the visual field.
	 * @param fGeneralizedDefectCorrectedSensitivityDeviationValue Float value of Generalized Defect CorrectedSensitivity Deviation Value
	 */
	public void setGeneralizedDefectCorrectedSensitivityDeviationValue( float fGeneralizedDefectCorrectedSensitivityDeviationValue)
	{
		/*
		 * Accept the Generalized Defect CorrectedSensitivity Deviation Value in String format of type FL.
		 * 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(0x0024,0x0103, fGeneralizedDefectCorrectedSensitivityDeviationValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0103,DicomConstants.DCM_DT_FL, fGeneralizedDefectCorrectedSensitivityDeviationValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the age corrected sensitivity deviation after correction for theGeneralized Defect, in dB. Generalized defect is proportional to theloss in sensitivity shared by all points in the visual field.
	 * @return float - Float value of the Generalized Defect CorrectedSensitivity Deviation Value
	 */
	public float getGeneralizedDefectCorrectedSensitivityDeviationValue()
	{
		Object objValue = getValue(0x0024,0x0103);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the percentile of the generalized defect corrected sensitivity deviationwithin the normal population of visual field, in percent.
	 * @param fGeneralizedDefectCorrectedSensitivityDeviationProbabilityValue Float value of Generalized DefectCorrected Sensitivity DeviationProbability Value
	 */
	public void setGeneralizedDefectCorrectedSensitivityDeviationProbabilityValue( float fGeneralizedDefectCorrectedSensitivityDeviationProbabilityValue)
	{
		/*
		 * Accept the Generalized Defect CorrectedSensitivity Deviation Value in String format of type FL.
		 * 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(0x0024,0x0104, fGeneralizedDefectCorrectedSensitivityDeviationProbabilityValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0104,DicomConstants.DCM_DT_FL, fGeneralizedDefectCorrectedSensitivityDeviationProbabilityValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the percentile of the generalized defect corrected sensitivity deviationwithin the normal population of visual field, in percent.
	 * @return float - Float value of the Generalized DefectCorrected Sensitivity DeviationProbability Value
	 */
	public float getGeneralizedDefectCorrectedSensitivityDeviationProbabilityValue()
	{
		Object objValue = getValue(0x0024,0x0104);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Validates all the attributes in {@link VisualFieldTestPointNormalsSequenceItem} 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(0x0024,0x0092))
				throw new MandatoryMissingException("0024,0092");
			if(!contains(0x0024,0x0100))
				throw new MandatoryMissingException("0024,0100");
			if(!contains(0x0024,0x0102))
				throw new MandatoryMissingException("0024,0102");
			if((!getDataForTag(0x0024, 0x0102).getValue().equals(EnumGeneralizedDefectCorrectedSensitivityDeviationFlag.YES)))
					if(!contains(0x0024,0x0103))
						throw new ConditionFailedException("value for conditional Element 0x0024,0x0103 required if value of Generalized Defect Corrected Sensitivity Deviation Flag(0024,0102) is YES.");
			if((!getDataForTag(0x0024, 0x0102).getValue().equals(EnumGeneralizedDefectCorrectedSensitivityDeviationFlag.YES)))
				if(!contains(0x0024,0x0104))
					throw new ConditionFailedException("value for conditional Element 0x0024,0x0104 required if value of Generalized Defect Corrected Sensitivity Deviation Flag(0024,0102) is YES.");
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
