/*******************************************************************************
 *  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.BasicColorImageSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.BasicGrayscaleImageSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
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.EnumDecimateOrCropBehavior;
import cdac.medinfo.sdk.dcm30_04.enums.EnumMagnificationType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPolarity;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 \* This module describes attributes in Image Box Pixel Presentation Module.
 */

public class ImageBoxPixelPresentationModule extends Module  
{

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

	/**
	 * Creates Clinical Trial Series Module with default initialization.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public ImageBoxPixelPresentationModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x2010,0x0060));
		objSingleCollection.add(new Tag(0x2010,0x0080));
		objSingleCollection.add(new Tag(0x2010,0x0150));
		objSingleCollection.add(new Tag(0x2020,0x0010));
		objSingleCollection.add(new Tag(0x2020,0x0020));
		objSingleCollection.add(new Tag(0x2020,0x0030));
		objSingleCollection.add(new Tag(0x2020,0x0040));
		objSingleCollection.add(new Tag(0x2020,0x0110));
		objSingleCollection.add(new Tag(0x2020,0x0111));
	}

	/**
	 * 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 true, if validate module
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public boolean validateModule() throws InvalidModuleException
	{
		return true;
	}

	/**
	 * Sets the position of the image on the film, based on Image Display Format (2010,0010).
	 * 
	 * @param sImagePosition - short value of Image Position.
	 */
	public void setImagePosition(short sImagePosition)
	{
		/*Accept Image Position in short format of type US.
		 * 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
		 */
		addDataElement(0x2020, 0x0010, DicomConstants.DCM_DT_US, sImagePosition);
	}

