/*******************************************************************************
 *  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.EnumBitsAllocated;
import cdac.medinfo.sdk.dcm30_04.enums.EnumBitsStored;
import cdac.medinfo.sdk.dcm30_04.enums.EnumHighBit;
import cdac.medinfo.sdk.dcm30_04.enums.EnumImageType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLossyImageCompression;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPhotometricInterpretation;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPixelRepresentation;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPlanarConfiguration;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSamplesPerPixel;

import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ItemLimitException;
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 is part of VL Endoscopic Image IOD which specifies the Attributes of Single-frame VL Endoscopic Images.
 */
public class VLImageModule extends Module  
{


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

	
	/**
	 * Instantiates a new vL image module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public VLImageModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0008,0x0008));
		objSingleCollection.add(new Tag(0x0008,0x0033));
		objSingleCollection.add(new Tag(0x0008,0x1140));
		objSingleCollection.add(new Tag(0x0008,0x2218));
		objSingleCollection.add(new Tag(0x0028,0x0002));
		objSingleCollection.add(new Tag(0x0028,0x0004));
		objSingleCollection.add(new Tag(0x0028,0x0006));
		objSingleCollection.add(new Tag(0x0028,0x0100));
		objSingleCollection.add(new Tag(0x0028,0x0101));
		objSingleCollection.add(new Tag(0x0028,0x0102));
		objSingleCollection.add(new Tag(0x0028,0x0103));
		objSingleCollection.add(new Tag(0x0028,0x1050));
		objSingleCollection.add(new Tag(0x0028,0x1051));
		objSingleCollection.add(new Tag(0x0028,0x2110));
	}


	/**
	 * 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(0x0008,0x0008))
				throw new MandatoryMissingException("0008,0008");
			if(!isPresent(0x0028,0x0004))
				throw new MandatoryMissingException("0028,0004");
			if(!isPresent(0x0028,0x0100))
				throw new MandatoryMissingException("0028,0100");
			if(!isPresent(0x0028,0x0101))
				throw new MandatoryMissingException("0028,0101");
			if(!isPresent(0x0028,0x0102))
				throw new MandatoryMissingException("0028,0102");
			if(!isPresent(0x0028,0x0103))
				throw new MandatoryMissingException("0028,0103");
			if(!isPresent(0x0028,0x0002))
				throw new MandatoryMissingException("0028,0002");
			if(!isPresent(0x0028,0x2110))
				throw new MandatoryMissingException("0028,2110");

			short shSamplesperPixel = getSamplesPerPixel();
			if((shSamplesperPixel > 1) && (!isPresent(0x0028,0x0006)))
				throw new ConditionFailedException("0028,0006 is required if Samples per Pixel has a value greater than 1");

			String []arrstrImageType = getImageType();
			if(arrstrImageType.length == 3 && arrstrImageType[2] != null)
			{
				if((EnumImageType.Value3.STEREO_L.getValue().equalsIgnoreCase(arrstrImageType[2]) || EnumImageType.Value3.STEREO_R.getValue().equalsIgnoreCase(arrstrImageType[2])) && (!isPresent(0x0008,0x1140)))
					throw new ConditionFailedException("0008,1140 is required if Image Type Value 3 is present and has a value of STEREO_L or STEREO_R");
			}

			if((isPresent(0x0028,0x1050)) && (!isPresent(0x0028,0x1051)))
				throw new ConditionFailedException("0028,1051 is required if 0028,1050 is present");

		
			
			/*
			 * Condition for element ContentTime not clear.
			 * Condition :- Required if the Image is part of a series in which the images are temporally related.
			 */

