/*******************************************************************************
 *  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.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
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 item class contains the methods to set, get and validate the attribute present in CT Additional XRay Source Sequence .
 */


public class CTAdditionalXRaySourceSequenceItem extends Item
{

	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	CTAdditionalXRaySourceSequenceItem()
	{
		super();
	}

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax.
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	CTAdditionalXRaySourceSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates BasicColorImageSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	CTAdditionalXRaySourceSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * 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,getDictionaryReader());
		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,getDictionaryReader());
		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 The diameter in mm of the region over which data were collected.
	 * @param strDataCollectionDiameter String value of Data Collection Diameter.
	 */
	public void setDataCollectionDiameter(String strDataCollectionDiameter)
	{
		/*Accept the DataCollectionDiameter 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,0x0090, strDataCollectionDiameter,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x0090,DicomConstants.DCM_DT_DS, strDataCollectionDiameter);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Data Collection Diameter.
	 * @return String - value of DataCollectionDiameter.
	 */
	public String getDataCollectionDiameter()
	{
		Object objValue = getValue(0x0018,0x0090);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the used nominal size of the focal spot in mm.
	 *  @param strFocalSpots String value of Focal Spot(s).
	 */
	public void setFocalSpots(String[] strFocalSpots)
	{
		/*Accept the FocalSpots 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,0x1190, strFocalSpots,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1190,DicomConstants.DCM_DT_DS, strFocalSpots);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Focal Spot(s).
	 * @return String - value of Focal Spot(s).
	 */
	public String[] getFocalSpots()
	{
		Object objValue = getValue(0x0018,0x1190);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets the type of filter(s) inserted into the X-Ray beam..
	 * @param FilterType
	 */
	public void setFilterType(String strFilterType)
	{
		/*Accept the FilterType in String format of type SH.
		 * 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, 0x1160, strFilterType,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1160,DicomConstants.DCM_DT_SH, strFilterType);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of filter(s) inserted into the X-Ray beam..
	 * @return String - value of type of filter(s) inserted into the X-Ray beam..
	 */
	public String getFilterType()
	{
		Object objValue = getValue(0x0018, 0x1160);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the X-Ray absorbing material used in the filter.
	 * @param strFilterMaterial String Value component of a Name/Value pair when the Concept implied by Concept Name
	 */
	public void setFilterMaterial(String strFilterMaterial)
	{
		/*Accept the Filter Material 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, 0x7050, strFilterMaterial,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018, 0x7050,DicomConstants.DCM_DT_CS, strFilterMaterial);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves  the X-Ray absorbing material used in the filter.
	 * @return String Array -  Value component of the X-Ray absorbing material used in the filter.
	 */
	public String[] getFilterMaterial()
	{
		if(getDataForTag(0x0040, 0xA30A)!=null)
		{
			if(getDataForTag(0x0040, 0xA30A).getValue()!=null)
			{
				Object objValue = getDataForTag(0x0018, 0x7050).getValue();
				if(objValue!=null)
				{
					if(objValue instanceof String)
					{
						String strTemp = (String)objValue;
						String[] strArr = new String[1];
						strArr[0] = strTemp;
						return strArr;
					}
					else
						return (String[])objValue;
				}
			}
		}
		return null;
	}
	/**
	 * Sets The exposure expressed in milliampere seconds.
	 * @param dblExposureInmAs Double value of coordinates
	 */
	public void setExposureInmAs(double dblExposureInmAs)
	{
		/*Accept the The exposure expressed in milliampere seconds 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,0x9332, dblExposureInmAs,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9332,DicomConstants.DCM_DT_FD, dblExposureInmAs);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves The exposure expressed in milliampere seconds.
	 * @return double - double value of ExposureInmAs
	 */
	public double getExposureInmAs()
	{
		Object objValue = getValue(0x0018,0x9332);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the weighting factor of the data from this additional source in amultiple energy composition image. 
	 * @param fEnergyWeightingFactor Float value of the Energy Weighting Factor
	 */
	public void setEnergyWeightingFactor( float fEnergyWeightingFactor)
	{
		/*
		 * Accept the EnergyWeightingFactor in String format of type FL.
		 * 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,0x9353, fEnergyWeightingFactor,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9353,DicomConstants.DCM_DT_FL, fEnergyWeightingFactor);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the weighting factor of the data from this additional source in amultiple energy composition image. 
	 * @return float - Float value of Energy Weighting Factor
	 */
	public float getEnergyWeightingFactor()
	{
		Object objValue = getValue(0x0018,0x9353);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Validates all the attributes in CTAdditionalXRaySourceSequence that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			//validation of mandatory elements
			if(!contains(0x0018,0x0060))
				throw new MandatoryMissingException("0018,0060");
			if(!contains(0x0018,0x9330))
				throw new MandatoryMissingException("(0018,9330");
			if(!contains(0x0018,0x0090))
				throw new MandatoryMissingException("0018,0090");
			if(!contains(0x0018,0x1190))
				throw new MandatoryMissingException("0018,1190");
			if(!contains(0x0018,0x1160))
				throw new MandatoryMissingException("0018,1160");
			if(!contains(0x0018,0x7050))
				throw new MandatoryMissingException("0018,7050");
			if(contains(0x0008,0x9215))
				if(!contains(0x0018,0x9353))
					new ConditionFailedException("value for conditional Element 0x0018,0x9353 required if value of Derivation Code Sequence (0008,9215) Item value is Present");
			
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
