/*******************************************************************************
 *  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.GeneralAnatomyMandatoryMacro;
import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.baselibs.TransformationAlgorithmSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.TransformationMethodCodeSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.TwoDimensionalToThreeDimensionalMapSequence;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumOphthalmicAxialLengthMethod;

import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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 Wide Field Ophthalmic Photography 3D Coordinates Module.
*/
public class WideFieldOphthalmicPhotography3DCoordinatesModule extends Module   
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;
	/**
	 * The obj general anatomy mandatory macro.
	 */
	GeneralAnatomyMandatoryMacro objGeneralAnatomyMandatoryMacro = null;
	
	/**
	 * Instantiates aWide Field Ophthalmic Photography 3D Coordinates Module 
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public WideFieldOphthalmicPhotography3DCoordinatesModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0022,0x1512));
		objSingleCollection.add(new Tag(0x0022,0x1513));
		objSingleCollection.add(new Tag(0x0022,0x1019));
		objSingleCollection.add(new Tag(0x0022,0x1515));
		objSingleCollection.add(new Tag(0x0022,0x1517));
		objSingleCollection.add(new Tag(0x0022,0x1518));
	}

	/**
	 * 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 boolean - returns validation status as success or failure
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */	
	@Override
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			/*
		 * Validation Rules for Mandatory Elements
		 */
			getGeneralAnatomyMandatoryMacro().validate();
			if(!isPresent(0x0022, 0x1512))
				throw new MandatoryMissingException("0022,1512");
			if(!isPresent(0x0022, 0x1513))
				throw new MandatoryMissingException("0022,1513");
			if(!isPresent(0x0022,0x1019))
				throw new MandatoryMissingException("0022,1019");
			if(!isPresent(0x0022,0x1515))
				throw new MandatoryMissingException("0022,1515");
			if(!isPresent(0x0022,0x1518))
				throw new ConditionFailedException("0022,1518");
		}
		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);

		}
		catch(InvalidMacroException ve)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ve.getMessage());

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

		}
		return true;
	}
	/**
	 * Returns instance of GeneralAnatomyMandatoryMacro.
	 * 
	 * @return Instance of GeneralAnatomyMandatoryMacro
	 */
	public GeneralAnatomyMandatoryMacro getGeneralAnatomyMandatoryMacro()
	{
		if(objGeneralAnatomyMandatoryMacro == null)
		{
			objGeneralAnatomyMandatoryMacro = new GeneralAnatomyMandatoryMacro(objIDataSet,objIDictionaryReader);
		}
		return objGeneralAnatomyMandatoryMacro;
	}
	/**
	 * This method provides the pre-initialized object of Transformation Method CodeSequence.
	 * 
	 * @return - object of Transformation Method CodeSequence. 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 TransformationMethodCodeSequence getTransformationMethodCodeSequence()
	{
		TransformationMethodCodeSequence objTransformationMethodCodeSequence = (TransformationMethodCodeSequence)getDicomValue(0x0022, 0x1512);
		if(objTransformationMethodCodeSequence == 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();
			objTransformationMethodCodeSequence = (TransformationMethodCodeSequence)objDicomV3Factory.createSequence(0x0022, 0x1512, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0022, 0x1512, DicomConstants.DCM_DT_SQ, objTransformationMethodCodeSequence.getValue());
		}
		return objTransformationMethodCodeSequence;
	}
	
	/**
	 * This method provides the pre-initialized object of Transformation Algorithm Sequence.
	 * 
	 * @return - object of RTransformationAlgorithmSequence. 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 TransformationAlgorithmSequence getTransformationAlgorithmSequence()
	{
		TransformationAlgorithmSequence objTransformationAlgorithmSequence = (TransformationAlgorithmSequence)getDicomValue(0x0022, 0x1513);
		if(objTransformationAlgorithmSequence == 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();
			objTransformationAlgorithmSequence = (TransformationAlgorithmSequence)objDicomV3Factory.createSequence(0x0022, 0x1513, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0022, 0x1513, DicomConstants.DCM_DT_SQ, objTransformationAlgorithmSequence.getValue());
		}
		return objTransformationAlgorithmSequence;
	}
	
	/**
	 * Sets the OphthalmicAxialLength.
	 * @param fOphthalmicAxialLength Float value of the Ratio of OphthalmicAxialLength
	 */
	public void setOphthalmicAxialLength( float fOphthalmicAxialLength)
	{
		/*
		 * Accept the Horizontal Field of View 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(0x0022,0x1019, DicomConstants.DCM_DT_FL,fOphthalmicAxialLength);
	}

	/**
	 * Retrieves the Ratio of OphthalmicAxialLength.
	 * @return float - Float value of the OphthalmicAxialLength
	 */
	public float getOphthalmicAxialLength()
	{
		Object objValue = getValue(0x0022,0x1019);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Ophthalmic AxialLength Method.
	 * The method used to obtain the Ophthalmic Axial Length.
	 * Enumerated Values:
	 * 1) MEASURED = Measured axial length.
	 * 2) ESTIMATED = An estimated value based upon performing the examination.
	 * 3) POPULATION = A length that represents a population norm.
	 * 
	 * @param enumOphthalmicAxialLengthMethod the enum approval status
	 */
	public void setApprovalStatus(EnumOphthalmicAxialLengthMethod  enumOphthalmicAxialLengthMethod)
	{
		/* Accept OphthalmicAxialLengthMethod 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(0x0022,0x1515, DicomConstants.DCM_DT_CS, enumOphthalmicAxialLengthMethod.getValue());
	}

	/**
	 * Retrieves OphthalmicAxialLengthMethod.
	 * 
	 * @return String - String value of OphthalmicAxialLengthMethod.
	 */ 
	public String getOphthalmicAxialLengthMethods()
	{
		Object objValue = getValue(0x0022,0x1515);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Ophthalmic FOV
	 * @param fOphthalmicFOV Float value of the Ophthalmic FOV
	 */
	public void setOphthalmicFOV( float fOphthalmicFOV)
	{
		/*
		 * Accept the Ophthalmic FOV 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(0x0022,0x1517, DicomConstants.DCM_DT_FL,fOphthalmicFOV);
	}
	/**
	 * Retrieves Ophthalmic FOV
	 * @return float - Float value of the Ophthalmic FOV
	 */
	public float getOphthalmicFOV()
	{
		Object objValue = getValue(0x0022,0x1517);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * This method provides the pre-initialized object of Two Dimensional to ThreeDimensional Map Sequence.
	 * 
	 * @return - object of Two Dimensional to ThreeDimensional Map 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 TwoDimensionalToThreeDimensionalMapSequence getTwoDimensionaltoThreeDimensionalMapSequence()
	{
		TwoDimensionalToThreeDimensionalMapSequence objTwoDimensionaltoThreeDimensionalMapSequence = (TwoDimensionalToThreeDimensionalMapSequence)getDicomValue(0x0022, 0x1518);
		if(objTwoDimensionaltoThreeDimensionalMapSequence == 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();
			objTwoDimensionaltoThreeDimensionalMapSequence = (TwoDimensionalToThreeDimensionalMapSequence)objDicomV3Factory.createSequence(0x0022, 0x1518, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0022, 0x1518, DicomConstants.DCM_DT_SQ, objTwoDimensionaltoThreeDimensionalMapSequence.getValue());
		}
		return objTwoDimensionaltoThreeDimensionalMapSequence;
	}
	
}

