/*******************************************************************************
 *  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.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 Ion Control Point Delivery Sequence.
 */

public class IonControlPointDeliverySequenceItem extends Item  
{

	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	IonControlPointDeliverySequenceItem()
	{
		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
	 */
	IonControlPointDeliverySequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	
	/**
	 * Validates all the attributes in IonBeamLimitingDeviceSequenceItem 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{
			if(!contains(0x300C,0x00F0))
				throw new MandatoryMissingException("300C,00F0");
			if(!contains(0x3008,0x0024))
				throw new MandatoryMissingException("3008,0024)");
			if(!contains(0x3008,0x0042))
				throw new MandatoryMissingException("3008,0042)");
			if(!contains(0x3008,0x0025))
				throw new MandatoryMissingException("3008,0025");
			if(!contains(0x3008,0x0044))
				throw new MandatoryMissingException("3008,0044");
			if(!contains(0x300A,0x0114))
				throw new MandatoryMissingException("300A,0114");
			if(!contains(0x0018,0x0060))
				throw new MandatoryMissingException("0018,0060)");
			if(!contains(0x300A,0x03AC))
				throw new MandatoryMissingException("300A,03AC");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		
		return true;
	}
	/**
	 * Sets ReferencedControlPointIndex
	 * @param strReferencedControlPointIndex String value of Referenced Control Point Index.
	 */
	public void setReferencedControlPointIndex (String strReferencedControlPointIndex )
	{
		/*Accept the ReferencedControlPointIndex  in String format of type IS.
		 * 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(0x300C,0x00F0, strReferencedControlPointIndex ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300C,0x00F0,DicomConstants.DCM_DT_IS, strReferencedControlPointIndex );

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves ReferencedControlPointIndex
	 * @return String - value of Referenced Control Point Index.
	 */
	public String getReferencedControlPointIndex()
	{
		Object objValue = getValue(0x300C,0x00F0);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Treatment Control Point Date.
	 * @param strTreatmentControlPointDate String value of Treatment Control Point Date.
	 */
	public void TreatmentControlPointDate(String strTreatmentControlPointDate)
	{
		/* Accept the Treatment Control Point Date of type DA.
		 * 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(0x3008,0x0024,strTreatmentControlPointDate,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0024,DicomConstants.DCM_DT_DA, strTreatmentControlPointDate);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Treatment Control Point Date.
	 * @return String - value of TreatmentControlPointDate.
	 */
	public String getTreatmentControlPointDate()
	{
		Object objValue = getValue(0x3008,0x0024);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Treatment Control Point Time.
	 * Time when the delivery of radiation at this control point began.
	 * @param strTreatmentControlPointTime String .
	 */
	public void setTreatmentControlPointTime(String[] strTreatmentControlPointTime)
	{
		/* Accept Treatment Control Point Time 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(0x3008,0x0025,strTreatmentControlPointTime,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0025,DicomConstants.DCM_DT_TM,strTreatmentControlPointTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time of TreatmentControlPointTime.
	 * @return String - value of Treatment Control Point Time.
	 */
	public String[] getTimeOfLastCalibration()
	{
		Object objValue = getValue(0x3008,0x0025);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets SpecifiedMeterset.
	 * Desired machine setting for current control point in units specified byPrimary Dosimeter Unit (300A,00B3).
	 * @param strSpecifiedMeterset String value of Slice Thickness.
	 */
	public void setSpecifiedMeterset(String strSpecifiedMeterset)
	{
		/*Accept the SpecifiedMeterset 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(0x3008,0x0042, strSpecifiedMeterset,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0042,DicomConstants.DCM_DT_DS, strSpecifiedMeterset);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves SpecifiedMeterset.
	 * @return String - value of Specified Meterset.
	 */
	public String getSliceThickness()
	{
		Object objValue = getValue(0x3008,0x0042);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets DeliveredMeterset.
	 * Machine setting actually delivered at current control point in unitsspecified by Primary Dosimeter Unit (300A,00B3).
	 * @param strDeliveredMeterset String value of Delivered Meterset.
	 */
	public void setDeliveredMeterset(String strDeliveredMeterset)
	{
		/*Accept the DeliveredMeterset 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(0x3008,0x0044, strDeliveredMeterset,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0044,DicomConstants.DCM_DT_DS, strDeliveredMeterset);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Delivered Meterset.
	 * @return String - value of Delivered Meterset.
	 */
	public String getDeliveredMeterset()
	{
		Object objValue = getValue(0x3008,0x0044);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the MetersetRateSet.
	 * The specified speed of delivery of the specified dose in units specifiedby Primary Dosimeter Unit (300A,00B3) per minute.
	 * @param fMetersetRateSet Float value of the MetersetRateSet.
	 */
	public void setMetersetRateSet( float fMetersetRateSet)
	{
		/*
		 * Accept the MetersetRateSet 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(0x3008,0x0045, fMetersetRateSet,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0045,DicomConstants.DCM_DT_FL, fMetersetRateSet);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the MetersetRateSet.
	 * @return float - Float value of the MetersetRateSet
	 */
	public float getMetersetRateSet()
	{
		Object objValue = getValue(0x3008,0x0045);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the MetersetRateDelivered.
	 * @param fMetersetRateDelivered Float value of the MetersetRateDelivered.
	 */
	public void setMetersetRateDelivered( float fMetersetRateDelivered)
	{
		/*
		 * Accept the MetersetRateDelivered 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(0x3008,0x0046, fMetersetRateDelivered,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0046,DicomConstants.DCM_DT_FL, fMetersetRateDelivered);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the MetersetRateDelivered.
	 * @return float - Float value of the MetersetRateDelivered
	 */
	public float getMetersetRateDelivered()
	{
		Object objValue = getValue(0x3008,0x0046);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets NominalBeamEnergy.
	 * @param strNominalBeamEnergy String value of NominalBeamEnergy.
	 */
	public void setNominalBeamEnergy(String strNominalBeamEnergy)
	{
		/*Accept the NominalBeamEnergy 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(0x300A,0x0114, strNominalBeamEnergy,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0114,DicomConstants.DCM_DT_DS, strNominalBeamEnergy);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves NominalBeamEnergy.
	 * @return String - value of Nominal Beam Energy.
	 */
	public String getNominalBeamEnergy()
	{
		Object objValue = getValue(0x300A,0x0114);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets KVP.
	 * Peak kilo voltage output of the setup 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;
	}
	
	/**
	 * Retrieves pre-initialized object of IonWedgePositionSequence.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 IonWedgePositionSequence - instance of IonWedgePositionSequence. 
	 */
	public IonWedgePositionSequence getIonWedgePositionSequence()
	{
		IonWedgePositionSequence objIonWedgePositionSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x300A,0x03AC);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objIonWedgePositionSequence = (IonWedgePositionSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objIonWedgePositionSequence = (IonWedgePositionSequence)objDicomV3Factory.createSequence(0x300A,0x03AC, objITransferSyntax, objIDictionaryReader);

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


}
