/*******************************************************************************
 *  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.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;




/***
* This item class contains methods to validate,set and get the attributes in {@link BrachyControlPointSequence}.
 */
public class BrachyControlPointSequenceItem extends Item  
{
	

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

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


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

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


	/**
	 * Validates all the attributes in BrachyControlPointSequenceItem 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(0x300A,0x0112))
				throw new MandatoryMissingException("300A,0112");
			if(!contains(0x300A,0x02D6))
				throw new MandatoryMissingException("300A,02D6");
			if(!contains(0x300A,0x02D2))
				throw new MandatoryMissingException("300A,02D2");
			if(contains(0x300C,0x0055))
				getBrachyReferencedDoseReferenceSequence().validateForModule();
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets Control Point Index.Uniquely identifies Control Point Index(300A,0112) in Brachy Control Point Sequence(300A,02D0) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strControlPointIndex -String value of Control Point Index.
	 */
	public void setControlPointIndex(String strControlPointIndex)
	{
		/*Accept the ControlPointIndex 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(0x300A,0x0112, strControlPointIndex,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0112,DicomConstants.DCM_DT_IS, strControlPointIndex);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Control Point Index.
	 * @return String - value of Control Point Index.
	 */
	public String getControlPointIndex()
	{
		Object objValue = getValue(0x300A,0x0112);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Cumulative Time Weight.Uniquely identifies Cumulative Time Weight(300A,02D6) in Brachy Control Point Sequence(300A,02D0) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strCumulativeTimeWeight -String value of Cumulative Time Weight.
	 */
	public void setCumulativeTimeWeight(String strCumulativeTimeWeight)
	{
		/*Accept the CumulativeTimeWeight 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,0x02D6, strCumulativeTimeWeight,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x02D6,DicomConstants.DCM_DT_DS, strCumulativeTimeWeight);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Cumulative Time Weight.
	 * @return String - value of Cumulative Time Weight.
	 */
	public String getCumulativeTimeWeight()
	{
		Object objValue = getValue(0x300A,0x02D6);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Control Point Relative Position.Uniquely identifies Control Point Relative Position(300A,02D2) in Brachy Control Point Sequence(300A,02D0) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strControlPointRelativePosition -String value of Control Point Relative Position.
	 */
	public void setControlPointRelativePosition(String strControlPointRelativePosition)
	{
		/*Accept the ControlPointRelativePosition 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,0x02D2, strControlPointRelativePosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x02D2,DicomConstants.DCM_DT_DS, strControlPointRelativePosition);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Cumulative Time Weight.
	 * @return String - value of Cumulative Time Weight.
	 */
	public String getControlPointRelativePosition()
	{
		Object objValue = getValue(0x300A,0x02D2);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Control Point 3D Position.Uniquely identifies Control Point 3D Position(300A,02D4) in Brachy Control Point Sequence(300A,02D0) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param arrstrControlPoint3DPosition- String value of Control Point 3D Position.
	 */
	public void setControlPoint3DPosition(String[] arrstrControlPoint3DPosition)
	{
		/*Accept the ControlPoint3DPosition 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,0x02D4, arrstrControlPoint3DPosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x02D4,DicomConstants.DCM_DT_DS, arrstrControlPoint3DPosition);

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves Control Point 3D Position.
	 * @return String - value of Control Point 3D Position.
	 */
	public String[] getControlPoint3DPosition()
	{
		Object objValue = getValue(0x300A,0x02D4);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets Control Point Orientation.Uniquely identifies Control Point Orientation(300A,0412) in Brachy Control Point Sequence(300A,02D0) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param fControlPointOrientation -float value of Control Point Orientation.
	 */
	public void setControlPointOrientation(float  fControlPointOrientation)
	{
		/*Accept the Control Point Orientation in float  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(0x300A,0x0412, fControlPointOrientation,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0412,DicomConstants.DCM_DT_FL, fControlPointOrientation);

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves Control Point Orientation.
	 * @return float  - value of Control Point Orientation.
	 */
	public float  getControlPointOrientation()
	{
		Object objValue = getValue(0x300A,0x0412);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Retrieves pre-initialized object of Brachy Referenced Dose Reference 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 BrachyReferencedDoseReferenceSequence - instance of Brachy Referenced Dose Reference Sequence. 
	 */
	public BrachyReferencedDoseReferenceSequence getBrachyReferencedDoseReferenceSequence()
	{
		BrachyReferencedDoseReferenceSequence objBrachyReferencedDoseReferenceSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x300C,0x0055);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objBrachyReferencedDoseReferenceSequence = (BrachyReferencedDoseReferenceSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objBrachyReferencedDoseReferenceSequence = (BrachyReferencedDoseReferenceSequence)objDicomV3Factory.createSequence(0x300C,0x0055, objITransferSyntax, objIDictionaryReader);

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