/*******************************************************************************
 *  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 the methods to set, get and validate the attribute present in Ion Tolerance Table Sequence.
 */

public class IonToleranceTableSequenceItem extends Item  
{
	

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

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

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

	/**
	 * This constructor creates WedgeSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	IonToleranceTableSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * Validates all the attributes in IonToleranceTableSequenceItem  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(0x300A,0x0042)))
				throw new MandatoryMissingException("300A,0042 is required ");
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets Tolerance Table Number.Uniquely identifies Tolerance Table Number(300A,0042) in Tolerance Table Sequence(300A,0040) in RT Tolerance Tables Module of RT Plan IOD.
	 * @param strToleranceTableNumber String value of Tolerance Table Number.
	 */
	public void setToleranceTableNumber(String strToleranceTableNumber)
	{
		/*Accept the Value Type 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,0x0042, strToleranceTableNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0042,DicomConstants.DCM_DT_IS, strToleranceTableNumber);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Tolerance Table Number.
	 * @return String - value of Tolerance Table Number.
	 */
	public String getToleranceTableNumber()
	{
		Object objValue = getValue(0x300A,0x0042);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Tolerance Table Label.Uniquely identifies Tolerance Table Label(300A,0043) in Tolerance Table Sequence(300A,0040) in RT Tolerance Tables Module of RT Plan IOD.
	 * @param strToleranceTableLabel String value of Tolerance Table Label.
	 */
	public void setToleranceTableLabel(String strToleranceTableLabel)
	{
		/*Accept the Value Type in String format of type SH.
		 * 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,0x0043, strToleranceTableLabel,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0043,DicomConstants.DCM_DT_SH, strToleranceTableLabel);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Tolerance Table Label.
	 * @return String - value of Tolerance Table Label.
	 */
	public String getToleranceTableLabel()
	{
		Object objValue = getValue(0x300A,0x0043);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Gantry Angle Tolerance.Uniquely identifies Gantry Angle Tolerance(300A,0044) in Tolerance Table Sequence(300A,0040) in RT Tolerance Tables Module of RT Plan IOD.
	 * @param strGantryAngleTolerance String value of Gantry Angle Tolerance.
	 */
	public void setGantryAngleTolerance(String strGantryAngleTolerance)
	{
		/*Accept the Value Type 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,0x0044, strGantryAngleTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0044,DicomConstants.DCM_DT_DS, strGantryAngleTolerance);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Gantry Angle Tolerance.
	 * @return String - value of Gantry Angle Tolerance.
	 */
	public String getGantryAngleTolerance()
	{
		Object objValue = getValue(0x300A,0x0044);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Gantry Pitch Angle Tolerance.
	 * @param fGantryPitchAngleTolerance float  value of Gantry Pitch Angle Tolerance.
	 */
	public void setGantryPitchAngleTolerance(float fGantryPitchAngleTolerance)
	{
		/*Accept the Gantry Pitch Angle Tolerance 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,0x014E, fGantryPitchAngleTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x014E,DicomConstants.DCM_DT_FL, fGantryPitchAngleTolerance);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Gantry Pitch Angle Tolerance.
	 * @return float  - value of GantryPitchAngleTolerance.
	 */
	public float  getGantryPitchAngleTolerance()
	{
		Object objValue = getValue(0x300A,0x014E);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Beam Limiting Device Angle Tolerance.Uniquely identifies Beam Limiting Device Angle Tolerance(300A,0046) in Tolerance Table Sequence(300A,0040) in RT Tolerance Tables Module of RT Plan IOD.
	 * @param strBeamLimitingDeviceAngleTolerance String value of Beam Limiting Device Angle Tolerance.
	 */
	public void setBeamLimitingDeviceAngleTolerance(String strBeamLimitingDeviceAngleTolerance)
	{
		/*Accept the Value Type 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,0x0046, strBeamLimitingDeviceAngleTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0046,DicomConstants.DCM_DT_DS, strBeamLimitingDeviceAngleTolerance);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Beam Limiting Device Angle Tolerance.
	 * @return String - value of Beam Limiting Device Angle Tolerance.
	 */
	public String getBeamLimitingDeviceAngleTolerance()
	{
		Object objValue = getValue(0x300A,0x0046);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Retrieves pre-initialized object of Beam Limiting Device Tolerance 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 BeamLimitingDeviceToleranceSequence - instance of Beam Limiting Device Tolerance Sequence. 
	 */
	public BeamLimitingDeviceToleranceSequence getBeamLimitingDeviceToleranceSequence()
	{
		BeamLimitingDeviceToleranceSequence objBeamLimitingDeviceToleranceSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3002, 0x0050);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objBeamLimitingDeviceToleranceSequence = (BeamLimitingDeviceToleranceSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objBeamLimitingDeviceToleranceSequence = (BeamLimitingDeviceToleranceSequence)objDicomV3Factory.createSequence(0x3002, 0x0050, objITransferSyntax, objIDictionaryReader);
	
			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x3002, 0x0050, objBeamLimitingDeviceToleranceSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x3002, 0x0050, DicomConstants.DCM_DT_SQ, objBeamLimitingDeviceToleranceSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objBeamLimitingDeviceToleranceSequence;
	}

	/**
	 * Sets Patient Support Angle Tolerance.Uniquely identifies Patient Support Angle Tolerance(300A,004C) in Tolerance Table Sequence(300A,0040) in RT Tolerance Tables Module of RT Plan IOD.
	 * @param strPatientSupportAngleTolerance String value of Patient Support Angle Tolerance.
	 */
	public void setPatientSupportAngleTolerance(String strPatientSupportAngleTolerance)
	{
		/*Accept the Value Type 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,0x004C, strPatientSupportAngleTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x004C,DicomConstants.DCM_DT_DS, strPatientSupportAngleTolerance);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Patient Support Angle Tolerance.
	 * @return String - value of Patient Support Angle Tolerance.
	 */
	public String getPatientSupportAngleTolerance()
	{
		Object objValue = getValue(0x300A,0x004C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Table Top Pitch Angle Tolerance. 
	 * @param strTableTopPitchAngleTolerance String value of Table Top Pitch Angle Tolerance.
	 */
	public void setTableTopPitchAngleTolerance(String strTableTopPitchAngleTolerance)
	{
		/*Accept the Table Top Pitch Angle Tolerance 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(0x300A,0x004F, strTableTopPitchAngleTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x004F,DicomConstants.DCM_DT_FL, strTableTopPitchAngleTolerance);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Table Top Pitch Angle Tolerance.
	 * @return String - value of Table Top Pitch Angle Tolerance.
	 */
	public String getTableTopPitchAngleTolerance()
	{
		Object objValue = getValue(0x300A,0x004F);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Table Top Roll Angle Tolerance.
	 * @param strTableTopRollAngleTolerance String value of Table Top Roll Angle Tolerance.
	 */
	public void setTableTopRollAngleTolerance(String strTableTopRollAngleTolerance)
	{
		/*Accept the Table Top Roll Angle Tolerance 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(0x300A,0x0050, strTableTopRollAngleTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0050,DicomConstants.DCM_DT_FL, strTableTopRollAngleTolerance);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Table Top Roll Angle Tolerance.
	 * @return String - value of Table Top Roll Angle Tolerance.
	 */
	public String getTableTopRollAngleTolerance()
	{
		Object objValue = getValue(0x300A,0x0050);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Table Top Vertical Position Tolerance.Uniquely identifies Table Top Eccentric Angle Tolerance(300A,0051) in Tolerance Table Sequence(300A,0040) in RT Tolerance Tables Module of RT Plan IOD.
	 * @param strTableTopVerticalPositionTolerance String value of Table Top Vertical Position Tolerance.
	 */
	public void setTableTopVerticalPositionTolerance(String strTableTopVerticalPositionTolerance)
	{
		/*Accept the Value Type 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,0x0051, strTableTopVerticalPositionTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0051,DicomConstants.DCM_DT_DS,strTableTopVerticalPositionTolerance);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Table Top Vertical Position Tolerance.
	 * @return String - value of Table Top Vertical Position Tolerance.
	 */
	public String getTableTopVerticalPositionTolerance()
	{
		Object objValue = getValue(0x300A,0x0051);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Snout Position Tolerance.
	 * @param fSnoutPositionTolerance Float value of the Snout Position Tolerance
	 */
	public void setSnoutPositionTolerance( float fSnoutPositionTolerance)
	{
		/*
		 * Accept the Snout Position Tolerance 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(0x300A,0x004B, fSnoutPositionTolerance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x004B,DicomConstants.DCM_DT_FL, fSnoutPositionTolerance);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the SnoutPositionTolerance.
	 * @return float - Float value of the SnoutPositionTolerance
	 */
	public float getSnoutPositionTolerance()
	{
		Object objValue = getValue(0x300A,0x004B);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}
