/*******************************************************************************
 *  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.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPhotometricInterpretation;

import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
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;
/**
\* This module is used for Floating Point Image Pixel Module.
*/
public class FloatingPointImagePixelModule extends Module  
{


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

	/**
	 * Instantiates a Floating PointImage Pixel Module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public FloatingPointImagePixelModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
	}


	static
	{
		objSingleCollection = new SingleCollection<ITag>();

		objSingleCollection.add(new Tag(0x0028,0x0002));
		objSingleCollection.add(new Tag(0x0028,0x0004));
		objSingleCollection.add(new Tag(0x0028,0x0010));
		objSingleCollection.add(new Tag(0x0028,0x0011));
		objSingleCollection.add(new Tag(0x0028,0x0100));
		objSingleCollection.add(new Tag(0x7FE0,0x0008));
		objSingleCollection.add(new Tag(0x0028,0x0034));
		objSingleCollection.add(new Tag(0x0028,0x0122));
		objSingleCollection.add(new Tag(0x0028,0x0124));
	}

	/**
	 * 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 - status indicating success or failure
	 * 
	 * @throws InvalidModuleException - throws exception indicating exaxct cause of failed validation
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			//Check for the Type 1,2 Mandatory Attributes.
			if(!isPresent(0x0028,0x0002))
			{
				throw new MandatoryMissingException("Missing Mandatory DataElement 0028,1401");
			}
			if(!isPresent(0x0028,0x0004))
			{
				throw new MandatoryMissingException("Missing Mandatory DataElement 0028,1404");
			}
			if(!isPresent(0x0028,0x0010))
			{
				throw new MandatoryMissingException("Missing Mandatory DataElement 0028,140C");
			}
			if(!isPresent(0x0028,0x0011))
			{
				throw new MandatoryMissingException("Missing Mandatory DataElement 0028,140B");
			}
			if(!isPresent(0x0028,0x0100))
			{
				throw new ConditionFailedException("Failed Conditional DataElement 0028,2000");
			}
			if(!isPresent(0x7FE0,0x0008))
			{
				throw new MandatoryMissingException("Missing Mandatory DataElement 0028,1401");
			}
			if(!isPresent(0x0028,0x0034))
			{
				throw new ConditionFailedException(" Failed Conditional DataElement 0028,1404");
			}
			if(!isPresent(0x0028,0x0122))
			{
				throw new MandatoryMissingException("Missing Mandatory DataElement 0028,1401");
			}
			if(!isPresent(0x0028,0x0124))
			{
				throw new ConditionFailedException("Failed Conditional DataElement 0028,1404");
			}
			
			
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

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

		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		catch(ValidationException ve)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ve.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),ve);
		}
	
	return true;
}
	/**
	 * Sets the Samples per Pixel.
	 * 
	 * @param sSamplesPerPixel short value of Samples per Pixel.
	 */
	public void setSamplesPerPixel(short sSamplesPerPixel)
	{
		/* Accept SamplesPerPixel 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(0x0028,0x0002, DicomConstants.DCM_DT_US, sSamplesPerPixel);
	}

	/**
	 * Retrieves the value of Samples Per Pixel.
	 * 
	 * @return short - short value of Samples Per Pixel.
	 */
	public short getSamplesPerPixel()
	{
		Object objValue = getValue(0x0028,0x0002);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets PhotometricInterpretation
	 * Specifies the intended interpretation of the pixel data.
	 * Enumerated Values:
	 * 1) MONOCHROME2
	 *  
	 * @param enumPhotometricInterpretation the enum approval status
	 */
	public void setPhotometricInterpretation(EnumPhotometricInterpretation  enumPhotometricInterpretation)
	{
		/* Accept Approval Status 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
		 */
		addDataElement(0x0028,0x0004, DicomConstants.DCM_DT_CS, enumPhotometricInterpretation.getValue());
	}

	/**
	 * Retrieves PhotometricInterpretation
	 * 
	 * @return String - String value of Photometric Interpretation.
	 */ 
	public String getPhotometricInterpretation()
	{
		Object objValue = getValue(0x0028,0x0004);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Rows.
	 * 
	 * @param sRows short value of Rows.
	 */
	public void setRows(short sRows)
	{
		/* Accept Rows 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(0x0028, 0x0010, DicomConstants.DCM_DT_US, sRows);
	}

	/**
	 * Retrieves the Rows.
	 * 
	 * @return short - short value of Rows.
	 */
	public short getRows()
	{
		Object objValue = getValue(0x0028,0x0010);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the Columns.
	 * 
	 * @param sColumns short value of Columns.
	 */
	public void setColumns(short sColumns)
	{
		/* Accept Columns 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(0x0028,0x0011, DicomConstants.DCM_DT_US, sColumns);
	}

	/**
	 * Retrieves Columns.
	 * 
	 * @return short - short value of Columns.
	 */
	public short getColumns()
	{
		Object objValue = getValue(0x0028,0x0011);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Bits Allocated.
	 * 
	 * @param sBitsAllocated short value of Bits Allocated.
	 */
	public void setBitsAllocated(short sColumns)
	{
		/* Accept BitsAllocated 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(0x0028,0x0100, DicomConstants.DCM_DT_US, sColumns);
	}

	/**
	 * Retrieves BitsAllocated.
	 * 
	 * @return short - short value of Bits Allocated.
	 */
	public short getBitsAllocated()
	{
		Object objValue = getValue(0x0028,0x0100);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Float Pixel Data.
	 * 
	 * @param fFloatPixelData the f first order phase correction angle
	 */
	public void setFloatPixelData(float fFloatPixelData)
	{
		/*Accept value of Float Pixel Data Correction Angle in float format of type OF.
		 * 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(0x7FE0,0x0008, DicomConstants.DCM_DT_OF, fFloatPixelData);
	}

	/**
	 * Retrieves Float Pixel Data.
	 * 
	 * @return float -  Float Pixel Data.
	 */
	public float getFloatPixelData()
	{
		IDataElement objIDataElement = objIDataSet.getDataForTag(0x7FE0,0x0008);
		if(objIDataElement == null)
			return DicomConstants.DCM_NO_VALUE;
		IDicomValue objIDicomValue = objIDataElement.getDicomValue();
		if(objIDicomValue == null)
			return DicomConstants.DCM_NO_VALUE;
		if(objIDicomValue.getValue()==null)
			return DicomConstants.DCM_NO_VALUE;
		return Float.parseFloat(objIDicomValue.getValue().toString());
	}
	
	/**
	 * Sets Pixel Aspect Ratio.
	 * 
	 * @param strPixelAspectRatio String value of Memory Allocation.
	 */
	public void setPixelAspectRatio(String strPixelAspectRatio)
	{
		/* Accept Pixel Aspect Ratio in String format of type IS.
		 * 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(0x0028,0x0034, DicomConstants.DCM_DT_IS, strPixelAspectRatio);
	}

	/**
	 * Return Pixel Aspect Ratio.
	 * 
	 * @return String - String value of Pixel Aspect Ratio.
	 */
	public String getPixelAspectRatio()
	{
		Object objValue = getValue(0x0028,0x0034);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Float Pixel Padding Value
	 * @param fFloatPixelPaddingValue Float value of the Float Pixel Padding Value
	 */
	public void setFloatPixelPaddingValue( float fFloatPixelPaddingValue)
	{
		/*
		 * Accept the FloatPixelPaddingValue in String format of type FL.
		 * 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(0x0028,0x0122, DicomConstants.DCM_DT_FL,fFloatPixelPaddingValue);
	}
	/**
	 * Retrieves FloatPixelPaddingValue
	 * @return float - Float value of the FloatPixelPaddingValue
	 */
	public float getFloatPixelPaddingValue()
	{
		Object objValue = getValue(0x0028,0x0122);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Float Pixel Padding Range Limit
	 * @param fFloatPixelPaddingRangeLimit Float value of the Float Pixel Padding Range Limit
	 */
	public void setFloatPixelPaddingRangeLimit( float fFloatPixelPaddingRangeLimit)
	{
		/*
		 * Accept the Float Pixel Padding Range Limit in String format of type FL.
		 * 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(0x0028,0x0124, DicomConstants.DCM_DT_FL,fFloatPixelPaddingRangeLimit);
	}
	/**
	 * Retrieves Float Pixel Padding Range Limit
	 * @return float - Float value of the Float Pixel Padding Range Limit
	 */
	public float getFloatPixelPaddingRangeLimit()
	{
		Object objValue = getValue(0x0028,0x0124);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}
