/*******************************************************************************
 *  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.IDicomValue;
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.utils.DicomConstants;




/**
 * A Sequence of Repeating Items that describes the intervention drugs used.
 */
public class InterventionDrugInformationSequenceItem extends Item  
{
	

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

	/**
	 * This constructor creates the item and with default TransferSyntax 
	 */
	InterventionDrugInformationSequenceItem()
	{
		super();
	}

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

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

	/**
	 * Retrieves pre-initialized object of Intervention Drug Code Sequence.If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 * @return InterventionDrugCodeSequence - instance of Intervention Drug Code Sequence. 
	 */
	public InterventionDrugCodeSequence getInterventionDrugCodeSequence()
	{
		InterventionDrugCodeSequence objInterventionDrugCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0018,0x0026);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objInterventionDrugCodeSequence = (InterventionDrugCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objInterventionDrugCodeSequence = (InterventionDrugCodeSequence)objDicomV3Factory.createSequence(0x0018,0x0026, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0018,0x0026, objInterventionDrugCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0018,0x0026, DicomConstants.DCM_DT_SQ, objInterventionDrugCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objInterventionDrugCodeSequence;
	}

	/**
	 * Sets Name of intervention drug.
	 * @param strInterventionDrugName String value of  Intervention Drug Name.
	 */
	public void setInterventionDrugName(String strInterventionDrugName)
	{
		/*Accept the InterventionDrugName in String format of type LO.
		 * 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, 0x0034, strInterventionDrugName,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x0034,DicomConstants.DCM_DT_LO, strInterventionDrugName);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Name of intervention drug.
	 * @return String value of  Intervention Drug Name.
	 */
	public String getInterventionDrugName()
	{
		Object objValue = getValue(0x0018, 0x0034);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Time of administration of the intervention drug.
	 * @param strInterventionStartTime String value of Intervention Start Time.
	 */
	public void setInterventionStartTime(String strInterventionStartTime)
	{
		/*Accept the Intervention Start Time in String format of type TM.
		 * 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, 0x0035, strInterventionStartTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x0035,DicomConstants.DCM_DT_TM, strInterventionStartTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time of administration of the intervention drug.
	 * @return String value of Intervention Start Time.
	 */
	public String getInterventionStartTime()
	{
		Object objValue = getValue(0x0018, 0x0035);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Time of completion of administration of the intervention drug.
	 * @param strInterventionStopTime String value of Intervention Stop Time.
	 */
	public void setInterventionStopTime(String strInterventionStopTime)
	{
		/*Accept the Intervention Stop Time in String format of type TM.
		 * 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, 0x0027, strInterventionStopTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x0027,DicomConstants.DCM_DT_TM, strInterventionStopTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time of completion of administration of the intervention drug.
	 * @return String value of Intervention Stop Time.
	 */
	public String getInterventionStopTime()
	{
		Object objValue = getValue(0x0018, 0x0027);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Intervention drug dose, in mg.
	 * @param strInterventionDrugDose String value of Intervention Drug Dose.
	 */
	public void setInterventionDrugDose(String strInterventionDrugDose)
	{
		/*Accept the Intervention Drug Dose 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, 0x0028, strInterventionDrugDose,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x0028,DicomConstants.DCM_DT_DS, strInterventionDrugDose);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Intervention drug dose, in mg.
	 * @return String value of Intervention Drug Dose.
	 */
	public String getInterventionDrugDose()
	{
		Object objValue = getValue(0x0018, 0x0028);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in AcquisitionContextSequenceItem 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
	{
		/*
		 * There is no type defined for the attributes in this Module.
		 * Hence cannot rightly validate them.
		 */
		return true;
	}
}
