/*******************************************************************************
 *  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.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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 specifies the Attributes that describe the Lookup table data.
 */

public class SupplementalPaletteColorLookupTableModule extends Module  
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0028,0x1101));
		objSingleCollection.add(new Tag(0x0028,0x1102));
		objSingleCollection.add(new Tag(0x0028,0x1103));
		objSingleCollection.add(new Tag(0x0028,0x1201));
		objSingleCollection.add(new Tag(0x0028,0x1202));
		objSingleCollection.add(new Tag(0x0028,0x1203));
	}

	/**
	 * Instantiates a new supplemental palette color lookup table module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public SupplementalPaletteColorLookupTableModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	/**
	 * 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 - returns validation status as success or failure
	 * 
	 * @throws InvalidModuleException  If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			
			if(!isPresent(0x0028,0x1101))
				throw new MandatoryMissingException("0028,1101");
			if(!isPresent(0x0028,0x1102))
				throw new MandatoryMissingException("0028,1102");
			if(!isPresent(0x0028,0x1103))
				throw new MandatoryMissingException("0028,1103");
			if(!isPresent(0x0028,0x1201))
				throw new MandatoryMissingException("0028,1201");
			if(!isPresent(0x0028,0x1202))
				throw new MandatoryMissingException("0028,1202");
			if(!isPresent(0x0028,0x1203))
				throw new MandatoryMissingException("0028,1203");

		}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);

		}
		return true;
	}

	/**
	 * Sets specified format of the Red Palette Color Lookup Table Data (0028,1201).
	 * 
	 * @param sRedPaletteColorLookupTableDescriptor  short value of Red Palette Color Lookup Table Descriptor
	 */
	public void setRedPaletteColorLookupTableDescriptor(short sRedPaletteColorLookupTableDescriptor)
	{
		/* Accept Red Palette Color Lookup Table Descriptor Value 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, 0x1101, DicomConstants.DCM_DT_US, sRedPaletteColorLookupTableDescriptor);
	}

	/**
	 * Returns the format of the Red Palette Color Lookup Table Data.
	 * 
	 * @return short - short value of Segmented Blue Palette Color Lookup Table Data
	 */
	public short getRedPaletteColorLookupTableDescriptor()
	{Object objectValue = getValue(0x0028,0x1101);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the specified format of the Green Palette Color Lookup Table Data (0028,1202)
	 * Required if Photometric Interpretation (0028,0004) has a value of PALETTE
	 * COLOR or Pixel Presentation (0008,9205) at the image level equals COLOR or
	 * MIXED.
	 * 
	 * @param sGreenPaletteColorLookupTableDescriptor short value of Green Palette Color Lookup Table Descriptor
	 */

	public void setGreenPaletteColorLookupTableDescriptor(short sGreenPaletteColorLookupTableDescriptor)
	{
		/*Accept Green Palette Color Lookup Table Descriptor Value 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, 0x1102, DicomConstants.DCM_DT_US, sGreenPaletteColorLookupTableDescriptor);
	}

	/**
	 * Returns the format of the Green Palette Color Lookup Table Data.
	 * 
	 * @return short - short value of Green Palette Color Lookup Table Data.
	 */
	public short getGreenPaletteColorLookupTableDescriptor()
	{Object objectValue = getValue(0x0028,0x1102);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the specified format of the Blue Palette Color Lookup table Data (0028,1203).
	 * Required if Photometric Interpretation (0028,0004) has a value of PALETTE COLOR
	 * 
	 * @param sBluePaletteColorLookupTableDescriptor short value of Blue Palette Color Lookup Table Descriptor.
	 */
	// This Attribute requries a VR of US or SS.
	//I am assuming US.
	public void setBluePaletteColorLookupTableDescriptor(short sBluePaletteColorLookupTableDescriptor)
	{
		/*Accept Blue Palette Color Lookup Table Descriptor Value 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, 0x1103, DicomConstants.DCM_DT_US, sBluePaletteColorLookupTableDescriptor);
	}

	/**
	 * Returns the format of the Blue Palette Color Lookup Table Data.
	 * 
	 * @return short - Blue Palette Color Lookup Table Data.
	 */
	public short getBluePaletteColorLookupTableDescriptor()
	{Object objectValue = getValue(0x0028,0x1103);
		if(objectValue!=null)
			return Short.parseShort(objectValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Red Palette Color Lookup Table Data. Required if Photometric Interpretation
	 * (0028,0004) has a value of PALETTE COLOR or Pixel Presentation (0008,9205)
	 * at the image level equals COLOR or MIXED.
	 * 
	 * @param objRedPltClrLkupTblData the obj red plt clr lkup tbl data
	 */
	public void setRedPaletteColorLookupTableData(ISingleCollection<byte[]> objRedPltClrLkupTblData)
	{
		/*Accept Red Palette Color Lookup Table Data in this image in short format of type OW.
		 * 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, 0x1201, DicomConstants.DCM_DT_OW, objRedPltClrLkupTblData);
	}

	/**
	 * Returns Red Palette Color Lookup Table Data.
	 * 
	 * @return ISingleCollection<byte[]> - collection of Red Palette Color Lookup Table Data.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getRedPaletteColorLookupTableData()
	{
		Object objValue = objIDataSet.getValue(0x0028,0x1201);
		if(objValue != null)
			return (ISingleCollection<byte[]>)(objValue);
		return null;
	}

	/**
	 * Sets Green Palette Color Lookup Table Data. Required if Photometric Interpretation
	 * (0028,0004) has a value of PALETTE COLOR or Pixel Presentation (0008,9205)
	 * at the image level equals COLOR or MIXED.
	 * 
	 * @param objGreenPltClrLkupTblData the obj green plt clr lkup tbl data
	 */

	public void setGreenPaletteColorLookupTableData(ISingleCollection<byte[]> objGreenPltClrLkupTblData)
	{
		/*Accept Green Palette Color Lookup Table Data in this image in short format of type OW.
		 * 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, 0x1202, DicomConstants.DCM_DT_OW, objGreenPltClrLkupTblData);
	}

	/**
	 * Returns Segmented Green Palette Color Lookup Table Data.
	 * 
	 * @return ISingleCollection<byte[]> - collection of Segmented Green Palette Color Lookup Table Data.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getGreenPaletteColorLookupTableData()
	{
		Object objValue = objIDataSet.getValue(0x0028,0x1202);
		if(objValue != null)
			return (ISingleCollection<byte[]>)(objValue);
		return null;
	}

	/**
	 * Sets Blue Palette Color Lookup Table Data. Required if Photometric Interpretation
	 * (0028,0004) has a value of PALETTE COLOR or Pixel Presentation (0008,9205)
	 * at the image level equals COLOR or MIXED.
	 * 
	 * @param objBluePltClrLkupTblData the obj blue plt clr lkup tbl data
	 */
	public void setBluePaletteColorLookupTableData(ISingleCollection<byte[]> objBluePltClrLkupTblData)
	{
		/*Accept Blue Palette Color Lookup Table Data in this image in short format of type OW.
		 * 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, 0x1203, DicomConstants.DCM_DT_OW, objBluePltClrLkupTblData);
	}

	/**
	 * Returns Segmented Blue Palette Color Lookup Table Data.
	 * 
	 * @return ISingleCollection<byte[]> -  collection of Segmented Blue Palette Color Lookup Table Data.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getBluePaletteColorLookupTableData()
	{
		Object objValue = objIDataSet.getValue(0x0028,0x1203);
		if(objValue != null)
			return (ISingleCollection<byte[]>)(objValue);
		return null;
	}

	/**
	 * This method returns the vector of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return - the customized collection containing the dataelement tags
	 * that beling to this module
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
