/*******************************************************************************
 *  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 the methods to set, get and validate the attribute present in Deformable Registration GridSequence.
 */

public class DeformableRegistrationGridSequenceItem extends Item
{

	private static final long serialVersionUID = 1L;

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

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

	/*
	 * (non-Javadoc) This mathod validates the contents of the Item
	 * @see cdac.medinfo.sdk.dcm30_04.commoninterface.IItem#validate()
	 */
	/**
	 * Validates all the attributes in DeformableRegistrationGridSequenceItem 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(0x0020,0x0037))
				throw new MandatoryMissingException("0020,0037");
			if(!contains(0x0020,0x0032))
				throw new MandatoryMissingException("0020,0032");
			if(!contains(0x0064,0x0007))
				throw new MandatoryMissingException("0064,0007");
			if(!contains(0x0064,0x0008))
				throw new MandatoryMissingException("0064,0008");
			if(!contains(0x0064,0x0009))
				throw new MandatoryMissingException("0064,0009");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Sets Image Orientation Patient.
	 * The direction of cosines of the first row and first column of 
	 * the Vector Grid Data (0064,0009) with respect to the patient.
	 * @param strImageOrientationPatient String value of Image Orientation (Patient).
	 */
	public void setImageOrientationPatient(String strImageOrientationPatient)
	{
		/*Accept the Image Orientation (Patient) 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(0x0020,0x0037, strImageOrientationPatient,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0020,0x0037,DicomConstants.DCM_DT_DS, strImageOrientationPatient);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Image Orientation (Patient).
	 * The direction of cosines of the first row and first column of 
	 * the Vector Grid Data (0064,0009) with respect to the patient.
	 * @return String - value of Image Orientation (Patient).
	 */
	public String getImageOrientationPatient()
	{
		Object objValue = getValue(0x0020,0x0037);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Image Position Patient.
	 * The x, y, and z coordinates of the upper left hand voxel (center of the first voxel transmitted) 
	 * of the grid, in mm in the RegisteredFrame of Reference.
	 * @param strImagePositionPatient String value of Image Position (Patient).
	 */
	public void setImagePositionPatient(String strImagePositionPatient)
	{
		/*Accept the Image Position (Patient) 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(0x0020,0x0032, strImagePositionPatient,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0020,0x0032,DicomConstants.DCM_DT_DS, strImagePositionPatient);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Image Position (Patient).
	 * The x, y, and z coordinates of the upper left hand voxel (center of the first voxel transmitted) 
	 * of the grid, in mm in the RegisteredFrame of Reference.
	 * @return String - value of Image Position (Patient).
	 */
	public String getImagePositionPatient()
	{
		Object objValue = getValue(0x0020,0x0032);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Grid Dimensions.
	 * The dimensions of the grid, in voxels. 
	 * A triple representing the number of voxels along the X, Y, and Z axes.
	 * @param lArrGridDimensions Long value of Grid Dimensions
	 */
	public void setGridDimensions(int[] lArrGridDimensions)
	{
		/* Accept the Grid Dimensions in long format of type UL
		 * 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(0x0064,0x0007, lArrGridDimensions,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0064,0x0007,DicomConstants.DCM_DT_UL, lArrGridDimensions);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Grid Dimensions.
	 * The dimensions of the grid, in voxels. 
	 * A triple representing the number of voxels along the X, Y, and Z axes.
	 * @return long[] - long value of Grid Dimensions.
	 */
	public int[] getGridDimensions()
	{
		Object objValue = getValue(0x0064,0x0007);
		if(objValue != null)
			return (int[])objValue;
		return null;
	}
	
	/**
	 * Sets the Grid Resolution.
	 * The resolution of the grid voxels. 
	 * A triple representing the size of a deformation voxel in along the X, Y, and Z dimension, in mm.
	 * @param dblArrGridResolution Double value of Grid Resolution
	 */
	public void setGridResolution(double[] dblArrGridResolution)
	{
		/*Accept the Grid Resolution 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(0x0064,0x0008, dblArrGridResolution,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0064,0x0008,DicomConstants.DCM_DT_FD, dblArrGridResolution);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Grid Resolution.
	 * The resolution of the grid voxels. 
	 * A triple representing the size of a deformation voxel in along the X, Y, and Z dimension, in mm.
	 * @return double - double value of Grid Resolution
	 */
	public double[] getGridResolution()
	{
		Object objValue = getValue(0x0064,0x0008);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	
	/* Sets Vector Grid Data.
	 * A data stream of vectors.
	 * @param fArrVectorGridData float value of Vector Grid Data
	 */
	public void setVectorGridData(float[] fArrVectorGridData)
	{
		/* Accept the Vector Grid Data of type OF.
		 * 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(0x0064,0x0009,fArrVectorGridData,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0064,0x0009,DicomConstants.DCM_DT_OF, fArrVectorGridData);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Vector Grid Data.
	 * A data stream of vectors.
	 * @return float - float value of Vector Grid Data
	 */
	public float[] getVectorGridData()
	{
		Object objValue = getValue(0x0064,0x0009);
		if(objValue != null)
			return (float[])objValue;
		return null;
	}
}
