/*******************************************************************************
 *  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.exceptions.InvalidMacroException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
 * This class contains Macro that describes the attributes for describes the Exposure Index for single projection X-ray images, as described by IEC 62494-1
 *  and the report of AAPM Task Group 116.
 */
public class ExposureIndexMacro extends Macro
{
	


	private static final long serialVersionUID = 1L;

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

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

	/**
	 * This constructor constructs a Macro with the specified Item and Dictionary Reader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ExposureIndexMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}
	/**
	 * Retrieves the Dictionary reader set in the Item
	 * @return IDictionaryReader - DictionaryReader set in the Item
	 */
	public IDictionaryReader getDictionaryReader()
	{
		return objIDictionaryReader;
	}
	
	/**
	 * Sets Exposure Index
	 * @param strExposureIndex String value of Exposure Index
	 */
	public void setExposureIndex(String strExposureIndex)
	{
		/*Accept the ExposureIndex in String format of type DS.
		 * 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(0x0018,0x1411, strExposureIndex,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1411,DicomConstants.DCM_DT_DS, strExposureIndex);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Exposure Index.
	 * @return String - value of Exposure Index.
	 */
	public String getExposureIndex()
	{
		Object objValue = getValue(0x0018,0x1411);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Target Exposure Index
	 * @param strTargetExposureIndex String value of Target Exposure Index
	 */
	public void setTargetExposureIndex(String strExposureIndex)
	{
		/*Accept the TargetExposureIndex in String format of type DS.
		 * 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(0x0018,0x1412, strExposureIndex,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1412,DicomConstants.DCM_DT_DS, strExposureIndex);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Exposure Index.
	 * @return String - value of Exposure Index.
	 */
	public String getTargetExposureIndex()
	{
		Object objValue = getValue(0x0018,0x1412);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Deviation Index
	 * @param strDeviationIndex String value of Deviation Index
	 */
	public void setDeviationIndex(String strDeviationIndex)
	{
		/*Accept the DeviationIndex in String format of type DS.
		 * 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(0x0018,0x1413, strDeviationIndex,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1413,DicomConstants.DCM_DT_DS, strDeviationIndex);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Deviation Index
	 * @return String - value of Deviation Index
	 */
	public String getDeviationIndex()
	{
		Object objValue = getValue(0x0018,0x1413);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Validates all the attributes in ExposureIndexMacro
	 * @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 Exposure Index Macro 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 boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
	
		return true;
	}

	/**
	 * Validates all the attributes in Exposure Index Macro 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
	{
		return true;
	}
}
