/*******************************************************************************
 *  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.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
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;

import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
/**
 * This class contains Macro that specifies the Attributes that identify the context for a Data Element Tag that is used as a Selector Attribute (0072,0026)in
 *  Image Set Selector Sequence (0072,0022), Filter Operations Sequence (0072,0400), or Sorting Operations Sequence (0072,0600). 
 */
public class HangingProtocolSelectorAttributeContextMacro extends Macro   
{
	


	private static final long serialVersionUID = 1L;

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

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

	/**
	 * Validates all the attributes in HangingProtocolSelectorAttributeContextMacro
	 * @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 HangingProtocolSelectorAttributeContextMacro 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
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( HangingProtocolSelectorAttributeContextMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(HangingProtocolSelectorAttributeContextMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in HangingProtocolSelectorAttributeContextMacro 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
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(HangingProtocolSelectorAttributeContextMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(HangingProtocolSelectorAttributeContextMacro.class.getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Sets a list of Selector Sequence Pointer.
	 * @param objSingleCollection collection of Selector Sequence Pointer.
	 */
	public void setSelectorSequencePointer(ISingleCollection<ITag> objSingleCollection)
	{
		/*Accept the Selector Sequence Pointer in String format of type AT.
		 * 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(0x0072, 0x0052, objSingleCollection,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072, 0x0052,DicomConstants.DCM_DT_AT, objSingleCollection);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves a list of Selector Sequence Pointer.
	 * @return ISingleCollection<ITag> - collection of Selector Sequence Pointer.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<ITag> getSelectorSequencePointer()
	{
		Object objValue = getValue(0x0072, 0x0052);
		if(objValue != null)
			return (ISingleCollection<ITag>)objValue;
		return null;
	}
	
	/**
	 * Sets the Functional Group Pointer.
	 * @param objITag Tag object of Functional Group Pointer.
	 */
	public void setFunctionalGroupPointer(ITag objITag)
	{
		/*Accept the Functional Group Pointer in String format of type AT.
		 * 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(0x0020,0x9167, objITag,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020,0x9167,DicomConstants.DCM_DT_AT, objITag);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  Functional Group Pointer.
	 * @return String - Tag object of Functional Group Pointer.
	 */
	public ITag getFunctionalGroupPointer()
	{
		Object objValue = getValue(0x0020,0x9167);
		if(objValue != null)
			return (ITag)objValue;
		return null;
	}
	
	/**
	 * Sets Selector Sequence Pointer Private Creator.
	 * @param strArrSelectorSequencePointerPrivateCreator String value of Selector Sequence Pointer Private Creator.
	 */
	public void setSelectorSequencePointerPrivateCreator(String[] strArrSelectorSequencePointerPrivateCreator)
	{
		/* Accept the Selector Sequence Pointer Private Creator 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(0x0072,0x0054, strArrSelectorSequencePointerPrivateCreator, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0054,DicomConstants.DCM_DT_LO, strArrSelectorSequencePointerPrivateCreator);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Selector Sequence Pointer Private Creator.
	 * @return String - value of Selector Sequence Pointer Private Creator.
	 */
	public String[] getSelectorSequencePointerPrivateCreator()
	{
		Object objValue = getValue(0x0072,0x0054);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets Functional Group Private Creator.
	 * @param strFunctionalGroupPrivateCreator String value of Functional Group Private Creator.
	 */
	public void setFunctionalGroupPrivateCreator(String strFunctionalGroupPrivateCreator)
	{
		/* Accept the Functional Group Private Creator 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(0x0020,0x9238, strFunctionalGroupPrivateCreator, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0020,0x9238,DicomConstants.DCM_DT_LO, strFunctionalGroupPrivateCreator);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Functional Group Private Creator.
	 * @return String - value of Functional Group Private Creator.
	 */
	public String getFunctionalGroupPrivateCreator()
	{
		Object objValue = getValue(0x0020,0x9238);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Selector Attribute Private Creator.
	 * @param strSelectorAttributePrivateCreator String value of Selector Attribute Private Creator.
	 */
	public void setSelectorAttributePrivateCreator(String strSelectorAttributePrivateCreator)
	{
		/* Accept the Selector Attribute Private Creator 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(0x0072,0x0056, strSelectorAttributePrivateCreator, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0056,DicomConstants.DCM_DT_LO, strSelectorAttributePrivateCreator);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Selector Attribute Private Creator.
	 * @return String - value of Selector Attribute Private Creator.
	 */
	public String getSelectorAttributePrivateCreator()
	{
		Object objValue = getValue(0x0072,0x0056);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
}
