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

/**
* This item class contains the methods to set, get and validate the attribute present in X-Ray Acquisition Dose Sequence {@link XRayAcquisitionDoseSequenceItem}.
 *
 */

public class XRayAcquisitionDoseSequenceItem extends Item{

	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	XRayAcquisitionDoseSequenceItem()
	{
		super();
	}


	/**
	 * This constructor creates the item and internal code sequence macro with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	XRayAcquisitionDoseSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates XRayAcquisitionDoseSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	XRayAcquisitionDoseSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	/**
	 * Sets the Minimal value of Exposure Time in ms.
	 * Duration of X-Ray exposure in milliseconds.
	 * @param dblExposureTimeinms Double value of coordinates
	 */
	public void setExposureTimeinms(double dblExposureTimeinms)
	{
		/* Accept the Exposure Time in ms in double format of type FD.
		 * Duration of X-Ray exposure in milliseconds.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x9328, dblExposureTimeinms,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9328,DicomConstants.DCM_DT_FD, dblExposureTimeinms);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Minimal value of Exposure Time in ms.
	 * @return double - double value of ExposureTimeinms
	 */
	public double getExposureTimeinms()
	{
		Object objValue = getValue(0x0018,0x9328);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Minimal value of Exposure in mAs.
	 * @param dblExposureinmAs Double value of Exposure in mAs
	 */
	public void setExposureinmAs(double dblExposureinmAs)
	{
		/* Accept the Chemical Shifts Minimum Integration Limit in ppm in double format of type FD.
		 * The exposure expressed in mAs, for example calculated from ExposureTime and X-Ray Tube Current
		 * 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 Minimal value of Exposure in mAs.
	 * @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 RelativeXrayExposure
	 * Indication of the applied dose, in manufacturer specific units.
	 * @param strRelativeXrayExposure String value of Referenced Reference Image Number.
	 */
	public void setRelativeXrayExposure(String strRelativeXrayExposure )
	{
		/* Accept the Relative Xray Exposure.
		 * Indication of the applied dose, in manufacturer specific units.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1405, strRelativeXrayExposure ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1405,DicomConstants.DCM_DT_IS, strRelativeXrayExposure );

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Relative Xray Exposure, Indication of the applied dose, in manufacturer specific units.
	 * @return String - value of RelativeXrayExposure.
	 */
	public String getRelativeXrayExposure()
	{
		Object objValue = getValue(0x0018,0x1405);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Half Value Layer.Uniquely identifies Half Value Layer
	 * The thickness of Aluminum in mm required to reduce the X-Ray Output(0040,0312) by a factor of two.
	 * @param strHalfValueLayer String value of Half Value Layer.
	 */
	public void setHalfValueLayer(String strHalfValueLayer)
	{
		/* Accept the HalfValueLayer in String format of type DS.
		 * The thickness of Aluminum in mm required to reduce the X-Ray Output(0040,0312) by a factor of two.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0314, strHalfValueLayer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x0314,DicomConstants.DCM_DT_DS, strHalfValueLayer);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Half Value Layer
	 * The thickness of Aluminum in mm required to reduce the X-Ray Output(0040,0312) by a factor of two.
	 * @return String - value of Half Value Layer.
	 */
	public String getHalfValueLayer()
	{
		Object objValue = getValue(0x0040,0x0314);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Half Value Layer.Uniquely identifies Half Value Layer
	 * Average organ dose value measured in dGy during the acquisition of thisimage.
	 * @param strHalfValueLayer String value of Half Value Layer.
	 */
	public void setOrganDose(String strOrganDose)
	{
		/* Accept the HalfValueLayer in String format of type DS.
		 * Average organ dose value measured in dGy during the acquisition of thisimage.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0316, strOrganDose,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x0316,DicomConstants.DCM_DT_DS, strOrganDose);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Organ Dose
	 * Average organ dose value measured in dGy during the acquisition of thisimage.
	 * @return String - value of OrganDose.
	 */
	public String getOrganDose()
	{
		Object objValue = getValue(0x0040,0x0316);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Entrance Dose in mGy.
	 * Average entrance dose value measured in mGy at the surface of the patientduring the acquisition of this image.
	 * @param strEntranceDoseinmGy String value of EntranceDoseinmGy.
	 */
	public void setEntranceDoseinmGy(String strEntranceDoseinmGy)
	{
		/* Accept the EntranceDoseinmGy in String format of type DS.
		 * Average entrance dose value measured in mGy at the surface of the patientduring the acquisition of this image.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x8302, strEntranceDoseinmGy,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x8302,DicomConstants.DCM_DT_DS, strEntranceDoseinmGy);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves EntranceDoseinmGy.
	 * Average entrance dose value measured in mGy at the surface of the patientduring the acquisition of this image.
	 * @return String - value of Entrance Dose inmGy.
	 */
	public String getEntranceDoseinmGy()
	{
		Object objValue = getValue(0x0040,0x8302);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Validates all the attributes in PositionerPositionSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			if(!contains(0x0018,0x9328))
				throw new MandatoryMissingException("0018,9328") ;
			if(!contains(0x0018,0x09332))
				throw new MandatoryMissingException("0018,9332") ;
			if(!contains(0x0040,0x0316))
				throw new MandatoryMissingException("0040,0316") ;
			if(!contains(0x0040,0x8302))
				throw new MandatoryMissingException("0040,8302") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
}
