/*******************************************************************************
 *  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.BackgroundIlluminationColorCodeSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.ScreeningTestModeCodeSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.StimulusColorCodeSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;

import cdac.medinfo.sdk.dcm30_04.enums.EnumVisualFieldShape;
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 Parameters Module.
*/
public class VisualFieldStaticPerimetryTestParametersModule extends Module  
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


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

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0024,0x0010));
		objSingleCollection.add(new Tag(0x0024,0x0011));
		objSingleCollection.add(new Tag(0x0024,0x0012));
		objSingleCollection.add(new Tag(0x0024,0x0016));
		objSingleCollection.add(new Tag(0x0024,0x0018));
		objSingleCollection.add(new Tag(0x0024,0x0020));
		objSingleCollection.add(new Tag(0x0024,0x0021));
		objSingleCollection.add(new Tag(0x0024,0x0024));
		objSingleCollection.add(new Tag(0x0024,0x0025));
		objSingleCollection.add(new Tag(0x0024,0x0028));
	}

	/**
	 * 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,0x0010))
				throw new MandatoryMissingException("0024,0010");
			if(!isPresent(0x0024,0x0011))
				throw new MandatoryMissingException("0024,0011");
			if(!isPresent(0x0024,0x0012))
				throw new MandatoryMissingException("0024,0012");
			if(!isPresent(0x0024,0x0018))
				throw new MandatoryMissingException("0024,0018");
			if(!isPresent(0x0024,0x0020))
				throw new MandatoryMissingException("0024,0020");
			if(!isPresent(0x0024,0x0021))
				throw new MandatoryMissingException("0024,0021");
			if(!isPresent(0x0024,0x0024))
				throw new MandatoryMissingException("0024,0024");
			if(!isPresent(0x0024,0x0025))
				throw new MandatoryMissingException("0024,0025");
			if(!isPresent(0x0024,0x0028))
				throw new MandatoryMissingException("0024,0028");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

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

		}
		return true;
	}

	/**
	 * Sets Visual Field Horizontal Extent.
	 * The maximum horizontal angular subtend (diameter or width) of the tested visual field, in degrees.
	 * @param fVisualFieldHorizontalExtent Float value of the Visual Field Horizontal Extent
	 */
	public void setVisualFieldHorizontalExtent( float fVisualFieldHorizontalExtent)
	{
		/*
		 * Accept the Visual Field Horizontal Extent 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,0x0010, DicomConstants.DCM_DT_FL,fVisualFieldHorizontalExtent);
	}
	/**
	 * Retrieves Visual Field Horizontal Extent.
	 * The maximum horizontal angular subtend (diameter or width) of the tested visual field, in degrees.
	 * @return float - Float value of the Visual Field Horizontal Extent
	 */
	public float getVisualFieldHorizontalExtent()
	{
		Object objValue = getValue(0x0024,0x0010);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Visual Field Vertical Extent.
	 * The maximum vertical angular subtend (diameter or height) of the tested visual field, in degrees.
	 * @param fVisualFieldVerticalExtent Float value of the Visual Field Vertical Extent
	 */
	public void setVisualFieldVerticalExtent( float fVisualFieldVerticalExtent)
	{
		/*
		 * Accept the Visual Field Horizontal Extent 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,0x0011, DicomConstants.DCM_DT_FL,fVisualFieldVerticalExtent);
	}
	/**
	 * Retrieves Visual Field Vertical Extent.
	 * The maximum vertical angular subtend (diameter or height) of the tested visual field, in degrees.
	 * @return float - Float value of the Visual Field Vertical Extent
	 */
	public float getVisualFieldVerticalExtent()
	{
		Object objValue = getValue(0x0024,0x0011);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Visual Field Shape.
	 * The shape of the visual field tested.
	 * Enumerated Values:
	 * 1) RECTANGLE
	 * 2) CIRCLE
	 * 3) ELLIPSE
	 * 
	 * @param enumVisualFieldShape the enum Visual Field Shape
	 */
	public void setVisualFieldShape(EnumVisualFieldShape  enumVisualFieldShape)
	{
		/* Accept Visual Field Shape 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, 0x0012, DicomConstants.DCM_DT_CS, enumVisualFieldShape.getValue());
	}

	/**
	 * Retrieves Visual Field Shape.
	 * The shape of the visual field tested.
	 * @return String - String value of Visual Field Shape.
	 */ 
	public String getVisualFieldShape()
	{
		Object objValue = getValue(0x0024,0x0012);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * This method provides the pre-initialized object of Screening Test Mode Code Sequence.
	 * 
	 * @return - object of Screening Test Mode Code 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 ScreeningTestModeCodeSequence getScreeningTestModeCodeSequence()
	{
		ScreeningTestModeCodeSequence objScreeningTestModeCodeSequence = (ScreeningTestModeCodeSequence)getDicomValue(0x0024, 0x0016);
		if(objScreeningTestModeCodeSequence == 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();
			objScreeningTestModeCodeSequence = (ScreeningTestModeCodeSequence)objDicomV3Factory.createSequence(0x0024, 0x0016, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0024, 0x0016, DicomConstants.DCM_DT_SQ, objScreeningTestModeCodeSequence.getValue());
		}
		return objScreeningTestModeCodeSequence;
	}
	
	/**
	 * Sets Maximum Stimulus Luminance.
	 * Maximum luminance of stimulus, in candelas per square meter(cd/m²).
	 * @param fMaximumStimulusLuminance Float value of the Maximum Stimulus Luminance
	 */
	public void setMaximumStimulusLuminance( float fMaximumStimulusLuminance)
	{
		/*
		 * Accept the Maximum Stimulus Luminance 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,0x0018, DicomConstants.DCM_DT_FL,fMaximumStimulusLuminance);
	}
	/**
	 * Retrieves Maximum Stimulus Luminance.
	 * Maximum luminance of stimulus, in candelas per square meter(cd/m²).
	 * @return float - Float value of the Maximum Stimulus Luminance
	 */
	public float getMaximumStimulusLuminance()
	{
		Object objValue = getValue(0x0024,0x0018);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Background Luminance.
	 * Background luminance of the device, in candelas per squaremeter (cd/m²).
	 * @param fBackgroundLuminance Float value of the Background Luminance
	 */
	public void setBackgroundLuminance( float fBackgroundLuminance)
	{
		/*
		 * Accept the Background Luminance 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,0x0020, DicomConstants.DCM_DT_FL,fBackgroundLuminance);
	}
	/**
	 * Retrieves Background Luminance.
	 * Background luminance of the device, in candelas per squaremeter (cd/m²).
	 * @return float - Float value of the Background Luminance
	 */
	public float getBackgroundLuminance()
	{
		Object objValue = getValue(0x0024,0x0020);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * This method provides the pre-initialized object of Stimulus Color Code Sequence.
	 * 
	 * @return - object of Stimulus Color Code 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 StimulusColorCodeSequence getStimulusColorCodeSequence()
	{
		StimulusColorCodeSequence objStimulusColorCodeSequence = (StimulusColorCodeSequence)getDicomValue(0x0024, 0x0021);
		if(objStimulusColorCodeSequence == 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();
			objStimulusColorCodeSequence = (StimulusColorCodeSequence)objDicomV3Factory.createSequence(0x0024, 0x0021, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0024, 0x0021, DicomConstants.DCM_DT_SQ, objStimulusColorCodeSequence.getValue());
		}
		return objStimulusColorCodeSequence;
	}
	
	/**
	 * This method provides the pre-initialized object of Background Illumination Color Code Sequence.
	 * 
	 * @return - object of Background Illumination Color Code 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 BackgroundIlluminationColorCodeSequence getBackgroundIlluminationColorCodeSequence()
	{
		BackgroundIlluminationColorCodeSequence objBackgroundIlluminationColorCodeSequence = (BackgroundIlluminationColorCodeSequence)getDicomValue(0x0024, 0x0024);
		if(objBackgroundIlluminationColorCodeSequence == 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();
			objBackgroundIlluminationColorCodeSequence = (BackgroundIlluminationColorCodeSequence)objDicomV3Factory.createSequence(0x0024, 0x0024, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0024, 0x0024, DicomConstants.DCM_DT_SQ, objBackgroundIlluminationColorCodeSequence.getValue());
		}
		return objBackgroundIlluminationColorCodeSequence;
	}
	
	/**
	 * Sets Stimulus Area.
	 * Area of light stimulus presented to the patient, in degrees squared.
	 * @param fStimulusArea Float value of the Stimulus Area.
	 */
	public void setStimulusArea( float fStimulusArea)
	{
		/*
		 * Accept the Stimulus Area 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,0x0025, DicomConstants.DCM_DT_FL,fStimulusArea);
	}
	/**
	 * Retrieves Stimulus Area.
	 * Area of light stimulus presented to the patient, in degrees squared.
	 * @return float - Float value of the Stimulus Area.
	 */
	public float getStimulusArea()
	{
		Object objValue = getValue(0x0024,0x0025);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Stimulus Presentation Time.
	 * The duration of time that a light stimulus is presented to a patient per each individual test point, in milliseconds
	 * @param fStimulusPresentationTime Float value of the Stimulus Presentation Time
	 */
	public void setStimulusPresentationTime( float fStimulusPresentationTime)
	{
		/*
		 * Accept the Stimulus Presentation Time 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,0x0028, DicomConstants.DCM_DT_FL,fStimulusPresentationTime);
	}
	/**
	 * Retrieves Stimulus Presentation Time.
	 * The duration of time that a light stimulus is presented to apatient per each individual test point, in milliseconds
	 * @return float - Float value of the Stimulus Presentation Time
	 */
	public float getStimulusPresentationTime()
	{
		Object objValue = getValue(0x0024,0x0028);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * 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;
	}
}
