/*******************************************************************************
 *  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.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 Diffusion BMatrix Sequence .
 */


public class DiffusionBMatrixSequenceItem extends Item
{

	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax.
	 */
	DiffusionBMatrixSequenceItem()
	{
		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 and internal code sequence macro with default TransferSyntax. 
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax. 
	 */
	DiffusionBMatrixSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates DiffusionBMatrixSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax.
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used. 
	 */
	DiffusionBMatrixSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * Sets the value of b[X,X].
	 * @param dblDiffusionBValueXX Double value of coordinates
	 */
	public void setDiffusionBValueXX(double dblDiffusionBValueXX)
	{
		/*Accept the DiffusionBValueXX in double format of type FD.
		 * 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,0x9602, dblDiffusionBValueXX,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9602,DicomConstants.DCM_DT_FD, dblDiffusionBValueXX);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of b[X,X].
	 * @return double - double value of DiffusionBValueXX
	 */
	public double getDiffusionBValueXX()
	{
		Object objValue = getValue(0x0018,0x9602);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the value of b[X,Y].
	 * @param dblDiffusionBValueXY Double value of coordinates
	 */
	public void setDiffusionBValueXY(double dblDiffusionBValueXY)
	{
		/*Accept the DiffusionBValueXY in double format of type FD.
		 * 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,0x9603, dblDiffusionBValueXY,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9603,DicomConstants.DCM_DT_FD, dblDiffusionBValueXY);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of b[X,Y].
	 * @return double - double value of DiffusionBValueXY
	 */
	public double getDiffusionBValueXY()
	{
		Object objValue = getValue(0x0018,0x9603);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the value of b[X,Z].
	 * @param dblDiffusionBValueXZ Double value of coordinates
	 */
	public void setDiffusionBValueXZ(double dblDiffusionBValueXZ)
	{
		/*Accept the DiffusionBValueXZ in double format of type FD.
		 * 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,0x9604, dblDiffusionBValueXZ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9604,DicomConstants.DCM_DT_FD, dblDiffusionBValueXZ);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of b[X,Z].
	 * @return double - double value of DiffusionBValueXZ
	 */
	public double getDiffusionBValueXZ()
	{
		Object objValue = getValue(0x0018,0x9604);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the value of b[Y,Y].
	 * @param dblDiffusionBValueYY Double value of coordinates
	 */
	public void setDiffusionBValueYY(double dblDiffusionBValueYY)
	{
		/*Accept the DiffusionBValueYY in double format of type FD.
		 * 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,0x9605, dblDiffusionBValueYY,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9605,DicomConstants.DCM_DT_FD, dblDiffusionBValueYY);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of b[Y,Y].
	 * @return double - double value of DiffusionBValueYY
	 */
	public double getDiffusionBValueYY()
	{
		Object objValue = getValue(0x0018,0x9605);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the value of b[Y,Z].
	 * @param dblDiffusionBValueYZ Double value of coordinates
	 */
	public void setDiffusionBValueYZ(double dblDiffusionBValueYZ)
	{
		/*Accept the DiffusionBValueYZ in double format of type FD.
		 * 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,0x9606, dblDiffusionBValueYZ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9606,DicomConstants.DCM_DT_FD, dblDiffusionBValueYZ);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of b[Y,Z].
	 * @return double - double value of DiffusionBValueYZ
	 */
	public double getDiffusionBValueYZ()
	{
		Object objValue = getValue(0x0018,0x9606);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the value of b[Z,Z].
	 * @param dblDiffusionBValueZZ Double value of coordinates
	 */
	public void setDiffusionBValueZZ(double dblDiffusionBValueZZ)
	{
		/*Accept the DiffusionBValueZZ in double format of type FD.
		 * 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,0x9607, dblDiffusionBValueZZ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9607,DicomConstants.DCM_DT_FD, dblDiffusionBValueZZ);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of b[Z,Z].
	 * @return double - double value of DiffusionBValueZZ
	 */
	public double getDiffusionBValueZZ()
	{
		Object objValue = getValue(0x0018,0x9607);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Validates all the attributes in DiffusionBMatrixSequenceItem that are present in the DataSet.
	 * @return boolean - returns validation success as success or Failure
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try{
			if(!contains(0x0018, 0x9602))
				throw new MandatoryMissingException("0018,9602");
			if(!contains(0x0018, 0x9603))
				throw new MandatoryMissingException("0018,9603");
			if(!contains(0x0018, 0x9604))
				throw new MandatoryMissingException("0018,9604");
			if(!contains(0x0018, 0x9605))
				throw new MandatoryMissingException("0018,9605");
			if(!contains(0x0018, 0x9606))
				throw new MandatoryMissingException("0018,96026");
			if(!contains(0x0018, 0x9607))
				throw new MandatoryMissingException("0018,9607");
				
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