	/**
	 * Retrieves the position of the image on the film, based on Image Display Format (2010,0010).
	 * 
	 * @return short - short value of Image Position.
	 */
	public short getImagePosition()
	{
		Object objectValue = getValue(0x2020, 0x0010);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets whether minimum pixel values (after VOI LUT transformation) are to printed black or white.
	 * Enumerated Values:
	 * 1) NORMAL = pixels shall be printed as specified by the Photometric Interpretation (0028,0004)
	 * 2) REVERSE = pixels shall be printed with the opposite polarity as specified by the Photometric
	 * Interpretation (0028,0004).
	 * If Polarity (2020,0020) is not specified by the SCU, the SCP shall print with NORMAL polarity.
	 * 
	 * @param enumPolarity - Enum value of Polarity.
	 */
	public void setPolarity(EnumPolarity enumPolarity)
	{
		/* Accept Polarity 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 Hash Map
		 */
		addDataElement(0x2020, 0x0020, DicomConstants.DCM_DT_CS, enumPolarity.getValue());
	}

	/**
	 * Retrieves whether minimum pixel values (after VOI LUT transformation) are to printed black or white.
	 * 
	 * @return String - String value of Polarity.
	 */
	public String getPolarity()
	{
		Object objectValue = getValue(0x2020, 0x0020);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets interpolation type by which the printer magnifies or decimates the image in order to fit the image in the
	 * image box on film.
	 * Defined Terms:
	 * 1) REPLICATE
	 * 2) BILINEAR
	 * 3) CUBIC
	 * 4) NONE
	 * 
	 * @param enumMagnificationType - Enum value of Magnification Type.
	 */
	public void setMagnificationType(EnumMagnificationType enumMagnificationType)
	{
		/* Accept Magnification Type 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 Hash Map
		 */
		addDataElement(0x2010, 0x0060, DicomConstants.DCM_DT_CS, enumMagnificationType.getValue());
	}

	/**
	 * Retrieves Magnification Type.
	 * 
	 * @return String - String value of Magnification Type.
	 */
	public String getMagnificationType()
	{
		Object objectValue = getValue(0x2010, 0x0060);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets value that specifies the type of the interpolation function. Values are defined in Conformance
	 * Statement. Only valid for Magnification Type (2010,0060) = CUBIC.
	 * 
	 * @param strSmoothingType - String value of Smoothing Type.
	 */
	public void setSmoothingType(String strSmoothingType)
	{
		/* Accept Smoothing Type 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 Hash Map
		 */
		addDataElement(0x2010,0x0080, DicomConstants.DCM_DT_CS, strSmoothingType);
	}

	/**
	 * Return value that specifies the type of the interpolation function.
	 * 
	 * @return String - String value of Smoothing Type.
	 */
	public String getSmoothingType()
	{Object objectValue = getValue(0x2010,0x0080);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets character string that contains either the ID of the printer configuration table that contains a set of
	 * values for implementation specific print parameters (e.g. perception LUT related parameters) or one or
	 * more configuration data values, encoded as characters.
	 * 
	 * @param strConfigurationInformation - String value of Configuration Information.
	 */
	public void setConfigurationInformation(String strConfigurationInformation)
	{
		/* Accept Configuration Information in String format of type ST.
		 * 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(0x2010,0x0150, DicomConstants.DCM_DT_ST, strConfigurationInformation);
	}

	/**
	 * Return character string that contains either the ID of the printer configuration table that contains a set of
	 * values for implementation specific print parameters (e.g. perception LUT related parameters) or one or
	 * more configuration data values, encoded as characters.
	 * 
	 * @return String - String value of Configuration Information.
	 */
	public String getConfigurationInformation()
	{Object objectValue = getValue(0x2010,0x0150);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets width (x-dimension) in mm of the image to be printed. This value overrides the size that
	 * corresponds with optimal filling of the Image Box.
	 * 
	 * @param strRequestedImageSize - String value of Requested Image Size.
	 */
	public void setRequestedImageSize(String strRequestedImageSize)
	{
		/* Accept Requested Image Size 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(0x2020,0x0030, DicomConstants.DCM_DT_DS, strRequestedImageSize);
	}

	/**
	 * Return width (x-dimension) in mm of the image to be printed. This value overrides the size that
	 * corresponds with optimal filling of the Image Box.
	 * 
	 * @return String - String value of Requested Image Size.
	 */
	public String getRequestedImageSize()
	{Object objectValue = getValue(0x2020,0x0030);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets whether image pixels are to be decimated or cropped if the image rows or columns is greater
	 * than the available printable pixels in an Image Box.
	 * Enumerated Values:
	 * 1) DECIMATE = a magnification factor <1 to be applied to the image.
	 * 2) CROP     = some image rows and/or columns are to be deleted before printing. The specific algorithm
	 * for cropping shall be described in the SCP Conformance Statement.
	 * 3) FAIL     = the SCP shall not crop or decimate.
	 * 
	 * @param enumDecimateOrCropBehavior - Enum value of Requested Decimate/Crop Behavior.
	 */
	public void setRequestedDecimateOrCropBehavior(EnumDecimateOrCropBehavior enumDecimateOrCropBehavior)
	{
		/* Accept Requested Decimate/Crop Behavior 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(0x2020,0x0040, DicomConstants.DCM_DT_CS, enumDecimateOrCropBehavior.getValue());
	}

	/**
	 * Return whether image pixels are to be decimated or cropped if the image rows or columns is greater
	 * than the available printable pixels in an Image Box.
	 * 
	 * @return String - String value of Requested Decimate/Crop Behavior.
	 */
	public String getRequestedDecimateOrCropBehavior()
	{Object objectValue = getValue(0x2020,0x0040);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Retrieves a sequence which provides the content of the grayscale image pixel data to be printed.
	 * 
	 * @return BasicGrayscaleImageSequence - instance of  {@link BasicGrayscaleImageSequence}.
	 */
	public BasicGrayscaleImageSequence getBasicGrayscaleImageSequence()
	{
		BasicGrayscaleImageSequence objBasicGrayscaleImageSequence = (BasicGrayscaleImageSequence)getDicomValue(0x2020, 0x0110);
		if(objBasicGrayscaleImageSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objBasicGrayscaleImageSequence = (BasicGrayscaleImageSequence)objDicomV3Factory.createSequence(0x2020, 0x0110, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x2020, 0x0110, DicomConstants.DCM_DT_SQ, objBasicGrayscaleImageSequence.getValue());
		}
		return objBasicGrayscaleImageSequence;
	}

	/**
	 * Retrieves a sequence which provides the content of the color image pixel data to be printed.
	 * 
	 * @return BasicColorImageSequence - instance of  {@link BasicColorImageSequence}.
	 */
	public BasicColorImageSequence getBasicColorImageSequence()
	{
		BasicColorImageSequence objBasicColorImageSequence = (BasicColorImageSequence)getDicomValue(0x2020, 0x0111);
		if(objBasicColorImageSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objBasicColorImageSequence = (BasicColorImageSequence)objDicomV3Factory.createSequence(0x2020, 0x0111, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x2020, 0x0111, DicomConstants.DCM_DT_SQ, objBasicColorImageSequence.getValue());
		}
		return objBasicColorImageSequence;
	}

	/**
	 * This method returns the custom collection containing the list of DataElements belonging to the Image Box Pixel Presentation Module.
	 * specified by DICOM Standard.
	 * 
	 * @return collection of Tags - custom collection containing the list of DataElements belonging to the Image Box Pixel Presentation Module
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
