/*******************************************************************************
 *  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.EnumRetestStimulusSeen;
import cdac.medinfo.sdk.dcm30_04.enums.EnumStimulusResults;
import cdac.medinfo.sdk.dcm30_04.enums.EnumTestPointNormalsDataFlag;
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 Sequence {@link VisualFieldTestPointSequenceItem}.
 */
public class VisualFieldTestPointSequenceItem extends Item
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

	/**
	 * This constructor creates Visual Field Test Point Sequence Item with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	VisualFieldTestPointSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * Validates all the attributes in {@link VisualFieldTestPointSequenceItem} 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,0x0090))
				throw new MandatoryMissingException("0024,0090");
			if(!contains(0x0024,0x0091))
				throw new MandatoryMissingException("0024,0091");
			if(!contains(0x0024,0x0093))
				throw new MandatoryMissingException("0024,0093");
			if(getDataForTag(0x0024, 0x0093).getValue().toString().equals(EnumStimulusResults.SEEN))
				if(!contains(0x0024,0x0094))
					throw new ConditionFailedException("value for conditional Element 0x0024,0x0094 required if value of Stimulus Results (0024,0093) is SEEN ");
			if(getDataForTag(0x0024, 0x0057).getValue().toString().equals(EnumTestPointNormalsDataFlag.YES ))
				if(!contains(0x0024,0x0097))
					throw new ConditionFailedException("value for conditional Element 0x0024,0x0097 required if value of (0024,0057) 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;
	}
	/**
	 * Sets Visual Field Test Point X-Coordinate
	 * @param fVisualFieldTestPointXCoordinate Float value of Visual Field Test Point X-Coordinate
	 */
	public void setVisualFieldTestPointXCoordinate( float fVisualFieldTestPointXCoordinate)
	{
		/*
		 * Accept the Visual Field Test Point X-Coordinate 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,0x0090, fVisualFieldTestPointXCoordinate,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0090,DicomConstants.DCM_DT_FL, fVisualFieldTestPointXCoordinate);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Visual Field Test Point X-Coordinate.
	 * @return float - Float value of Visual Field Test Point X-Coordinate
	 */
	public float getVisualFieldTestPointXCoordinate()
	{
		Object objValue = getValue(0x0024,0x0090);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Visual Field Test Point Y-Coordinate
	 * @param fVisualFieldTestPointYCoordinate Float value of Visual Field Test Point Y-Coordinate
	 */
	public void setVisualFieldTestPointYCoordinate( float fVisualFieldTestPointYCoordinate)
	{
		/*
		 * Accept the Visual Field Test Point Y-Coordinate 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,0x0091, fVisualFieldTestPointYCoordinate,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0091,DicomConstants.DCM_DT_FL, fVisualFieldTestPointYCoordinate);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Visual Field Test Point Y-Coordinate.
	 * @return float - Float value of Visual Field Test Point Y-Coordinate
	 */
	public float getVisualFieldTestPointYCoordinate()
	{
		Object objValue = getValue(0x0024,0x0091);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets whether the patient saw a stimulus presented at a luminance other than maximum, a presentation at maximum luminance, or did not see any presented stimulus.
	 *Enumerated Values:
	 *SEEN	stimulus seen at a luminance value less than maximum
	 *NOT SEEN	stimulus not seen
	 *SEEN AT MAX	stimulus seen at the maximum luminance possible for the instrument
	 * @param enumStimulusResults enum value of StimulusResults.
	 */
	public void setStimulusResults(EnumStimulusResults enumStimulusResults)
	{
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0024,0x0093, enumStimulusResults.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0093,DicomConstants.DCM_DT_CS, enumStimulusResults.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return StimulusResults
	 * @return String - value of Stimulus Results
	 */
	public String getStimulusResults()
	{
		Object objValue = getValue(0x0024,0x0093);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Sensitivity Value
	 * @param fSensitivityValue Float value of Sensitivity Value
	 */
	public void setSensitivityValue( float fSensitivityValue)
	{
		/*
		 * Accept the Sensitivity 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,0x0094, fSensitivityValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0094,DicomConstants.DCM_DT_FL, fSensitivityValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Sensitivity Value
	 * @return float - Float value of Sensitivity Value.
	 */
	public float getSensitivityValue()
	{
		Object objValue = getValue(0x0024,0x0094);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets whether the retested stimulus presented was seen by the patient.
	 *Enumerated Values:
	 *YES	
	 *NO	
	 * @param enumRetestStimulusSeen enum value of RetestStimulusSeen.
	 */
	public void setStimulusResults(EnumRetestStimulusSeen enumRetestStimulusSeen)
	{
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0024,0x0095, enumRetestStimulusSeen.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0095,DicomConstants.DCM_DT_CS, enumRetestStimulusSeen.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return Retest Stimulus Seen
	 * @return String - value of Retest Stimulus Seen
	 */
	public String getRetestStimulusSeen()
	{
		Object objValue = getValue(0x0024,0x0095);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Retest Sensitivity Value
	 * @param fRetestSensitivityValue Float value of Retest Sensitivity Value
	 */
	public void setRetestSensitivityValue( float fRetestSensitivityValue)
	{
		/*
		 * Accept the Retest Sensitivity 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,0x0096, fRetestSensitivityValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0096,DicomConstants.DCM_DT_FL, fRetestSensitivityValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Retest Sensitivity Value
	 * @return float - Float value of Retest Sensitivity Value.
	 */
	public float getRetestSensitivityValue()
	{
		Object objValue = getValue(0x0024,0x0096);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Quantified Defect
	 * @param fQuantifiedDefect Float value of Quantified Defect
	 */
	public void setQuantifiedDefect( float fQuantifiedDefect)
	{
		/*
		 * Accept the Retest Sensitivity 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,0x0098, fQuantifiedDefect,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0024,0x0098,DicomConstants.DCM_DT_FL, fQuantifiedDefect);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Quantified Defect
	 * @return float - Float value of Quantified Defect.
	 */
	public float getQuantifiedDefect()
	{
		Object objValue = getValue(0x0024,0x0098);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Retrieves pre-initialized object of Visual Field Test Point Normals 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 VisualFieldTestPointNormalsSequence - instance of Visual Field Test Point Normals Sequence. 
	 */
	public VisualFieldTestPointNormalsSequence getVisualFieldTestPointNormalsSequence()
	{
		VisualFieldTestPointNormalsSequence objVisualFieldTestPointNormalsSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0024,0x0097); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objVisualFieldTestPointNormalsSequence = (VisualFieldTestPointNormalsSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objVisualFieldTestPointNormalsSequence = (VisualFieldTestPointNormalsSequence)objDicomV3Factory.createSequence(0x0024,0x0097, objITransferSyntax, objIDictionaryReader);

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