/*******************************************************************************
 *  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.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.enums.EnumReconstructionAlgorithm;



/**
 * This Item class provides methods to set,get and validate the attributes in {@link CTReconstructionSequence}
 */
public class CTReconstructionSequenceItem extends Item  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal CT Reconstruction Sequence Item with default TransferSyntax 
	 * and default DictionaryReader.
	 */
	CTReconstructionSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item and internal CT Reconstruction Sequence Item with default TransferSyntax 
	 * and default DictionaryReader.
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	CTReconstructionSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}
	/**
	 * This constructor creates Item with specified transferSyntax and specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	CTReconstructionSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in CT Reconstruction Sequence Item 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
		{
			/*
			 *  Since this class is implemented while implementing MACRO, Validations required for modules
			 *  is not cleared that is modules using this item is not known now.
			 *  Validation conditions for attributes of these Item is not cleared.  
			 *  Refer p.d.f Chapter 3
			 *  Page No :- 649
			 */
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce);
		}
		catch( InvalidMacroException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ime.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ime);
		}
		return true;
	}
	
	
	/**
	 * Sets the Reconstruction Algorithm Defined in Terms of TEXT,NUMERIC,CODE,DATE,TIME,PNAME
	 * @param strReconstructionAlgorithm Specifies type of the value encoded in this Item
	 */
	public void setReconstructionAlgorithm(EnumReconstructionAlgorithm enumReconstructionAlgorithm)
	{
		/*Accept the Reconstruction Algorithm 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(0x0018,0x9315, enumReconstructionAlgorithm.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9315,DicomConstants.DCM_DT_CS, enumReconstructionAlgorithm.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return EnumReconstructionAlgorithm - Type of the value encoded in this Item
	 */
	public EnumReconstructionAlgorithm getReconstructionAlgorithm()
	{
		Object objValue = getValue(0x0018,0x9315);
		if(objValue != null)
			return EnumReconstructionAlgorithm.valueOf(objValue.toString());
		return null;
	}
	
	/**
	 * Sets Convolution Kernel.A label describing the convolution kernel
	 * or algorithm used to reconstruct the data
	 * @param strArrayConvolutionKernel
	 */
	public void setConvolutionKernel(String[] strArrayConvolutionKernel)
	{
		/*Accept the Array of strConvolutionKernel in String format of type SH.
		 * 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,0x1210, strArrayConvolutionKernel,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1210,DicomConstants.DCM_DT_SH,strArrayConvolutionKernel);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Source Applicator ID.
	 * @return String value of Source Applicator ID.
	 */
	public String[] getConvolutionKernel()
	{
		Object objValue = getValue(0x0018,0x1210);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets Convolution Kernel Group.
	 * A label describing the group that the Convolution Kernel (0018,1210) belongs
	 * @param strConvolutionKernelGroup .
	 */
	public void setConvolutionKernelGroup(String strConvolutionKernelGroup)
	{
		/*Accept the ConvolutionKernelGroup 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(0x0018,0x9316, strConvolutionKernelGroup,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9316,DicomConstants.DCM_DT_CS,strConvolutionKernelGroup);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Convolution Kernel Group.
	 * @return String value of ConvolutionKernelGroup.
	 */
	public String getConvolutionKernelGroup()
	{
		Object objValue = getValue(0x0018,0x9316);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Reconstruction Diameter.
	 * The diameter in mm of the region from which data were used in creating the reconstruction of the image
	 * @param strReconstructionDiameter .
	 */
	public void setReconstructionDiameter(String strReconstructionDiameter)
	{
		/*Accept the Reconstruction Diameter 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(0x0018,0x1100, strReconstructionDiameter,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1100,DicomConstants.DCM_DT_DS,strReconstructionDiameter);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Reconstruction Diameter.
	 * @return String value of Reconstruction Diameter.
	 */
	public String getReconstructionDiameter()
	{
		Object objValue = getValue(0x0018,0x1100);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	
	/**
	 * Sets The Reconstruction Field of View
	 * @param arrdblReconstructionFieldOfView array of double values
	 */
	public void setReconstructionFieldOfView(double[] arrdblReconstructionFieldOfView)
	{
		/*Accept the Reconstruction Field of View  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,0x9317, arrdblReconstructionFieldOfView,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9317,DicomConstants.DCM_DT_FD, arrdblReconstructionFieldOfView);

		addDataElement(objIDataElement);

	}

	/**
	 * Retrieves The Reconstruction Field of View
	 * @return double[] - double values arrdblReconstructionFieldOfView
	 */
	public double[] getDReconstructionFieldOfView()
	{
		Object objValue = getValue(0x0018,0x9317);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	
	/**
	 * Sets The Reconstruction Pixel Spacing
	 * @param arrdblReconstructionPixelSpacing array of double values
	 */
	public void setReconstructionPixelSpacing(double[] arrdblReconstructionPixelSpacing)
	{
		/*Accept the Reconstruction Pixel Spacing  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,0x9322, arrdblReconstructionPixelSpacing,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9322,DicomConstants.DCM_DT_FD, arrdblReconstructionPixelSpacing);

		addDataElement(objIDataElement);

	}

	/**
	 * Retrieves The Reconstruction Pixel Spacing
	 * @return double[] - double values arrdblReconstructionPixelSpacing
	 */
	public double[] getReconstructionPixelSpacing()
	{
		Object objValue = getValue(0x0018,0x9322);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	
	/**
	 * Sets Reconstruction Angle
	 * @param dblReconstructionAngle .
	 */
	public void setReconstructionAngle(double dblReconstructionAngle)
	{
		/* Accept the Reconstruction Angle 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,0x9319, dblReconstructionAngle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9319,DicomConstants.DCM_DT_FD, dblReconstructionAngle);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Reconstruction Angle
	 * @return double .
	 */
	public double getReconstructionAngle()
	{
		Object objValue = getValue(0x0018,0x9319);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	
	/**
	 * Sets Image Filter.
	 * A label describing the filter applied to the reconstructed image after the original reconstruction has been completed.
	 * @param strImageFilter .
	 */
	public void setImageFilter(String strImageFilter)
	{
		/*Accept the Image Filter in String format of type SH.
		 * 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,0x9320, strImageFilter,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9320,DicomConstants.DCM_DT_SH,strImageFilter);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Image Filter.
	 * @return String value of Image Filter.
	 */
	public String getImageFilter()
	{
		Object objValue = getValue(0x0018,0x9320);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
