/*******************************************************************************
 *  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.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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 class specifies the Table 10-27. RT Equipment Correlation Macro.
 *
 */
public class RTEquipmentCorrelationMacro extends Macro
{
	

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

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public RTEquipmentCorrelationMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}

	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public RTEquipmentCorrelationMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public RTEquipmentCorrelationMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public RTEquipmentCorrelationMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Validates all the attributes in RTEquipmentCorrelationMacro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}

	/**
	 * Validates all the attributes in RTEquipmentCorrelationMacro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{ 

			//validate condition here

		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( AlgorithmIdentificationMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(AlgorithmIdentificationMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in RTEquipmentCorrelationMacro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{  
			//validate condition here			

		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(AlgorithmIdentificationMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(AlgorithmIdentificationMacro.class.getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets Patient Support Angle.
	 * @param strPatientSupportAngle String value of Patient Support Angle
	 */
	public void setPatientSupportAngle(String strPatientSupportAngle)
	{
		/*Accept the Patient Support Angle 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,0x0122, strPatientSupportAngle,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x300A,0x0122,DicomConstants.DCM_DT_DS, strPatientSupportAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Patient Support Angle
	 * @return String - value ofPatient Support Angle
	 */
	public String getPatientSupportAngle()
	{
		Object objValue = getValue(0x300A,0x0122);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Table Top Pitch Angle
	 * @param fTableTopPitchAngle Float value of Table Top Pitch Angle
	 */
	public void setTableTopPitchAngle( float fTableTopPitchAngle)
	{
		/*
		 * Accept the Table Top Pitch 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(0x300A,0x0140, fTableTopPitchAngle,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x300A,0x0140,DicomConstants.DCM_DT_FL, fTableTopPitchAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Table Top Pitch Angle
	 * @return float - Float value of the Table Top Pitch Angle
	 */
	public float getTableTopPitchAngle()
	{
		Object objValue = getValue(0x300A,0x0140);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}	
	/**
	 * Sets the Table Top Roll Angle
	 * @param fTableTopRollAngle Float value of Table Top Roll Angle
	 */
	public void setTableTopRollAngle( float fTableTopRollAngle)
	{
		/*
		 * Accept the Table Top Roll 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(0x300A,0x0144, fTableTopRollAngle,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x300A,0x0144,DicomConstants.DCM_DT_FL, fTableTopRollAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Table Top Roll Angle
	 * @return float - Float value of the Table Top Roll Angle
	 */
	public float getTableTopRollAngle()
	{
		Object objValue = getValue(0x300A,0x0144);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Table Top Longitudinal Position.
	 * @param strTableTopLongitudinalPosition String value of Table Top Longitudinal Position
	 */
	public void setTableTopLongitudinalPosition(String strTableTopLongitudinalPosition)
	{
		/*Accept the Table Top Longitudinal Position 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,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x300A,0x0129,DicomConstants.DCM_DT_DS, strTableTopLongitudinalPosition);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Table Top Longitudinal Position
	 * @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.
	 * @param strTableTopLateralPosition String value of Table Top Lateral Position
	 */
	public void setTableTopLateralPosition(String strTableTopLateralPosition)
	{
		/*Accept the Table Top Lateral Position 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,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x300A,0x012A,DicomConstants.DCM_DT_DS, strTableTopLateralPosition);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Table Top Lateral Position
	 * @return String - value of Table Top Lateral Position
	 */
	public String getTableTopLateralPosition()
	{
		Object objValue = getValue(0x300A,0x012A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
}
