/*******************************************************************************
 *  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.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 methods to validate, set and get the attributes of Table Position Sequence .
 */
public class TablePositionSequenceItem extends Item
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item with default TransferSyntax 
	 */
	TablePositionSequenceItem()
	{
		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 with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	TablePositionSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/*
	 * (non-Javadoc) This method validates the contents of the Item
	 * @see cdac.medinfo.sdk.dcm30_04.commoninterface.IItem#validate()
	 */
	/**
	 * Validates all the attributes in TablePositionSequenceItem 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,0x0128))
				throw new MandatoryMissingException("300A,0128");
			if(!contains(0x300A,0x0129))
				throw new MandatoryMissingException("300A,0129");
			if(!contains(0x300A,0x012A))
				throw new MandatoryMissingException("300A,012A");
			if(!contains(0x0018,0x9469))
				throw new MandatoryMissingException("0018,9469");
			if(!contains(0x0018,0x9470))
				throw new MandatoryMissingException("0018,9470");
			if(!contains(0x0018,0x9471))
				throw new MandatoryMissingException("0018,9471");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Sets Table Top Vertical Position.
	 * Table Top Vertical position with respect to an arbitrary chosen reference by the equipment in (mm).
	 * @param strTableTopVerticalPosition String value of Table Top Vertical Position.
	 */
	public void setTableTopVerticalPosition(String strTableTopVerticalPosition)
	{
		/*Accept the TableTopVerticalPosition 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,0x0128, strTableTopVerticalPosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0128,DicomConstants.DCM_DT_DS, strTableTopVerticalPosition);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Table Top Vertical Position.
	 * Table Top Vertical position with respect to an arbitrary chosen reference by the equipment in (mm).
	 * @return String - value of Table Top Vertical Position.
	 */
	public String getTableTopVerticalPosition()
	{
		Object objValue = getValue(0x300A,0x0128);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Table Top Longitudinal Position.
	 * Table Top Longitudinal position with respect to an arbitrary chosen reference by the equipment in (mm).
	 * @param strTableTopLongitudinalPosition String value of Table Top Longitudinal Position.
	 */
	public void setTableTopLongitudinalPosition(String strTableTopLongitudinalPosition)
	{
		/*Accept the TableTopLongitudinalPosition 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,0x0129, strTableTopLongitudinalPosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0129,DicomConstants.DCM_DT_DS, strTableTopLongitudinalPosition);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Table Top Longitudinal Position.
	 * Table Top Longitudinal position with respect to an arbitrary chosen reference by the equipment in (mm).
	 * @return String - value of Table Top Longitudinal Position.
	 */
	public String getTableTopLongitudinalPosition()
	{
		Object objValue = getValue(0x300A,0x0129);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Table Top Lateral Position.
	 * Table Top Lateral position with respect to an arbitrary chosen reference by the equipment in (mm).
	 * @param strTableTopLateralPosition String value of Table Top Lateral Position.
	 */
	public void setTableTopLateralPosition(String strTableTopLateralPosition)
	{
		/*Accept the TableTopLateralPosition 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,0x012A, strTableTopLateralPosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x012A,DicomConstants.DCM_DT_DS, strTableTopLateralPosition);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Table Top Lateral Position.
	 * Table Top Lateral position with respect to an arbitrary chosen reference by the equipment in (mm).
	 * @return String - value of Table Top Lateral Position.
	 */
	public String getTableTopLateralPosition()
	{
		Object objValue = getValue(0x300A,0x012A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Table Horizontal Rotation Angle.
	 * @param fTableHorizontalRotationAngle Float value of the Table Horizontal Rotation Angle
	 */
	public void setTableHorizontalRotationAngle( float fTableHorizontalRotationAngle)
	{
		/*
		 * Accept the Table Horizontal Rotation Angle 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(0x0018,0x9469, fTableHorizontalRotationAngle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9469,DicomConstants.DCM_DT_FL, fTableHorizontalRotationAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Table Horizontal Rotation Angle.
	 * @return float - Float value of the Table Horizontal Rotation Angle
	 */
	public float getTableHorizontalRotationAngle()
	{
		Object objValue = getValue(0x0018,0x9469);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Table Head Tilt Angle.
	 * @param fTableHeadTiltAngle Float value of the Table Head Tilt Angle
	 */
	public void setTableHeadTiltAngle( float fTableHeadTiltAngle)
	{
		/*
		 * Accept the Table Head Tilt Angle 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(0x0018,0x9470, fTableHeadTiltAngle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9470,DicomConstants.DCM_DT_FL, fTableHeadTiltAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Table Head Tilt Angle.
	 * @return float - Float value of the Table Head Tilt Angle
	 */
	public float getTableHeadTiltAngle()
	{
		Object objValue = getValue(0x0018,0x9470);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Table Cradle Tilt Angle.
	 * @param fTableCradleTiltAngle Float value of Table Cradle Tilt Angle
	 */
	public void setTableCradleTiltAngle( float fTableCradleTiltAngle)
	{
		/*
		 * Accept the Table Cradle Tilt Angle 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(0x0018,0x9471, fTableCradleTiltAngle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9471,DicomConstants.DCM_DT_FL, fTableCradleTiltAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Table Cradle Tilt Angle.
	 * @return float - Float value of Table Cradle Tilt Angle
	 */
	public float getTableCradleTiltAngle()
	{
		Object objValue = getValue(0x0018,0x9471);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

}
