/*******************************************************************************
 *  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.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPupilDilated;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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;

/**
 * This class contains the Macro that describes patient clinical conditions related to an Ophthalmic Acquisition Parameters.
 * @author saloni
 *
 */
public class OphthalmicAcquisitionParametersMacro extends Macro   
{
	

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

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public OphthalmicAcquisitionParametersMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}

	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public OphthalmicAcquisitionParametersMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public OphthalmicAcquisitionParametersMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public OphthalmicAcquisitionParametersMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Validates all the attributes in OphthalmicAcquisitionParametersMacro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}

	/**
	 * Validates all the attributes in OphthalmicAcquisitionParametersMacro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{ 

			//validate condition here
			if(!objIDataSet.isPresent(0x0022,0x001B))
				throw new MandatoryMissingException("0022,001B");
			if(!objIDataSet.isPresent(0x0022,0x000A))
				throw new MandatoryMissingException("0022,000A");
			if(!objIDataSet.isPresent(0x0022,0x000B))
				throw new MandatoryMissingException("0022,000B");
			if(!objIDataSet.isPresent(0x0022,0x000D))
				throw new MandatoryMissingException("0022,000D");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( OphthalmicAcquisitionParametersMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(OphthalmicAcquisitionParametersMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in OphthalmicAcquisitionParametersMacro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{  
			//validate condition here
			if(!objIItem.contains(0x0022,0x001B))
				throw new MandatoryMissingException("0022,001B");
			if(!objIItem.contains(0x0022,0x000A))
				throw new MandatoryMissingException("0022,000A");
			if(!objIItem.contains(0x0022,0x000B))
				throw new MandatoryMissingException("0022,000B");
			if(!objIItem.contains(0x0022,0x000D))
				throw new MandatoryMissingException("0022,000D");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(OphthalmicAcquisitionParametersMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(OphthalmicAcquisitionParametersMacro.class.getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Retrieves pre-initialized object of Steep Refractive State 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 RefractiveStateSequence - instance of RefractiveStateSequence. 
	 */
	public RefractiveStateSequence getRefractiveStateSequence()
	{
		RefractiveStateSequence objRefractiveStateSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0022, 0x001B); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRefractiveStateSequence = (RefractiveStateSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRefractiveStateSequence = (RefractiveStateSequence)objDicomV3Factory.createSequence(0x0022, 0x001B, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0022, 0x001B, objRefractiveStateSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0022, 0x001B, DicomConstants.DCM_DT_SQ, objRefractiveStateSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objRefractiveStateSequence;
	}
	
	/**
	 * Sets the Emmetropic Magnification.
	 * Emmetropic magnification value (dimensionless).
	 * @param fEmmetropicMagnification Float value of the Emmetropic Magnification
	 */
	public void setEmmetropicMagnification( float fEmmetropicMagnification)
	{
		/*
		 * Accept the Emmetropic Magnification 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(0x0022,0x000A, fEmmetropicMagnification, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0022,0x000A,DicomConstants.DCM_DT_FL, fEmmetropicMagnification);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Emmetropic Magnification.
	 * Emmetropic magnification value (dimensionless).
	 * @return float - Float value of the Emmetropic Magnification
	 */
	public float getEmmetropicMagnification()
	{
		Object objValue = getValue(0x0022,0x000A);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Intra Ocular Pressure.
	 * Value of intraocular pressure in mmHg.
	 * @param fIntraOcularPressure Float value of the Intra Ocular Pressure
	 */
	public void setIntraOcularPressure( float fIntraOcularPressure)
	{
		/*
		 * Accept the Intra Ocular Pressure 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(0x0022,0x000B, fIntraOcularPressure, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0022,0x000B,DicomConstants.DCM_DT_FL, fIntraOcularPressure);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Intra Ocular Pressure.
	 * Value of intraocular pressure in mmHg.
	 * @return float - Float value of the Intra Ocular Pressure
	 */
	public float getIntraOcularPressure()
	{
		Object objValue = getValue(0x0022,0x000B);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets type of Pupil Dilated
	 * Whether or not the patient's pupils were pharmacologically dilated for this acquisition.
	 * Defined Terms:
	 * 1)YES	0022
	 * 2)NO
	 * @param enumPupilDilated enum value of PupilDilated.
	 */
	public void setPupilDilated(EnumPupilDilated enumPupilDilated)
	{
		/* Accept the PupilDilated 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(0x0022,0x000D, enumPupilDilated.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0022,0x000D,DicomConstants.DCM_DT_CS, enumPupilDilated.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return type of PupilDilated .
	 * @return String - value of PupilDilated.
	 */
	public String getPupilDilated()
	{
		Object objValue = getValue(0x0022,0x000D);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Mydriatic Agent 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 MydriaticAgentSequence - instance of Mydriatic Agent Sequence. 
	 */
	public MydriaticAgentSequence getMydriaticAgentSequence()
	{
		MydriaticAgentSequence objMydriaticAgentSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0022, 0x0058); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objMydriaticAgentSequence = (MydriaticAgentSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objMydriaticAgentSequence = (MydriaticAgentSequence)objDicomV3Factory.createSequence(0x0022, 0x0058, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0022, 0x0058, objMydriaticAgentSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0022, 0x0058, DicomConstants.DCM_DT_SQ, objMydriaticAgentSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objMydriaticAgentSequence;
	}
	
	/**
	 * Sets the Degree of Dilation.
	 * @param fDegreeofDilation Float value of the DegreeofDilation
	 */
	public void setDegreeofDilation( float fDegreeofDilation)
	{
		/*
		 * Accept the DegreeofDilation 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(0x0022,0x000E, fDegreeofDilation,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0022,0x000E,DicomConstants.DCM_DT_FL, fDegreeofDilation);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the DegreeofDilation.
	 * @return float - Float value of the DegreeofDilation .
	 */
	public float getDegreeofDilation()
	{
		Object objValue = getValue(0x0022,0x000E);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}
