/*******************************************************************************
 *  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;

/**
 * Specifies the Attributes that identify the context for a Data Element Tag that is used as a Selector Attribute (0072,0026).The attribute may be an attribute nested within one or more Sequences, and/or a Private Attribute.
 *
 */
public class SelectorAttributeMacro extends Macro   
{
	

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

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

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

	/**
	 * Validates all the attributes in SelectorAttributeMacro
	 * @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 SelectorAttributeMacro 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(0x0072,0x0026))
				throw new MandatoryMissingException("0072,0026");
			if(!objIDataSet.isPresent(0x0072,0x0028))
				throw new MandatoryMissingException("0072,0028");
			if(!objIDataSet.isPresent(0x0072,0x0052))
				throw new ConditionFailedException("0072,0052");
			if(!objIDataSet.isPresent(0x0072,0x0054))
				throw new ConditionFailedException("0072,0054");
			if(!objIDataSet.isPresent(0x0074,0x1057))
				throw new ConditionFailedException("0074,1057");
			if(!objIDataSet.isPresent(0x0072,0x0056))
				throw new ConditionFailedException("0072,0056");
		}
		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;
	}

	/**
	 * Validates all the attributes in SelectorAttributeMacro 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(0x0072,0x0026))
				throw new MandatoryMissingException("0072,0026");
			if(!objIItem.contains(0x0072,0x0028))
				throw new MandatoryMissingException("0072,0028");
			if(!objIItem.contains(0x0072,0x0052))
				throw new MandatoryMissingException("0072,0052");
			if(!objIItem.contains(0x0072,0x0054))
				throw new MandatoryMissingException("0072,0054");
			if(!objIItem.contains(0x0074,0x1057))
				throw new MandatoryMissingException("0074,1057");
			if(!objIItem.contains(0x0072,0x0056))
				throw new MandatoryMissingException("0072,0056");
		}
		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;
	}
	/**
	 * Sets the SelectorAttribute.
	 * @param objITag Tag object of Selector Attribute.
	 */
	public void setSelectorAttribute(ITag objITag)
	{
		/*Accept the SelectorAttribute 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,0x0026, objITag,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0026,DicomConstants.DCM_DT_AT, objITag);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  SelectorAttribute.
	 * @return String - Tag object of Selector Attribute.
	 */
	public ITag getSelectorAttribute()
	{
		Object objValue = getValue(0x0072,0x0026);
		if(objValue != null)
			return (ITag)objValue;
		return null;
	}
	/**
	 * Sets SelectorValueNumber.
	 * Positive integer identifying which value of a multi-valued attribute identified bySelector Attribute (0072,0026) is to be referenced.
	 * The value 1 identifies the first value. The value zero identifies any value
	 * @param shSelectorValueNumber short value of Selector Value Number
	 */
	public void setSelectorValueNumber(short shSelectorValueNumber)
	{
		/* Accept the SelectorValueNumber of type US.
		 * 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,0x0028,shSelectorValueNumber,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0028,DicomConstants.DCM_DT_US, shSelectorValueNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves SelectorValueNumber
	 * @return short - short value of SelectorValueNumber
	 */
	public short getSelectorValueNumber()
	{
		Object objValue = getValue(0x0072,0x0028);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets a list of Selector Sequence Pointer
	 * @param objSingleCollection collection of Data Elements Signed.
	 */
	public void setDataElementsSigned(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 SelectorSequencePointer
	 * @return ISingleCollection<ITag> - collection of Data Elements Signed.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<ITag> getSelectorSequencePointer()
	{
		Object objValue = getValue(0x0072,0x0052);
		if(objValue != null)
			return (ISingleCollection<ITag>)objValue;
		return null;
	}
	/**
	 * Sets SelectorSequencePointerPrivateCreator.
	 * @param strArrSelectorSequencePointerPrivateCreator String value of SelectorSequencePointerPrivateCreator.
	 */
	public void setSelectorSequencePointerPrivateCreator(String[] strArrSelectorSequencePointerPrivateCreator)
	{
		/* Accept the SelectorSequencePointerPrivateCreator 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 user-defined label identifier for SelectorSequencePointerPrivateCreator.
	 * @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 SelectorSequencePointerItems. 
	 * @param strSelectorSequencePointerItems String value of SelectorSequencePointerItems.
	 */
	public void setSelectorSequencePointerItems (String strSelectorSequencePointerItems )
	{
		/*Accept the Selector Sequence Pointer Items  in String format of type IS.
		 * 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(0x0074,0x1057, strSelectorSequencePointerItems ,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0074,0x1057,DicomConstants.DCM_DT_IS, strSelectorSequencePointerItems );

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves SelectorSequencePointerItems.
	 * @return String - value of SelectorSequencePointerItems.
	 */
	public String getSelectorSequencePointerItems()
	{
		Object objValue = getValue(0x0074,0x1057);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets SelectorAttributePrivateCreator.
	 * @param strSelectorAttributePrivateCreator String value of SelectorAttributePrivateCreator.
	 */
	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 user-defined label identifier for Selector Attribute Private Creator.
	 * @return String - value of Selector Attribute Private Creator.
	 */
	public String getRescaleType()
	{
		Object objValue = getValue(0x0072,0x0056);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
