/*******************************************************************************
 *  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.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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.EnumFeatureList;

/**
 * This class specifies the Table C.23.2-2. Screen Specifications Macro Attributes
 *
 */
public class ScreenSpecificationsMacro extends Macro
{
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public ScreenSpecificationsMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}

	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ScreenSpecificationsMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public ScreenSpecificationsMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ScreenSpecificationsMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Validates all the attributes in ScreenSpecificationsMacro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}

	/**
	 * Validates all the attributes in ScreenSpecificationsMacro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{ 

			//validate condition here
			if(!objIDataSet.isPresent(0x0072,0x0104))
				throw new MandatoryMissingException("0072,0104");
			if(!objIDataSet.isPresent(0x0072,0x0106))
				throw new MandatoryMissingException("0072,0106");
			if(!objIDataSet.isPresent(0x0072,0x0108))
				throw new MandatoryMissingException("0072,0108");
			if(!objIDataSet.isPresent(0x0072,0x010A))
				throw new ConditionFailedException("0072,010A");
			if(!objIDataSet.isPresent(0x0072,0x010A)&&!objIDataSet.isPresent(0x0072,0x010C))
				throw new ConditionFailedException("Required Element 0072,010C if Screen Minimum Grayscale Bit Depth (0072,010A) is notpresent.");

		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException( KeratometricMeasurementsMacro.class.getSimpleName() ,ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(KeratometricMeasurementsMacro.class.getSimpleName() ,me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in ScreenSpecificationsMacro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{  
			if(!objIItem.contains(0x0072,0x0104))
				throw new MandatoryMissingException("0072,0104");
			if(!objIItem.contains(0x0072,0x0106))
				throw new MandatoryMissingException("0072,0106");
			if(!objIItem.contains(0x0072,0x0108))
				throw new MandatoryMissingException("0072,0108");
			if(!objIItem.contains(0x0072,0x010A))
				throw new ConditionFailedException("0072,010A");
			if(!objIItem.contains(0x0072,0x010A)&&!objIItem.contains(0x0072,0x010C))
				throw new ConditionFailedException("Required Element 0072,010C if Screen Minimum Grayscale Bit Depth (0072,010A) is notpresent.");

		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidMacroException(KeratometricMeasurementsMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(KeratometricMeasurementsMacro.class.getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets Positive integer indicating the intended number of rows of theaddressable area of the screen in pixels.
	 * @param shNumberOfVerticalPixels short value of Number of Vertical Pixels
	 */
	public void setNumberOfVerticalPixels(short shNumberOfVerticalPixels)
	{
		/* Accept the WaveformBitsStored 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x0104,shNumberOfVerticalPixels,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0104,DicomConstants.DCM_DT_US, shNumberOfVerticalPixels);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Positive integer indicating the intended number of rows of theaddressable area of the screen in pixels.
	 * @return short - short value of Number of Vertical Pixels
	 */
	public short getNumberOfVerticalPixels()
	{
		Object objValue = getValue(0x0072,0x0104);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Positive integer indicating the intended number of columns of theaddressable area of the screen in pixels.
	 * @param shNumberOfVerticalPixels short value of Number of Horizontal Pixels
	 */
	public void setNumberOfHorizontalPixels(short shNumberOfVerticalPixels)
	{
		/* Accept the WaveformBitsStored 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x0106,shNumberOfVerticalPixels,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0106,DicomConstants.DCM_DT_US, shNumberOfVerticalPixels);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Positive integer indicating the intended number of columns of theaddressable area of the screen in pixels.
	 * @return short - short value of Number of Horizontal Pixels
	 */
	public short getNumberOfHorizontalPixels()
	{
		Object objValue = getValue(0x0072,0x0106);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Exactly four dimensionless floating point values indicating the rectangularcoordinate position of the screen within the overall bounding box thatencompasses all the screens.
	 * @param dblArrDisplayEnvironmentSpatialPosition Double value of Display Environment SpatialPosition
	 */
	public void setDisplayEnvironmentSpatialPosition(double[] dblArrDisplayEnvironmentSpatialPosition)
	{
		/*Accept the DisplayEnvironmentSpatialPosition 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(0x0072,0x0108, dblArrDisplayEnvironmentSpatialPosition,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x0108,DicomConstants.DCM_DT_FD, dblArrDisplayEnvironmentSpatialPosition);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Exactly four dimensionless floating point values indicating the rectangularcoordinate position of the screen within the overall bounding box thatencompasses all the screens.
	 * @return double - double value of Display Environment SpatialPosition
	 */
	public double[] getDisplayEnvironmentSpatialPosition()
	{
		Object objValue = getValue(0x0072,0x0108);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	/**
	 * Sets Positive integer indicating the desired minimum number of grayscalebits per pixel of the screen.
	 * @param shScreenMinimumGrayscaleBitDepth short value of Screen Minimum GrayscaleBit Depth
	 */
	public void setScreenMinimumGrayscaleBitDepth(short shScreenMinimumGrayscaleBitDepth)
	{
		/* Accept the Screen Minimum GrayscaleBit Depth 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x010A,shScreenMinimumGrayscaleBitDepth,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x010A,DicomConstants.DCM_DT_US, shScreenMinimumGrayscaleBitDepth);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Positive integer indicating the desired minimum number of grayscalebits per pixel of the screen.
	 * @return short - short value of Screen Minimum GrayscaleBit Depth
	 */
	public short getScreenMinimumGrayscaleBitDepth()
	{
		Object objValue = getValue(0x0072,0x010A);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Positive integer indicating the desired minimum total number of bits percolor channel used to present a pixel.
	 * @param shScreenMinimumColorBitDepth short value of ScreenMinimumColorBitDepth
	 */
	public void setScreenMinimumColorBitDepth(short shScreenMinimumGrayscaleBitDepth)
	{
		/* Accept the Screen Minimum Color Bit Depth 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x010C,shScreenMinimumGrayscaleBitDepth,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x010C,DicomConstants.DCM_DT_US, shScreenMinimumGrayscaleBitDepth);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Positive integer indicating the desired minimum total number of bits percolor channel used to present a pixel.
	 * @return short - short value of Screen Minimum Color Bit Depth
	 */
	public short getScreenMinimumColorBitDepth()
	{
		Object objValue = getValue(0x0072,0x010C);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Positive integer indicating the desired maximum time in millisecondsrequired by the application to repaint the full screen once
	 * @param shApplicationMaximumRepaintTime short value of ApplicationMaximumRepaintTime
	 */
	public void setApplicationMaximumRepaintTime(short shApplicationMaximumRepaintTime)
	{
		/* Accept the ApplicationMaximumRepaintTime 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x010E,shApplicationMaximumRepaintTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072,0x010E,DicomConstants.DCM_DT_US, shApplicationMaximumRepaintTime);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Positive integer indicating the desired maximum time in millisecondsrequired by the application to repaint the full screen once
	 * @return short - short value of ApplicationMaximumRepaintTime
	 */
	public short getApplicationMaximumRepaintTime()
	{
		Object objValue = getValue(0x0072,0x010E);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}

}
