/*******************************************************************************
 *  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.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 describes the Macro which contains attributes for encoding the algorithm which are used to create or derive a SOP Instance contents. 
 * An algorithm is described by the Algorithm Family, a specific Algorithm Name, and an Algorithm Version. 
 */
public class AlgorithmIdentificationMacro extends Macro   
{

	private static final long serialVersionUID = 1L;

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

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

	/**
	 * Validates all the attributes in AlgorithmIdentificationMacro.
	 * @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 AlgorithmIdentificationMacro 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(0x0066,0x002F))
				throw new MandatoryMissingException("0066,002F");
			if(!objIDataSet.isPresent(0x0066,0x0036))
				throw new MandatoryMissingException("0066,0036");
			if(!objIDataSet.isPresent(0x0066,0x0031))
				throw new MandatoryMissingException("0066,0031");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( AlgorithmIdentificationMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(AlgorithmIdentificationMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in AlgorithmIdentificationMacro 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(0x0066,0x002F))
				throw new MandatoryMissingException("0066,002F");
			if(!objIItem.contains(0x0066,0x0036))
				throw new MandatoryMissingException("0066,0036");
			if(!objIItem.contains(0x0066,0x0031))
				throw new MandatoryMissingException("0066,0031");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(AlgorithmIdentificationMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(AlgorithmIdentificationMacro.class.getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Retrieves pre-initialized object of Algorithm Family 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 AlgorithmFamilyCodeSequence - instance of AlgorithmFamilyCodeSequence. 
	 */
	public AlgorithmFamilyCodeSequence getAlgorithmFamilyCodeSequence()
	{
		AlgorithmFamilyCodeSequence objAlgorithmFamilyCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0066, 0x002F); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objAlgorithmFamilyCodeSequence = (AlgorithmFamilyCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objAlgorithmFamilyCodeSequence = (AlgorithmFamilyCodeSequence)objDicomV3Factory.createSequence(0x0066, 0x002F, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0066, 0x002F, objAlgorithmFamilyCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0066, 0x002F, DicomConstants.DCM_DT_SQ, objAlgorithmFamilyCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objAlgorithmFamilyCodeSequence;
	}
	
	/**
	 * Retrieves pre-initialized object of Algorithm Name 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 AlgorithmNameCodeSequence - instance of AlgorithmNameCodeSequence. 
	 */
	public AlgorithmNameCodeSequence getAlgorithmNameCodeSequence()
	{
		AlgorithmNameCodeSequence objAlgorithmNameCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0066, 0x0030); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objAlgorithmNameCodeSequence = (AlgorithmNameCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objAlgorithmNameCodeSequence = (AlgorithmNameCodeSequence)objDicomV3Factory.createSequence(0x0066, 0x0030, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0066, 0x0030, objAlgorithmNameCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0066, 0x0030, DicomConstants.DCM_DT_SQ, objAlgorithmNameCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objAlgorithmNameCodeSequence;
	}
	
	/**
	 * Sets Algorithm Name.
	 * The name assigned by a manufacturer to a specific software algorithm.
	 * @param strAlgorithmName- String value of Algorithm Name.
	 */
	public void setAlgorithmName(String strAlgorithmName)
	{
		/* Accept the Algorithm Name 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(0x0066,0x0036, strAlgorithmName,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0066,0x0036,DicomConstants.DCM_DT_LO, strAlgorithmName);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Algorithm Name.
	 * The name assigned by a manufacturer to a specific software algorithm.
	 * @return String - value of Algorithm Name.
	 */
	public String getAlgorithmName()
	{
		Object objValue = getValue(0x0066,0x0036);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Algorithm Version.
	 * The software version identifier assigned by a manufacturer to a specific software algorithm.
	 * @param strAlgorithmVersion -String value of Algorithm Version.
	 */
	public void setAlgorithmVersion(String strAlgorithmVersion)
	{
		/* Accept the Algorithm Version 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(0x0066,0x0031, strAlgorithmVersion,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0066,0x0031,DicomConstants.DCM_DT_LO, strAlgorithmVersion);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Algorithm Version.
	 * The software version identifier assigned by a manufacturer to a specific software algorithm.
	 * @return String - value of Algorithm Version.
	 */
	public String getAlgorithmVersion()
	{
		Object objValue = getValue(0x0066,0x0031);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Algorithm Parameters.
	 * @param strAlgorithmParameters- String value of Algorithm Parameters.
	 */
	public void setAlgorithmParameters(String strAlgorithmParameters)
	{
		/* Accept Algorithm Parameters of type LT.
		 * 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(0x0066,0x0032, strAlgorithmParameters,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0066,0x0032,DicomConstants.DCM_DT_LT, strAlgorithmParameters);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Algorithm Parameters.
	 * @return String - value of Algorithm Parameters.
	 */
	public String getAlgorithmParameters()
	{
		Object objValue = getValue(0x0066,0x0032);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Algorithm Source.
	 * @param strAlgorithmSource -String value of Algorithm Source.
	 */
	public void setAlgorithmSource(String strAlgorithmSource)
	{
		/* Accept the Algorithm Source 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(0x0024,0x0202, strAlgorithmSource,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0024,0x0202,DicomConstants.DCM_DT_LO, strAlgorithmSource);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Algorithm Source.
	 * @return String - value of Algorithm Source.
	 */
	public String getAlgorithmSource()
	{
		Object objValue = getValue(0x0024,0x0202);
		if(objValue != null)
			return objValue.toString();
		return null;
	}	
}
