/*******************************************************************************
 *  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.enums.EnumContrastBolusAgentAdministered;
import cdac.medinfo.sdk.dcm30_04.enums.EnumContrastBolusAgentDetected;
import cdac.medinfo.sdk.dcm30_04.enums.EnumContrastBolusAgentPhase;
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 ContrastBolusUsageSequence
 */
public class ContrastBolusUsageSequenceItem  extends Item
{
	

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

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

	/**
	 * This constructor creates the item ContrastBolusUsageSequenceItem with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax  specifies the DICOM Standard defined TransfeSyntax
	 */
	ContrastBolusUsageSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates ContrastBolusUsageSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	ContrastBolusUsageSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Sets Identifying number, unique within this SOP Instance, of the agent administered.
	 * @param shContrastOrBolusAgentNumber short value of number
	 */
	public void setContrastOrBolusAgentNumber(short shContrastOrBolusAgentNumber)
	{
		/*Accept the ContrastOrBolusAgentNumber in short format 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(0x0018,0x9337, shContrastOrBolusAgentNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9337,DicomConstants.DCM_DT_US, shContrastOrBolusAgentNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Identifying number, unique within this SOP Instance, of the agent administered.
	 * @return short - short value of number.
	 */
	public short getContrastOrBolusAgentNumber()
	{
		Object objValue = getValue(0x0018,0x9337);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets The administration of the selected agent had begun by the time this frame was acquired.
	 * @param enumContrastBolusAgentAdministered Enumerated value of ContrastOrBolusAgentAdministered
	 */
	public void setContrastOrBolusAgentAdministered(EnumContrastBolusAgentAdministered enumContrastBolusAgentAdministered)
	{
		/*Accept the ContrastOrBolusVolume 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,0x9342, enumContrastBolusAgentAdministered,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9342,DicomConstants.DCM_DT_CS, enumContrastBolusAgentAdministered);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves The administration of the selected agent had begun by the time this frame was acquired.
	 * @return String - String value of ContrastOrBolusVolume.
	 */
	public String getContrastOrBolusAgentAdministered()
	{
		Object objValue = getValue(0x0018,0x9342);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets The selected agent was detected in the frame.
	 * @param enumContrastBolusAgentDetected Enumerated value of of ContrastBolusAgentDetected
	 */
	public void setContrastOrBolusAgentDetected(EnumContrastBolusAgentDetected enumContrastBolusAgentDetected)
	{
		/*Accept the ContrastOrBolusIngredientConcentration 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,0x9343, enumContrastBolusAgentDetected,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9343,DicomConstants.DCM_DT_CS, enumContrastBolusAgentDetected);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves The selected agent was detected in the frame.
	 * @return String - String value of The selected agent was detected in the frame..
	 */
	public String getContrastOrBolusAgentDetected()
	{
		Object objValue = getValue(0x0018,0x9343);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Nominal phase of intravenous contrast administration.
	 * @param enumContrastBolusAgentDetected Enumerated value of of ContrastBolusAgentDetected
	 */
	public void setContrastOrBolusAgentPhase(EnumContrastBolusAgentPhase enumContrastBolusAgentPhase)
	{
		/*Accept the ContrastOrBolusIngredientConcentration 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,0x9344, enumContrastBolusAgentPhase,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9344,DicomConstants.DCM_DT_CS, enumContrastBolusAgentPhase);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Nominal phase of intravenous contrast administration.
	 * @return String - String value of The selected agent was detected in the frame..
	 */
	public String getContrastOrBolusAgentPhase()
	{
		Object objValue = getValue(0x0018,0x9344);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in ContrastBolusUsageSequenceItem 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
		{
			super.validateForModule();
			if(!contains(0x0018,0x9337))
				throw new MandatoryMissingException("0018,9337");
			if(!contains(0x0018,0x9342))
				throw new MandatoryMissingException("0018,9342");
			if(!contains(0x0018,0x9343))
				throw new MandatoryMissingException("0018,9343");
			if(!contains(0x0018,0x9344))		// Validation condition specified for this attribute is not cleared 3rd PDF, page No 326
				throw new MandatoryMissingException("0018,9344");
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
