/*******************************************************************************
 *  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.enums.EnumWedgePosition;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
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 Wedge PositionSequence.
 */

public class IonWedgePositionSequenceItem  extends Item  
{

	private static final long serialVersionUID = 1L;

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

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


	/**
	 * Sets Referenced Wedge Number .Uniquely identifies wedge specified by Wedge Number (300A,00D2) within Beam referenced by Referenced Beam Number(300C,0006).
	 * @param strReferencedWedgeNumber String value of Referenced Wedge Number.
	 */
	public void setReferencedWedgeNumber(String strReferencedWedgeNumber)
	{
		/*Accept the ReferencedWedgeNumber 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,0x00C0, strReferencedWedgeNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300C,0x00C0,DicomConstants.DCM_DT_IS, strReferencedWedgeNumber);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Referenced Wedge Number.
	 * @return String - value of Referenced Wedge Number.
	 */
	public String getReferencedWedgeNumber()
	{
		Object objValue = getValue(0x300C,0x00C0);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Position of Wedge at current control point
	 * @param enumWedgePosition Enum value of Wedge Position.
	 */
	public void setWedgePosition(EnumWedgePosition enumWedgePosition)
	{
		/*Accept the WedgePosition in String format of type CS.
		 * 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,0x0118, enumWedgePosition.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0118,DicomConstants.DCM_DT_CS, enumWedgePosition.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Referenced Wedge Number.
	 * @return String - value of Wedge Position.
	 */
	public String getWedgePosition()
	{
		Object objValue = getValue(0x300A,0x0118);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in WedgePositionSequenceItem  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,0x00D5))
				throw new MandatoryMissingException("value of conditional Element 300C,00D5 required ") ;
			if(!contains(0x300A,0x0118))
				throw new MandatoryMissingException("value of conditional Element 300A,01D8 required ") ;
			if(!contains(0x300C,0x00D0))
				throw new MandatoryMissingException("value of conditional Element 300C,00C0 required ") ;
			if(!contains(0x300A,0x0118))
				throw new MandatoryMissingException("value of conditional Element 300A,0118 required ") ;
			if(!contains(0x300A,0x00DB))
				throw new ConditionFailedException("value of conditional Element 300A,00DB required ") ;
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets the SourcetoWedgeTrayDistance.
	 * @param fSourcetoWedgeTrayDistance Float value of the SourcetoWedgeTrayDistance.
	 */
	public void setSourcetoWedgeTrayDistance( float fSourcetoWedgeTrayDistance)
	{
		/*
		 * Accept the SourcetoWedgeTrayDistance 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,0x00DB, fSourcetoWedgeTrayDistance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00DB,DicomConstants.DCM_DT_FL, fSourcetoWedgeTrayDistance);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Source to Wedge Tray Distance.
	 * @return float - SourcetoWedgeTrayDistance
	 */
	public float getSourcetoWedgeTrayDistance()
	{
		Object objValue = getValue(0x300A,0x00DB);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}



