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

/**
* This item class contains the methods to set, get and validate the attribute present in Channel Display Sequence .
 */

public class ChannelDisplaySequenceItem extends Item
{

	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	ChannelDisplaySequenceItem()
	{
		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
	 */
	ChannelDisplaySequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Sets one or more value in a Referenced Wave form Channels.
	 * @param arrReferencedWaveformChannels- Short value of Referenced Waveform Channels.
	 */
	public void setReferencedWaveformChannels(short[] arrReferencedWaveformChannels)
	{
		/* Accept the Identifier of the displayed channel, specified as a pair of values (M,C).
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0040, 0xA0B0, arrReferencedWaveformChannels,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA0B0,DicomConstants.DCM_DT_US, arrReferencedWaveformChannels);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves one or more value in a Referenced Wave form Channels.
	 * @return short Array - value of Referenced Waveform Channels.
	 */
	public short[] getReferencedWaveformChannels()
	{
		Object objValue = getValue(0x0040, 0xA0B0);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	
	/**
	 * Sets Channel Off set.
	 * @param strChannelOffset -String value of Channel Off set.
	 */
	public void setChannelOffset(String strChannelOffset)
	{
		/*Accept the strChannelOffset in String format of type DS.
		 * 
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0218, strChannelOffset,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0218,DicomConstants.DCM_DT_DS, strChannelOffset);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Channel Off set.
	 * @return String - value of Channel Off set.
	 */
	public String getChannelOffset()
	{
		Object objValue = getValue(0x003A,0x0218);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Channel Recommended Display CIE Lab Value.
	 * @param arrChannelRecommendedDisplayCIELabValue -Short value of Channel Recommended Display CIE Lab Value
	 */
	public void setChannelRecommendedDisplayCIELabValue(short[] arrChannelRecommendedDisplayCIELabValue)
	{
		/* Accept the Channel Recommended Display CIE Lab Value in Array of short format of type US.
		 * A color triplet value recommended for rendering the channel on a color display. 
		 * The units are specified in PCS-Values, and the value is encoded as CIELab
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x003A, 0x0244, arrChannelRecommendedDisplayCIELabValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A, 0x0244,DicomConstants.DCM_DT_US, arrChannelRecommendedDisplayCIELabValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves one or more Channel Recommended Display CIE Lab Value in a Instance.
	 * @return short Array - frames in a Channel Recommended Display CIE Lab Value.
	 */
	public short[] getChannelRecommendedDisplayCIELabValue()
	{
		Object objValue = getValue(0x003A, 0x0244);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}

	/**
	 * Sets Position of the Channel within the Presentation Group display area
	 * @param shChannelPosition short value of Rotation of right image.
	 */
	public void setChannelPosition(short shChannelPosition)
	{
		/* Accept the ChannelPosition of right image of type FL.
		 * Position of the Channel within the Presentation Group display area
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x003A,0x0245,shChannelPosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0245,DicomConstants.DCM_DT_FL, shChannelPosition);

		addDataElement(objIDataElement);

	}

	/**
	 * Retrieves  Channel Position
	 * Position of the Channel within the Presentation Group display area
	 * @return short - short value of Rotation of right image.
	 */
	public short getChannelPosition()
	{
		Object objValue = getValue(0x003A,0x0245);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Display Shading Flag Defined in Terms of enum.
	 * @param enumDisplayShadingFlag -Specifies type of the value encoded in this Item.
	 */
	public void setDisplayShadingFlag(EnumDisplayShadingFlag enumDisplayShadingFlag)
	{
		/* Accept the DisplayShadingFlag in enum format of type CS.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0246, enumDisplayShadingFlag,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0246,DicomConstants.DCM_DT_CS, enumDisplayShadingFlag);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Display Shading Flag.
	 * @return String - value of Display Shading Flag.
	 */
	public String getDisplayShadingFlag()
	{
		Object objValue = getValue(0x003A,0x0246);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Fraction of the Presentation Group vertical display dimension assigned to the unit quantity (least significant bit) of the Channel samples 
	 * @param arrFractionalChannelDisplayScale -Float value of Fractional Channel Display Scale.
	 */
	public void setFractionalChannelDisplayScale(float[] arrFractionalChannelDisplayScale)
	{
		/* Accept the Fractional Channel Display Scale in float format of type FL.
		 * Fraction of the Presentation Group vertical display dimension assigned to the unit quantity (least significant bit) of the Channel samples 
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0247, arrFractionalChannelDisplayScale,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0247,DicomConstants.DCM_DT_FL, arrFractionalChannelDisplayScale);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Fractional Channel Display Scale.
	 * @return float[] - Float array of  Fractional Channel Display Scale.
	 */
	public float[] getFractionalChannelDisplayScale()
	{
		Object objValue = getValue(0x003A,0x0247);
		if(objValue != null)
			return (float[])objValue;
		return null;
	}
	
	/**
	 * Sets the AbsoluteChannelDisplayScale to source pixels,specified in one dimension.
	 * @param fAbsoluteChannelDisplayScale -Float value of the Ratio of AbsoluteChannelDisplayScale
	 */
	public void setAbsoluteChannelDisplayScale( float fAbsoluteChannelDisplayScale)
	{
		/* Nominal vertical display height in mm assigned to the unit quantity (least significant bit) of the Channel samples 
		 * Accept the AbsoluteChannelDisplayScale in String format of type FL.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x003A,0x0248, fAbsoluteChannelDisplayScale,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0248,DicomConstants.DCM_DT_FL, fAbsoluteChannelDisplayScale);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Ratio of Absolute Channel Display Scale,specified in one dimension.
	 * @return float - Float value of the Ratio of AbsoluteChannelDisplayScale
	 */
	public float getAbsoluteChannelDisplayScale()
	{
		Object objValue = getValue(0x003A,0x0248);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Validates all the attributes in Channel Display Sequence 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(0x0040,0xA0B0))
				throw new MandatoryMissingException("0040,A0B0") ;
			if(!contains(0x003A,0x000D))
				throw new MandatoryMissingException("003A,0244") ;
			if(!contains(0x003A,0x0245))
				throw new MandatoryMissingException("003A,0245") ;
			
			if(contains(0x003A,0x0247) && contains(0x003A,0x0248))
					throw new ConditionFailedException("0x003A,0x0247") ;
			if(contains(0x003A,0x0248) && contains(0x003A,0x0247))
				    throw new ConditionFailedException("0x003A,0x0248") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
}
