/*******************************************************************************
 *  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.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
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;

/**
* This item class contains the methods to set, get and validate the attribute present in Pixel Value Mapping to Coded Concept Sequence.
 */

public class PixelValueMappingtoCodedConceptSequenceItem extends Item{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	PixelValueMappingtoCodedConceptSequenceItem()
	{
		super();
	}


	/**
	 * This constructor creates the item and internal code sequence macro with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	PixelValueMappingtoCodedConceptSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates PixelValueMappingtoCodedConceptSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	PixelValueMappingtoCodedConceptSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}



	/**
	 * Validates all the attributes in PixelValueMappingtoCodedConceptSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{	
			if(!contains(0x0022,0x1452))
				throw new MandatoryMissingException("0022,1452") ;
			if(!contains(0x0040,0x9098))
				throw new MandatoryMissingException("0040,9098") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Sets the MappedPixelValue
	 * Pixel value to be mapped.
	 * @param sMappedPixelValue short array of LUT Descriptor values. 
	 */
	public void setMappedPixelValue(short[] sMappedPixelValue)
	{
		/* Accept the MappedPixelValue in short Array format of type US or SS.
		 * Pixel value to be mapped.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0028, 0x3002, sMappedPixelValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028, 0x3002,DicomConstants.DCM_DT_US, sMappedPixelValue);
		addDataElement(objIDataElement);
	}
	/**
	 * Returns the format of the Pixel value to be mapped..
	 * @return short[] - short array of Mapped Pixel Value.
	 */
	public short[] getMappedPixelValue()
	{
		Object objValue = getValue(0x0028, 0x3002);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	/**
	 * Retrieves pre-initialized object of Pixel Value Mapping Code Sequence.If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence.
	 * @return PixelValueMappingCodeSequence - instance of PrimaryFluenceModeSequence. 
	 */
	public PixelValueMappingCodeSequence getPixelValueMappingCodeSequence()
	{
		PixelValueMappingCodeSequence objPixelValueMappingCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0x9098);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objPixelValueMappingCodeSequence = (PixelValueMappingCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objPixelValueMappingCodeSequence = (PixelValueMappingCodeSequence)objDicomV3Factory.createSequence(0x0040, 0x9098, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0x9098, objPixelValueMappingCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0x9098, DicomConstants.DCM_DT_SQ, objPixelValueMappingCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objPixelValueMappingCodeSequence;
	}
	
	/**
	 * Sets Pixel Value Mapping Explanation.
	 * Free form text explanation of the Coded Concept.
	 * @param strPixelValueMappingExplanation String value of Pixel Value Mapping Explanation.
	 */
	public void setPixelValueMappingExplanation(String strRescaleType)
	{
		/* Accept the PixelValueMappingExplanation in String format of type LO.
		 * Free form text explanation of the Coded Concept.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0022,0x1454, strRescaleType,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0022,0x1454,DicomConstants.DCM_DT_LO, strRescaleType);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for PixelValueMappingExplanation.
	 * Free form text explanation of the Coded Concept.
	 * @return String - value of Rescale Type.
	 */
	public String getPixelValueMappingExplanation()
	{
		Object objValue = getValue(0x0022,0x1454);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

}
