/*******************************************************************************
 *  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.dicommodel.modules;

import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.ResultsNormalsSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.baselibs.VisualFieldGlobalResultsIndexSequence;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCorrectedLocalizedDeviationFromNormalCalculated;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCorrectedLocalizedDeviationFromNormalProbabilityCalculated;
import cdac.medinfo.sdk.dcm30_04.enums.EnumShortTermFluctuationCalculated;
import cdac.medinfo.sdk.dcm30_04.enums.EnumShortTermFluctuationProbabilityCalculated;

import cdac.medinfo.sdk.dcm30_04.enums.EnumVisualFieldTestNormalsFlag;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;
//TODO: Auto-generated Javadoc
/**
\* This module describes attributes related to Visual Field Static Perimetry Test Results Module.
*/
public class VisualFieldStaticPerimetryTestResultsModule extends Module  
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	/**
	 * Creates Visual Field Static Perimetry Test Results Module with default initialization.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public VisualFieldStaticPerimetryTestResultsModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0024,0x0070));
		objSingleCollection.add(new Tag(0x0024,0x0063));
		objSingleCollection.add(new Tag(0x0024,0x0064));
		objSingleCollection.add(new Tag(0x0024,0x0074));
		objSingleCollection.add(new Tag(0x0024,0x0075));
		objSingleCollection.add(new Tag(0x0024,0x0076));
		objSingleCollection.add(new Tag(0x0024,0x0077));
		objSingleCollection.add(new Tag(0x0024,0x0078));
		objSingleCollection.add(new Tag(0x0024,0x0079));
		objSingleCollection.add(new Tag(0x0024,0x0080));
		objSingleCollection.add(new Tag(0x0024,0x0081));
		objSingleCollection.add(new Tag(0x0024,0x0320));
		
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return true, if validate module
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			/*
		 * Validation Rules for Mandatory Elements
		 */
			if(!isPresent(0x0024,0x0063))
				throw new MandatoryMissingException("0024,0063");
			if(!isPresent(0x0024,0x0074))
				throw new MandatoryMissingException("0024,0074");
			if(!isPresent(0x0024,0x0076))
				throw new MandatoryMissingException("0024,0076");
			if(!isPresent(0x0024,0x0078))
				throw new MandatoryMissingException("0024,0078");
			if(!isPresent(0x0024,0x0080))
				throw new MandatoryMissingException("0024,0080");
			
			if(isPresent(0x0024,0x0063) && getVisualFieldTestNormalsFlag().equalsIgnoreCase(EnumVisualFieldTestNormalsFlag.YES.getValue()))
				if(!isPresent(0x0024,0x0064))
					throw new ConditionFailedException("0x0024,0x0064");
			
			if(isPresent(0x0024,0x0074) && getShortTermFluctuationCalculated().equalsIgnoreCase(EnumShortTermFluctuationCalculated.YES.getValue()))
				if(!isPresent(0x0024,0x0075))
					throw new ConditionFailedException("0x0024,0x0075");
			
			if(isPresent(0x0024,0x0076) && getShortTermFluctuationProbabilityCalculated().equalsIgnoreCase(EnumShortTermFluctuationProbabilityCalculated.YES.getValue()))
				if(!isPresent(0x0024,0x0077))
					throw new ConditionFailedException("0x0024,0x0077");
			
			if(isPresent(0x0024,0x0078) && getCorrectedLocalizedDeviationFromNormalCalculated().equalsIgnoreCase(EnumCorrectedLocalizedDeviationFromNormalCalculated.YES.getValue()))
				if(!isPresent(0x0024,0x0079))
					throw new ConditionFailedException("0x0024,0x0079");
			
			if(isPresent(0x0024,0x0080) && getCorrectedLocalizedDeviationFromNormalProbabilityCalculated().equalsIgnoreCase(EnumCorrectedLocalizedDeviationFromNormalProbabilityCalculated.YES.getValue()))
				if(!isPresent(0x0024,0x0081))
					throw new ConditionFailedException("0x0024,0x0081");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),mme);

		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),cfe);

		}
		return true;
	}
	
	/**
	 * Sets VisualFieldMeanSensitivity.
	 * Average sensitivity of the test points of the visual field, in dB.
	 * @param fVisualFieldMeanSensitivity Float value of the VisualFieldMeanSensitivity
	 */
	public void setVisualFieldMeanSensitivity( float fVisualFieldMeanSensitivity)
	{
		/*
		 * Accept the VisualFieldMeanSensitivity 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
		 */
		addDataElement(0x0024,0x0070, DicomConstants.DCM_DT_FL,fVisualFieldMeanSensitivity);
	}
	/**
	 * Retrieves VisualFieldMeanSensitivity.
	 * Average sensitivity of the test points of the visual field, in dB.
	 * @return float - Float value of the VisualFieldMeanSensitivity
	 */
	public float getVisualFieldMeanSensitivity()
	{
		Object objValue = getValue(0x0024,0x0070);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Visual Field Test Normals Flag.
	 * Whether normals exist for this patient's results.
	 * Enumerated Values:
	 * 1) YES
	 * 2) NO
	 * 
	 * @param enumVisualFieldTestNormalsFlag the enum Visual Field Test Normals Flag
	 */
	public void setVisualFieldTestNormalsFlag(EnumVisualFieldTestNormalsFlag  enumVisualFieldTestNormalsFlag)
	{
		/* Accept Visual Field Test Normals 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
		 */
		addDataElement(0x0024, 0x0063, DicomConstants.DCM_DT_CS, enumVisualFieldTestNormalsFlag.getValue());
	}

	/**
	 * Retrieves Visual Field Test Normals Flag.
	 * 
	 * @return String - String value of Visual Field Test Normals Flag.
	 */ 
	public String getVisualFieldTestNormalsFlag()
	{
		Object objValue = getValue(0x0024,0x0063);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * This method provides the pre-initialized object of Results Normals Sequence.
	 * 
	 * @return - object of Results 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
	 */
	public ResultsNormalsSequence getResultsNormalsSequence()
	{
		ResultsNormalsSequence objResultsNormalsSequence = (ResultsNormalsSequence)getDicomValue(0x0024, 0x0064);
		if(objResultsNormalsSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objResultsNormalsSequence = (ResultsNormalsSequence)objDicomV3Factory.createSequence(0x0024, 0x0064, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0024, 0x0064, DicomConstants.DCM_DT_SQ, objResultsNormalsSequence.getValue());
		}
		return objResultsNormalsSequence;
	}
	
	/**
	 * Sets Short Term Fluctuation Calculated.
	 * Whether the short term fluctuation was calculated.
	 * Enumerated Values:
	 * 1) YES
	 * 2) NO
	 * 
	 * @param enumShortTermFluctuationCalculated the enum Short Term Fluctuation Calculated
	 */
	public void setShortTermFluctuationCalculated(EnumShortTermFluctuationCalculated  enumShortTermFluctuationCalculated)
	{
		/* Accept Short Term Fluctuation Calculated 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
		 */
		addDataElement(0x0024, 0x0074, DicomConstants.DCM_DT_CS, enumShortTermFluctuationCalculated.getValue());
	}

	/**
	 * Retrieves Short Term Fluctuation Calculated.
	 * 
	 * @return String - String value of Short Term Fluctuation Calculated.
	 */ 
	public String getShortTermFluctuationCalculated()
	{
		Object objValue = getValue(0x0024,0x0074);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Short Term Fluctuation.
	 * Average deviation of sensitivity for the repeated test locations,in dB.
	 * @param fShortTermFluctuation Float value of the Short Term Fluctuation
	 */
	public void setShortTermFluctuation( float fShortTermFluctuation)
	{
		/*
		 * Accept the Short Term Fluctuation 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
		 */
		addDataElement(0x0024,0x0075, DicomConstants.DCM_DT_FL,fShortTermFluctuation);
	}
	/**
	 * Retrieves Short Term Fluctuation.
	 * Average deviation of sensitivity for the repeated test locations,in dB.
	 * @return float - Float value of the Short Term Fluctuation
	 */
	public float getShortTermFluctuation()
	{
		Object objValue = getValue(0x0024,0x0075);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Short Term Fluctuation Probability Calculated.
	 * Whether the short term fluctuation probability was calculated
	 * Enumerated Values:
	 * 1) YES
	 * 2) NO
	 * 
	 * @param enumShortTermFluctuationProbabilityCalculated the enum Short Term Fluctuation Probability Calculated
	 */
	public void setShortTermFluctuationProbabilityCalculated(EnumShortTermFluctuationProbabilityCalculated  enumShortTermFluctuationProbabilityCalculated)
	{
		/* Accept Short Term Fluctuation Probability Calculated 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
		 */
		addDataElement(0x0024, 0x0076, DicomConstants.DCM_DT_CS, enumShortTermFluctuationProbabilityCalculated.getValue());
	}

	/**
	 * Retrieves Short Term Fluctuation Probability Calculated.
	 * 
	 * @return String - String value of Short Term Fluctuation Probability Calculated.
	 */ 
	public String getShortTermFluctuationProbabilityCalculated()
	{
		Object objValue = getValue(0x0024,0x0076);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Short Term Fluctuation Probability.
	 * The percentile of the Short Term Fluctuation (0024,0075) value within the normal population, in percent.
	 * @param fShortTermFluctuationProbability Float value of the Short Term Fluctuation Probability
	 */
	public void setShortTermFluctuationProbability( float fShortTermFluctuationProbability)
	{
		/*
		 * Accept the Short Term Fluctuation Probability 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
		 */
		addDataElement(0x0024,0x0077, DicomConstants.DCM_DT_FL,fShortTermFluctuationProbability);
	}
	/**
	 * Retrieves Short Term Fluctuation Probability.
	 * The percentile of the Short Term Fluctuation (0024,0075) value within the normal population, in percent.
	 * @return float - Float value of the Short Term Fluctuation Probability
	 */
	public float getShortTermFluctuationProbability()
	{
		Object objValue = getValue(0x0024,0x0077);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Corrected Localized Deviation From Normal Calculated.
	 * Whether the corrected localized deviation from normal was calculated.
	 * 1) YES
	 * 2) NO
	 * 
	 * @param enumCorrectedLocalizedDeviationFromNormalCalculated the enum Corrected Localized Deviation From Normal Calculated
	 */
	public void setCorrectedLocalizedDeviationFromNormalCalculated(EnumCorrectedLocalizedDeviationFromNormalCalculated  enumCorrectedLocalizedDeviationFromNormalCalculated)
	{
		/* Accept Corrected Localized Deviation From Normal Calculated 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
		 */
		addDataElement(0x0024, 0x0078, DicomConstants.DCM_DT_CS, enumCorrectedLocalizedDeviationFromNormalCalculated.getValue());
	}

	/**
	 * Retrieves Corrected Localized Deviation From Normal Calculated.
	 * 
	 * @return String - String value of Corrected Localized Deviation From Normal Calculated.
	 */ 
	public String getCorrectedLocalizedDeviationFromNormalCalculated()
	{
		Object objValue = getValue(0x0024,0x0078);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Corrected Localized Deviation From Normal.
	 * Weighted square root of loss variance corrected for short term fluctuation, in dB.
	 * @param fCorrectedLocalizedDeviationFromNormal Float value of the Corrected Localized Deviation From Normal
	 */
	public void setCorrectedLocalizedDeviationFromNormal( float fCorrectedLocalizedDeviationFromNormal)
	{
		/*
		 * Accept the Corrected Localized Deviation From Normal 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
		 */
		addDataElement(0x0024,0x0079, DicomConstants.DCM_DT_FL,fCorrectedLocalizedDeviationFromNormal);
	}
	/**
	 * Retrieves Corrected Localized Deviation From Normal.
	 * Weighted square root of loss variance corrected for short term fluctuation, in dB.
	 * @return float - Float value of the Corrected Localized Deviation From Normal
	 */
	public float getCorrectedLocalizedDeviationFromNormal()
	{
		Object objValue = getValue(0x0024,0x0079);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Corrected Localized Deviation From Normal Probability Calculated.
	 * Whether the corrected localized deviation from Normal probability was calculated.
	 * 1) YES
	 * 2) NO
	 * 
	 * @param enumCorrectedLocalizedDeviationFromNormalProbabilityCalculated the enum Corrected Localized Deviation From Normal Probability Calculated
	 */
	public void setCorrectedLocalizedDeviationFromNormalProbabilityCalculated(EnumCorrectedLocalizedDeviationFromNormalProbabilityCalculated  enumCorrectedLocalizedDeviationFromNormalProbabilityCalculated)
	{
		/* Accept Corrected Localized Deviation From Normal Calculated 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
		 */
		addDataElement(0x0024, 0x0080, DicomConstants.DCM_DT_CS, enumCorrectedLocalizedDeviationFromNormalProbabilityCalculated.getValue());
	}

	/**
	 * Retrieves Corrected Localized Deviation From Normal Probability Calculated.
	 * 
	 * @return String - String value of Corrected Localized Deviation From Normal Probability Calculated.
	 */ 
	public String getCorrectedLocalizedDeviationFromNormalProbabilityCalculated()
	{
		Object objValue = getValue(0x0024,0x0080);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Corrected Localized Deviation From Normal Probability.
	 * The percentile of the Corrected Localized Deviation FromNormal (0024,0079) value within the normal population, in percent.
	 * @param fCorrectedLocalizedDeviationFromNormalProbability Float value of the Corrected Localized Deviation From Normal Probability
	 */
	public void setCorrectedLocalizedDeviationFromNormalProbability( float fCorrectedLocalizedDeviationFromNormalProbability)
	{
		/*
		 * Accept the Corrected Localized Deviation From Normal Probability 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
		 */
		addDataElement(0x0024,0x0081, DicomConstants.DCM_DT_FL,fCorrectedLocalizedDeviationFromNormalProbability);
	}
	/**
	 * Retrieves Corrected Localized Deviation From Normal Probability.
	 * @return float - Float value of the Corrected Localized Deviation From Normal Probability
	 */
	public float getCorrectedLocalizedDeviationFromNormalProbability()
	{
		Object objValue = getValue(0x0024,0x0081);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * This method provides the pre-initialized object of Visual Field GlobalResults Index Sequence.
	 * 
	 * @return - object of Visual Field GlobalResults Index 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
	 */
	public VisualFieldGlobalResultsIndexSequence getVisualFieldGlobalResultsIndexSequence()
	{
		VisualFieldGlobalResultsIndexSequence objVisualFieldGlobalResultsIndexSequence = (VisualFieldGlobalResultsIndexSequence)getDicomValue(0x0024, 0x0320);
		if(objVisualFieldGlobalResultsIndexSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objVisualFieldGlobalResultsIndexSequence = (VisualFieldGlobalResultsIndexSequence)objDicomV3Factory.createSequence(0x0024, 0x0320, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0024, 0x0320, DicomConstants.DCM_DT_SQ, objVisualFieldGlobalResultsIndexSequence.getValue());
		}
		return objVisualFieldGlobalResultsIndexSequence;
	}
	
	/**
	 * This method returns the collection of keys representing the DataElements that belong to this module.
	 * 
	 * @return Collection of Tags - returns the collection containing the tags
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
