/*******************************************************************************
 *  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 a MAcro that describes the attributes for the Ophthalmic Axial Measurements.
 * @author saloni
 *
 */

public class OphthalmicAxialMeasurementsMacro extends Macro   
{
	

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

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public OphthalmicAxialMeasurementsMacro(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 OphthalmicAxialMeasurementsMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public OphthalmicAxialMeasurementsMacro(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 OphthalmicAxialMeasurementsMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Validates all the attributes in OphthalmicAxialMeasurementsMacro
	 * @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 OphthalmicAxialMeasurementsMacro 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,0x1024))
				throw new MandatoryMissingException("0022,1024");
			if(!objIDataSet.isPresent(0x0022,0x1025))
				throw new MandatoryMissingException("0022,1025");
			if(!objIDataSet.isPresent(0x0022,0x000D))
				throw new MandatoryMissingException("0022,000D");
			if(!objIDataSet.isPresent(0x0022,0x000E))
				throw new ConditionFailedException("0022,000E");
			if(!objIDataSet.isPresent(0x0022,0x0058))
				throw new ConditionFailedException("0022,0058");
			if(!objIDataSet.isPresent(0x0022,0x1050))
				throw new MandatoryMissingException("0022,1050");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( OphthalmicAxialMeasurementsMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(OphthalmicAxialMeasurementsMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in KeratometricMeasurementsMacro 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,0x1024))
				throw new MandatoryMissingException("0022,1024");
			if(!objIItem.contains(0x0022,0x1025))
				throw new MandatoryMissingException("0022,1025");
			if(!objIItem.contains(0x0022,0x000D))
				throw new MandatoryMissingException("0022,000D");
			if(!objIItem.contains(0x0022,0x000E))
				throw new ConditionFailedException("0022,000E");
			if(!objIItem.contains(0x0022,0x0058))
				throw new ConditionFailedException("0022,0058");
			if(!objIItem.contains(0x0022,0x1050))
				throw new MandatoryMissingException("0022,1050");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(KeratometricMeasurementsMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(KeratometricMeasurementsMacro.class.getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Retrieves pre-initialized object of Lens Status 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.
	 * @return LensStatusCodeSequence - instance of Lens Status Code Sequence. 
	 */
	public LensStatusCodeSequence getLensStatusCodeSequence()
	{
		LensStatusCodeSequence objLensStatusCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0022,0x1024); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objLensStatusCodeSequence = (LensStatusCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objLensStatusCodeSequence = (LensStatusCodeSequence)objDicomV3Factory.createSequence(0x0022,0x1024, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0022,0x1024, objLensStatusCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0022,0x1024, DicomConstants.DCM_DT_SQ, objLensStatusCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objLensStatusCodeSequence;
	}
	/**
	 * Sets Lens Status Description.
	 * @param strLensStatusDescription String value of Lens Status Description.
	 */
	public void setLensStatusDescription(String strLensStatusDescription)
	{
		/* Accept the LensStatusDescription in String format of type LO.
		 * 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,0x1065, strLensStatusDescription,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0022,0x1065,DicomConstants.DCM_DT_LO, strLensStatusDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for LensStatusDescription.
	 * @return String - value of LensStatusDescription.
	 */
	public String getLensStatusDescription()
	{
		Object objValue = getValue(0x0022,0x1065);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Retrieves pre-initialized object of Vitreous Status 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.
	 * @return VitreousStatusCodeSequence - instance of Vitreous Status Code Sequence. 
	 */
	public VitreousStatusCodeSequence getVitreousStatusCodeSequence()
	{
		VitreousStatusCodeSequence objVitreousStatusCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0022,0x1025); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objVitreousStatusCodeSequence = (VitreousStatusCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objVitreousStatusCodeSequence = (VitreousStatusCodeSequence)objDicomV3Factory.createSequence(0x0022,0x1025, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0022,0x1025, objVitreousStatusCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0022,0x1025, DicomConstants.DCM_DT_SQ, objVitreousStatusCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objVitreousStatusCodeSequence;
	}
	/**
	 * Sets Vitreous Status Description.
	 * @param strVitreousStatusDescription String value of Vitreous Status Description.
	 */
	public void setVitreousStatusDescription(String strVitreousStatusDescription)
	{
		/* Accept the VitreousStatusDescription in String format of type LO.
		 * 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,0x1066, strVitreousStatusDescription,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0022,0x1066,DicomConstants.DCM_DT_LO, strVitreousStatusDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for VitreousStatusDescription.
	 * @return String - value of VitreousStatusDescription.
	 */
	public String getVitreousStatusDescription()
	{
		Object objValue = getValue(0x0022,0x1066);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets type of Pupil Dilated
	 * Whether or not the patient's pupils were pharmacologicallydilated 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;
	}
	/**
	 * 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;
	}
	/**
	 * 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;
	}
	
}