			/*
			 * Condition for element Anatomic Region Sequence not clear.
			 * Condition :- Required if Number of Frames (0028,0008) is present and Specimen Accession Number (0040,050A) is absent.
			 */
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

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

		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());

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

		}
		catch(ItemLimitException ile)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ile.getMessage());

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

		}
		return true;
	}

	/**
	 * This method is used to set Image identification characteristics.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * Defined Terms for Value 3:
	 * AXIAL identifies a CT Axial Image
	 * LOCALIZER identifies a CT Localizer Image
	 * 
	 * @param arrStrImageType :Image identification characteristics.
	 */	
	public void setImageType(String[] arrStrImageType)
	{
		/*Accept Image identification characteristics in String Array format of type CS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0008, 0x0008, DicomConstants.DCM_DT_CS, arrStrImageType);
	}


	/**
	 * Retrieves the String values of the attribute Image Type.
	 * 
	 * @return String[] - returns the type of image
	 */
	public String[] getImageType()
	{
		Object objValue = getValue(0x0008,0x0008);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * The method is used to set Specifies the intended interpretation of the pixel data.
	 * The function accepts a Enum Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This DE is then added to the Module HashMap
	 * Enumerated Values:
	 * MONOCHROME2
	 * RGB
	 * YBR_FULL_422
	 * YBR_FULL_420
	 * YBR_RCT
	 * YBR_ICT
	 * 
	 * @param enumPhotometricInterpretation Enum value Photometric Interpretation.
	 */
	public void setPhotometricInterpretation(EnumPhotometricInterpretation enumPhotometricInterpretation)
	{
		/*Accept Specifies the intended interpretation of the pixel data 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 Data Set
		 */
		addDataElement(0x0028, 0x0004, DicomConstants.DCM_DT_CS, enumPhotometricInterpretation.getValue());
	}

	/**
	 * Retrieves the intended interpretation of the pixel data.
	 * 
	 * @return String - String value of Photometric Interpretation.
	 */
	public String getPhotometricInterpretation()
	{
		Object objectValue = getValue(0x0028, 0x0004);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Method is used to set Number of bits allocated for each pixel sample
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param enumBitsAllocated Enum value of Bits Allocated
	 */
	public void setBitsAllocated(EnumBitsAllocated enumBitsAllocated)
	{
		/*Accept bits allocated for each pixel in this image 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, enumBitsAllocated.getValue());
	}

	/**
	 * Retrieves the short value of the attribute Bits Allocated.
	 * 
	 * @return short - short value of Bits Allocated
	 */
	public short getBitsAllocated()
	{Object objectValue = getValue(0x0028, 0x0100);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Method is used to set Number of bits stored for each pixel sample
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param enumBitsStored Enum value of Bits Stored
	 */
	public void setBitsStored(EnumBitsStored enumBitsStored)
	{
		/*Accept bits stored for each pixel in this image 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, 0x0101, DicomConstants.DCM_DT_US, enumBitsStored.getValue());
	}

	/**
	 * Retrieves the short value of the attribute Bits Stored.
	 * 
	 * @return short - short value of Bits Stored
	 */
	public short getBitsStored()
	{Object objectValue = getValue(0x0028, 0x0101);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Method is used to set Most significant bit for pixel sample data
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param enumHighBit Enum short value of significant bit for pixel sample data
	 */
	public void setHighBit(EnumHighBit enumHighBit)
	{
		/*Accept Most significant bit for pixel in this image 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, 0x0102, DicomConstants.DCM_DT_US, enumHighBit.getValue());
	}

	/**
	 * Retrieves the short value of the attribute High Bit.
	 * 
	 * @return short - short value of High Bit
	 */
	public short getHighBit()
	{Object objectValue = getValue(0x0028, 0x0102);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Data representation of the pixel samples. Each sample shall have the same pixel representation.
	 * 
	 * @param enumPixelRepresentation Enum short value specifies the pixel representation
	 */
	public void setPixelRepresentation(EnumPixelRepresentation enumPixelRepresentation)
	{
		/*Accept Data representation of pixel in this image 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, 0x0103, DicomConstants.DCM_DT_US, enumPixelRepresentation.getValue());
	}

	/**
	 * Retrieves the pixel representation.
	 * 
	 * @return short - short value of pixel representation
	 */
	public short getPixelRepresentation()
	{
		Object objectValue = getValue(0x0028, 0x0103);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the number of separate planes in the image. One, three, and four image planes are defined.
	 * 
	 * @param enumSamplesPerPixel Enum short value specifies the samples per pixel in the image
	 */
	public void setSamplesPerPixel(EnumSamplesPerPixel enumSamplesPerPixel)
	{
		/*Accept Number of samples (planes) in this image 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, enumSamplesPerPixel.getValue());
	}

	/**
	 * Retrieves the value of separate planes in the image.
	 * 
	 * @return short - short value of samples per pixel
	 */
	public short getSamplesPerPixel()
	{
		Object objectValue = getValue(0x0028, 0x0002);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the value which shows the pixel data are sent color-by-plane or color-by-pixel.
	 * Enumerated Value:
	 * 0001H (frame interleave)
	 * 
	 * @param enumPlanarConfiguration Enum short value specifies planar configuration of the image
	 */
	public void setPlanarConfiguration(EnumPlanarConfiguration enumPlanarConfiguration)
	{
		/*Accept Planar Configuration of pixel in this image 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, 0x0006, DicomConstants.DCM_DT_US, enumPlanarConfiguration.getValue());
	}

	/**
	 * Retrieves the value which shows the pixel data are sent color-by-plane or color-by-pixel.
	 * 
	 * @return short - short value of the planar configuration
	 */
	public short getPlanarConfiguration()
	{
		Object objectValue = getValue(0x0028, 0x0006);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the time when the data creation was started.
	 * 
	 * @param strContentTime String value of the time when the data creation was started.
	 */	
	public void setContentTime(String strContentTime)
	{
		/*Accept The Time at which image pixel data creation started in String format of type DA.
		 * 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(0x0008, 0x0033, DicomConstants.DCM_DT_TM, strContentTime);
	}

	/**
	 * Retrieves the time when the data creation was started.
	 * 
	 * @return String - String value of Content Time.
	 */
	public String getContentTime()
	{Object objectValue = getValue(0x0008,0x0033);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets the value which Specifies whether an Image has undergone lossy compression.
	 * 
	 * @param enumLossyImageCompression Enum value of Lossy Image Compression.
	 */
	public void setLossyImageCompression(EnumLossyImageCompression enumLossyImageCompression)
	{
		/*Accept Specifies whether an Image has undergone lossy compression.
		 * 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(0x0028, 0x2110, DicomConstants.DCM_DT_CS, enumLossyImageCompression.getValue());
	}

	/**
	 * Retrieves value which Specifies whether an Image has undergone lossy compression.
	 * 
	 * @return String - String value of Lossy Image Compression.
	 */
	public String getLossyImageCompression()
	{
		Object objectValue = getValue(0x0028, 0x2110);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	

	/**
	 * 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;
	}
}
