/*******************************************************************************
 *  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.enums.EnumCollimatorShape;
import cdac.medinfo.sdk.dcm30_04.enums.EnumModality;
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.EnumFeatureList;


/**
 * This class specifies the X-Ray 3D General Per Projection Acquisition Macro that specifies the attributes that are applicablefor a single projection belonging to this acquisition context.
 *
 */

public class XRay3DGeneralPerProjectionAcquisitionMacro extends Macro
{
	

	/**
	 * 
	 */
	XRayCollimatorDimensionsMacro objXRayCollimatorDimensionsMacro=null;
	private static final long serialVersionUID = 1L;


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

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

	/**
	 * Validates all the attributes in XRay3DGeneralPerProjectionAcquisitionMacro
	 * @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 XRay3DGeneralPerProjectionAcquisitionMacro 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
		{ 
			if(objIDataSet.getDataForTag(0x0008,0x0060).getValue().toString().equals(EnumModality.MG))
				if(!objIDataSet.isPresent(0x0018,0x0060))
					throw new ConditionFailedException ("0018,0060 is Required if Modality (0008,0060) is MG.");
			if(objIDataSet.getDataForTag(0x0008,0x0060).getValue().toString().equals(EnumModality.MG))
				if(!objIDataSet.isPresent(0x0018,0x9220))
					throw new ConditionFailedException ("0018,9220 is Required if Modality (0008,0060) is MG.");
			
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( LensometryMeasurementsMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(LensometryMeasurementsMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in XRay3DGeneralPerProjectionAcquisitionMacro 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
		{  
			if(objIItem.getDataForTag(0x0008,0x0060).getValue().toString().equals(EnumModality.MG))
				if(!objIItem.contains(0x0018,0x0060))
					throw new ConditionFailedException ("0018,0060 is Required if Modality (0008,0060) is MG.");
			if(objIItem.getDataForTag(0x0008,0x0060).getValue().toString().equals(EnumModality.MG))
				if(!objIItem.contains(0x0018,0x9220))
					throw new ConditionFailedException ("0018,9220 is Required if Modality (0008,0060) is MG.");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(LensometryMeasurementsMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(LensometryMeasurementsMacro.class.getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets Peak kilo voltage output of the X-Ray generator used.
	 * @param strKVP String value of KVP.
	 */
	public void setKVP(String strKVP)
	{
		/*Accept the KVP 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,0x0060, strKVP,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x0060,DicomConstants.DCM_DT_DS, strKVP);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves KVP.
	 * @return String - value of KVP.
	 */
	public String getKVP()
	{
		Object objValue = getValue(0x0018,0x0060);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Nominal X-Ray tube current in milliamperes.
	 * @param dblXRayTubeCurrentInmA Double value of coordinates
	 */
	public void setXRayTubeCurrentInmA(double dblXRayTubeCurrentInmA)
	{
		/*Accept the X-Ray tube current in milliamperes in double format of type FD.
		 * 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,0x9330, dblXRayTubeCurrentInmA,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x9330,DicomConstants.DCM_DT_FD, dblXRayTubeCurrentInmA);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Nominal X-Ray tube current in milliamperes..
	 * @return double - double value of XRayTubeCurrentInmA
	 */
	public double getXRayTubeCurrentInmA()
	{
		Object objValue = getValue(0x0018,0x9330);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Frame Acquisition Duration.Uniquely identifies Frame Acquisition Duration(0018,9220) in Frame Content Sequence(0020,9111).
	 * @param flFrameAcquisitionDuration double value of Frame Acquisition Duration.
	 */
	public void setFrameAcquisitionDuration(double flFrameAcquisitionDuration)
	{
		/*Accept the FrameAcquisitionDuration in String format of type FD.
		 * 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,0x9220, flFrameAcquisitionDuration,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x9220,DicomConstants.DCM_DT_FD, flFrameAcquisitionDuration);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Frame Acquisition Duration.
	 * @return String - value of Frame Acquisition Duration.
	 */
	public double getFrameAcquisitionDuration()
	{
		Object objValue = getValue(0x0018,0x9220);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets type Collimator Shape.
	 * Shape(s) of the collimator.
	 * Defined Terms:
	 * 1) RECTANGULAR.
	 * 2) CIRCULAR.
	 * 3) POLYGONAL.
	 * @param enumCollimatorShape enum value of Collimator Shape.
	 */
	public void setCollimatorShape(EnumCollimatorShape enumCollimatorShape)
	{
		/* Accept the Collimator Shape 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(0x0018,0x1700, enumCollimatorShape.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1700,DicomConstants.DCM_DT_CS, enumCollimatorShape.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Return type of Collimator Shape. 
	 * Shape(s) of the collimator.
	 * @return String - value of Collimator Shape.
	 */
	public String getCollimatorShape()
	{
		Object objValue = getValue(0x0018,0x1700);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * This method returns the X-Ray Collimator Dimensions MacroAttributes with proper initialization.
	 * @return - instance of {@link XRayCollimatorDimensionsMacro} 
	 */
	public XRayCollimatorDimensionsMacro getXRayCollimatorDimensionsMacro()
	{
		if(objXRayCollimatorDimensionsMacro == null)
		{
			objXRayCollimatorDimensionsMacro = new XRayCollimatorDimensionsMacro((IDataSet) this);
			return objXRayCollimatorDimensionsMacro;
		}
		return objXRayCollimatorDimensionsMacro;
	}
}
