/*******************************************************************************
 *  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.EnumApplicatorApertureShape;
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.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 the methods to set, get and validate the attribute present in Applicator Geometry Sequence .
 */

public class ApplicatorGeometrySequenceItem extends Item
{

	private static final long serialVersionUID = 1L;


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

	/**
	 * This constructor creates ApplicatorGeometrySequenceItem with specified TransferSyntax and
	 * specified DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax.
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used .
	 */
	ApplicatorGeometrySequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	
	/**
	 * Validates all the attributes in ApplicatorGeometrySequenceItem according the rules defined by the Applicator Geometry Sequence.
	 * @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,0x0432))
				throw new MandatoryMissingException("300A,0432");
			if(contains(0x300A,0x0432) && ( getDataForTag(0x300A,0x0432).getValue().toString().equals(EnumApplicatorApertureShape.SYMMETRIC_APERTURE_CIRCULAR) ||  
					getDataForTag(0x300A,0x0432).getValue().toString().equals(EnumApplicatorApertureShape.SYMMETRIC_APERTURE_SQUARE) ))
				if(!contains(0x300A,0x0433))
						throw new ConditionFailedException("0x300A,0x0433");
			if(contains(0x300A,0x0432) && getDataForTag(0x300A,0x0432).getValue().toString().equals(EnumApplicatorApertureShape.SYMMETRIC_APERTURE_RECTANGULAR))
			{
				if(!contains(0x300A,0x0434))
					throw new ConditionFailedException("0x300A,0x0434");
				if(!contains(0x300A,0x0435))
					throw new ConditionFailedException("0x300A,0x0435");
			}
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Sets the Value of Applicator Aperture Shape.
	 * 1) SYM_SQUARE = A square-shaped aperture symmetrical to the central axis.
	 * 2) SYM_RECTANGLE = A rectangular-shaped aperture symmetrical to the central axis.
	 * 3) SYM_CIRCULAR = A circular-shaped aperture symmetrical to the central axis.
	 * @param enumApplicatorApertureShape - Specifies value of Applicator Aperture Shape.
	 */
	public void setApplicatorApertureShape(EnumApplicatorApertureShape enumApplicatorApertureShape)
	{
		/*Accept the Applicator Aperture Shape 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, 0x0432, enumApplicatorApertureShape,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x0432,DicomConstants.DCM_DT_CS, enumApplicatorApertureShape);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Applicator Aperture Shape
	 * @return String - Value of Applicator Aperture Shape
	 */
	public String getApplicatorApertureShape()
	{
		Object objValue = getValue(0x300A, 0x0432);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Value of Applicator Opening
	 * @param fApplicatorOpening -Specifies value of Applicator Opening
	 */
	public void setApplicatorOpening(float fApplicatorOpening)
	{
		/*Accept the Applicator Opening 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, 0x0433, fApplicatorOpening,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x0433,DicomConstants.DCM_DT_FL, fApplicatorOpening);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Applicator Opening
	 * @return float - Value of Applicator Opening
	 */
	public float getApplicatorOpening()
	{
		Object objValue = getValue(0x300A, 0x0433);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Value of Applicator Opening X
	 * @param strApplicatorOpeningX- Specifies value of Applicator Opening X
	 */
	public void setApplicatorOpeningX(String strApplicatorOpeningX)
	{
		/*Accept the Applicator Opening X 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, 0x0434, strApplicatorOpeningX,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x0434,DicomConstants.DCM_DT_FL, strApplicatorOpeningX);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Applicator Opening X
	 * @return String - Value of Applicator Opening X
	 */
	public String getApplicatorOpeningX()
	{
		Object objValue = getValue(0x300A, 0x0434);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Value of Applicator Opening Y
	 * @param strApplicatorOpeningY -Specifies value of Applicator Opening Y
	 */
	public void setApplicatorOpeningY(String strApplicatorOpeningY)
	{
		/*Accept the Applicator Opening Y 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, 0x0435, strApplicatorOpeningY,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x0435,DicomConstants.DCM_DT_FL, strApplicatorOpeningY);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Applicator Opening Y
	 * @return String - Value of Applicator Opening Y
	 */
	public String getApplicatorOpeningY()
	{
		Object objValue = getValue(0x300A, 0x0435);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
