/*******************************************************************************
 *  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.services.print.printsinkimpl;

import cdac.medinfo.sdk.dcm30_04.baselibs.BasicGrayscaleImageSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.OriginalImageSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedImageOverlayBoxSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedPresentationLUTSequence;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumDecimateOrCropBehavior;
import cdac.medinfo.sdk.dcm30_04.enums.EnumMagnificationType;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
 *  This class contains the Attributes required for Query Model. Also contains methods to populate and fill dataset of Query Model.
 */
public class NSetGrayscaleImageBoxAttribute extends Attributes
{
	/**
	 * This is default Constructor for NSetGrayscaleImageBoxAttribute.
	 */
	public NSetGrayscaleImageBoxAttribute() {
		super();
	}
	/**
	 * 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;
	}
	
	/**
	 * 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.
			 */
			ITransferSyntax objITransferSyntax = getDataSet().getTransferSyntax();
			IDictionaryReader objIDictionaryReader = getDataSet().getDictionaryReader();
			
			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 references to an Image Overlay Box SOP Class/Instance pair and a specific
	 * frame number in multi-frame instances.
	 * 
	 * @return ReferencedImageOverlayBoxSequence - instance of  {@link ReferencedImageOverlayBoxSequence}.
	 */
	public ReferencedImageOverlayBoxSequence getReferencedImageOverlayBoxSequence()
	{
		ReferencedImageOverlayBoxSequence objReferencedImageOverlayBoxSequence = (ReferencedImageOverlayBoxSequence)getDicomValue(0x2020, 0x0130);
		if(objReferencedImageOverlayBoxSequence == 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.
			 */
			ITransferSyntax objITransferSyntax = getDataSet().getTransferSyntax();
			IDictionaryReader objIDictionaryReader = getDataSet().getDictionaryReader();
			
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedImageOverlayBoxSequence = (ReferencedImageOverlayBoxSequence)objDicomV3Factory.createSequence(0x2020, 0x0130, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x2020, 0x0130, DicomConstants.DCM_DT_SQ, objReferencedImageOverlayBoxSequence.getValue());
		}
		return objReferencedImageOverlayBoxSequence;
	}
	
	/**
	 * 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: REPLICATE, BILINEAR,CUBIC,NONE.
	 * 
	 * @param enumMagnificationType String value of Magnification Type.
	 */
	public void setMagnificationType(EnumMagnificationType enumMagnificationType)
	{
		/* Accept Magnification 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,0x0060, DicomConstants.DCM_DT_CS, enumMagnificationType.getValue());
	}

	/**
	 * Return interpolation type by which the printer magnifies or decimates the image in order to fit the image in the
	 * image box on film.
	 * 
	 * @return String - String value of Magnification Type.
	 */
	public String getMagnificationType()
	{
		Object objValue = getValue(0x2010,0x0060);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets maximum density of the images on the film, expressed in hundredths of OD. If Max Density is
	 * higher than maximum printer density than Max Density is set to maximum printer density.
	 * 
	 * @param shMaxDensity - short value of Max Density.
	 */
	public void setMaxDensity(short shMaxDensity)
	{
		/* Accept Max Density in String 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 Hash Map
		 */
		addDataElement(0x2010,0x0130, DicomConstants.DCM_DT_US, shMaxDensity);
	}

	/**
	 * Return maximum density of the images on the film, expressed in hundredths of OD.
	 * 
	 * @return short - short value of Max Density.
	 */
	public short getMaxDensity()
	{
		Object objValue = getValue(0x2010,0x0130);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * 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 objValue = getValue(0x2010,0x0080);
		if(objValue != null)
			return objValue.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 objValue = getValue(0x2010,0x0150);
		if(objValue != null)
			return objValue.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 references to a Presentation LUT related SOP Class/Instance pairs.
	 * 
	 * @return - instance of {@link ReferencedPresentationLUTSequence}
	 */
	public ReferencedPresentationLUTSequence getReferencedPresentationLUTSequence()
	{
		ReferencedPresentationLUTSequence objReferencedPresentationLUTSequence = (ReferencedPresentationLUTSequence)getDicomValue(0x2050,0x0500);
		if(objReferencedPresentationLUTSequence == 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.
			 */
			ITransferSyntax objITransferSyntax = getDataSet().getTransferSyntax();
			IDictionaryReader objIDictionaryReader = getDataSet().getDictionaryReader();
			
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedPresentationLUTSequence = (ReferencedPresentationLUTSequence)objDicomV3Factory.createSequence(0x2050,0x0500, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x2050,0x0500, DicomConstants.DCM_DT_SQ, objReferencedPresentationLUTSequence.getValue());
		}
		return objReferencedPresentationLUTSequence;
	}
	
	/**
	 * Retrieves a sequence containing attributes of the original modality images to be printed in this Film Session.
	 * 
	 * @return OriginalImageSequence - instance of  {@link OriginalImageSequence}.
	 */
	public OriginalImageSequence getOriginalImageSequence()
	{
		OriginalImageSequence objOriginalImageSequence = (OriginalImageSequence)getDicomValue(0x2130, 0x00C0);
		if(objOriginalImageSequence == 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.
			 */
			ITransferSyntax objITransferSyntax = getDataSet().getTransferSyntax();
			IDictionaryReader objIDictionaryReader = getDataSet().getDictionaryReader();
			
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objOriginalImageSequence = (OriginalImageSequence)objDicomV3Factory.createSequence(0x2130, 0x00C0, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x2130, 0x00C0, DicomConstants.DCM_DT_SQ, objOriginalImageSequence.getValue());
		}
		return objOriginalImageSequence;
	}
	
	/**
	 * Populates the query Model using filled dataset.
	 */
	public void populate(IDataSet objIDataSet)
	{
		super.populate(objIDataSet);
	}	
	
	/**
	 * Fills the dataset using already populated Query Model.
	 */
	public void fill(IDataSet objIDataSet)
	{
		super.fill(objIDataSet);
	}
}
