/*******************************************************************************
 *  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.dicommodel.modules;



import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSliceProgressionDirection;

import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 \* This module specifies Attributes that describe Nuclear Medicine reconstructed volumes.
 * Reconstructed volumes are created by applying a transformation (reconstruction) process to the
 * acquired TOMO frames. This module is present only when the Image Type (0008,0008), Value 3,
 * is equal to RECON TOMO or RECON GATED TOMO.
 * 
 * 
 */
public class NMReconstructionModule extends Module  
{


	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	/**
	 * Instantiates a new nM reconstruction module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public NMReconstructionModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0018,0x0050));
		objSingleCollection.add(new Tag(0x0018,0x0088));
		objSingleCollection.add(new Tag(0x0018,0x1100));
		objSingleCollection.add(new Tag(0x0018,0x1210));
		objSingleCollection.add(new Tag(0x0020,0x1041));
		objSingleCollection.add(new Tag(0x0054,0x0500));
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return boolean - returns validation status as success or failure
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */	
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			if(!(isPresent(0x0018,0x0088)))
			throw new MandatoryMissingException("Mandatory missing element 0018,0088");
		if(!(isPresent(0x0018,0x0050)))
			throw new MandatoryMissingException("Mandatory missing element 0018,0050");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),mme);

		}
		return true;
	}

	/**
	 * Spacing between slices, in mm, measured from center-to-center of each slice along the normal to the first image.
	 * The sign of the Spacing Between Slices (0018,0088) determines the direction of stacking. The normal is determined by the
	 * cross product of the direction cosines of the first row and first column of the first frame, such that a positive spacing
	 * indicates slices are stacked behind the first slice and a negative spacing indicates slices are stacked in front of the
	 * first slice. See Image Orientation (0020,0037) in the NM Detector module.
	 * 
	 * @param strSpacingBetweenSlices - String value of Spacing Between Slices.
	 */
	public void setSpacingBetweenSlices(String strSpacingBetweenSlices)
	{
		/*Accept Spacing Between Slices 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 Hash Map
		 */
		addDataElement(0x0018, 0x0088, DicomConstants.DCM_DT_DS, strSpacingBetweenSlices);
	}

	/**
	 * Diameter, in mm, of the region from within which the data was used in creating the
	 * reconstruction of the image. Data may exist outside this region and portions of
	 * the patient may exist outside this region.
	 * 
	 * @param strReconstructionDiameter - String value of Reconstruction Diameter.
	 */
	public void setReconstructionDiameter(String strReconstructionDiameter)
	{
		/*Accept Reconstruction Diameter of Images 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 Hash Map
		 */
		addDataElement(0x0018, 0x1100, DicomConstants.DCM_DT_IS, strReconstructionDiameter);
	}

	/**
	 * Sets A label describing the convolution kernel or algorithm used to reconstruct the data.
	 * 
	 * @param arrStrConvolutionKernel - String array of Convolution Kernel.
	 */
	public void setConvolutionKernel(String[] arrStrConvolutionKernel)
	{
		/*Accept Convolution Kernel 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 DataSet
		 */
		addDataElement(0x0018, 0x1210, DicomConstants.DCM_DT_SH, arrStrConvolutionKernel);
	}

	/**
	 * Sets Nominal slice thickness, in mm.
	 * 
	 * @param strSliceThickness - String value of Slice Thickness.
	 */
	public void setSliceThickness(String strSliceThickness)
	{
		/*Accept Nominal slice thickness, in mm 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 Hash Map
		 */
		addDataElement(0x0018, 0x0050, DicomConstants.DCM_DT_DS, strSliceThickness);
	}

	/**
	 * Sets Relative position of exposure expressed in mm.
	 * 
	 * @param strSliceLocation - String value of Slice Location.
	 */
	public void setSliceLocation(String strSliceLocation)
	{
		/*Accept Relative position of exposure expressed in mm.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 Hash Map
		 */
		addDataElement(0x0020,0x1041, DicomConstants.DCM_DT_DS, strSliceLocation);
	}

	/**
	 * Describes the anatomical direction that slices are progressing as the slices are
	 * considered in order (as defined by the Slice Vector (0054,0080)). Meaningful
	 * only for cardiac images. When View Code Sequence (0054,0220) indicates a short axis view, then
	 * Enumerated Values are: APEX_TO_BASE, BASE_TO_APEX
	 * 
	 * @param enumSliceProgressionDirection - Enum values of Slice Progression Direction.
	 */
	public void setSliceProgressionDirection(EnumSliceProgressionDirection enumSliceProgressionDirection)
	{
		/*Accept Slice Progression Direction 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 DataSet
		 */
		addDataElement(0x0054, 0x0500, DicomConstants.DCM_DT_CS, enumSliceProgressionDirection.getValue());
	}

	/**
	 * Retrieves Spacing between slices, in mm.
	 * 
	 * @return String - String value of Spacing Between Slices.
	 */
	public String getSpacingBetweenSlices()
	{Object objectValue = getValue(0x0018,0x0088);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Retrieves Diameter, in mm, of the region from within which the data was used in creating the reconstruction of the image.
	 * 
	 * @return String - String value of Reconstruction Diameter.
	 */
	public String getReconstructionDiameter()
	{Object objectValue = getValue(0x0018,0x1100);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Retrieves A label describing the convolution kernel or algorithm used to reconstruct the data.
	 * 
	 * @return String[] - String array of convolution kernel.
	 */
	public String[] getConvolutionKernel()
	{Object objectValue = getValue(0x0018,0x1210);
		if(objectValue!=null)
			return (String[])objectValue;
		return null;
	}

	/**
	 * Retrieves Nominal slice thickness, in mm.
	 * 
	 * @return String - String array of Slice Thickness.
	 */
	public String getSliceThickness()
	{Object objectValue = getValue(0x0018,0x0050);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Retrieves Relative position of exposure expressed in mm.
	 * 
	 * @return String - String array of Slice Location.
	 */
	public String getSliceLocation()
	{Object objectValue = getValue(0x0020,0x1041);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Retrieves  the anatomical direction that slices are progressing as the slices are considered in order (as defined by the Slice Vector (0054,0080)).
	 * 
	 * @return String - String value of Slice Progression Direction.
	 */
	public String getSliceProgressionDirection()
	{Object objectValue = getValue(0x0054,0x0500);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/* 
	 * I have modified the collection type from vector to custom class
	 * This method is used by the Module Iterator to iterate through the DataSet and return only
	 * those elements that belong to the respective Module class whose getVectorOfKeys is called.
	 */
	/**
	 * This method returns the vector of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return vector of Tags - returns the vector containing the collection of tags
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
