/*******************************************************************************
 *  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.enums.EnumWaveformBitsAllocated;
import cdac.medinfo.sdk.dcm30_04.enums.EnumWaveformOriginality;
import cdac.medinfo.sdk.dcm30_04.enums.EnumWaveformSampleInterpretation;
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;

import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;

/**
* This item class contains methods to validate, set and get the attributes of {@link WaveformSequence}.
 */

public class WaveformSequenceItem extends Item  
{
	


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	WaveformSequenceItem()
	{
		super();
	}

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	WaveformSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/*
	 * (non-Javadoc) This mathod validates the contents of the Item
	 * @see cdac.medinfo.sdk.dcm30_04.commoninterface.IItem#validate()
	 */
	/**
	 * Validates all the attributes in WaveformSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try{
			if(!contains(0x003A,0x0004))
				throw new MandatoryMissingException("003A,0004");
			if(!contains(0x003A,0x0005))
				throw new MandatoryMissingException("003A,0005");
			if(!contains(0x003A,0x0010))
				throw new MandatoryMissingException("003A,0010");
			if(!contains(0x003A,0x001A))
				throw new MandatoryMissingException("003A,001A");
			if(!contains(0x003A,0x0200))
				throw new MandatoryMissingException("003A,0200");
			if(!contains(0x5400,0x1004))
				throw new MandatoryMissingException("5400,1004");
			if(!contains(0x5400,0x1006))
				throw new MandatoryMissingException("5400,1006");
			if(!contains(0x5400,0x1010))
				throw new MandatoryMissingException("5400,1010");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException("Waveform Sequence Item",me);
		}
		return true;
	}

	/**
	 * Sets attribute Offset time in milliseconds from a reference time
	 * @param StrMultiplexGroupTimeOffset String time in milliseconds from a reference time
	 */	
	public void setMultiplexGroupTimeOffset(String StrMultiplexGroupTimeOffset)
	{
		/*Accept MultiplexGroupTimeOffset in String 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1068,StrMultiplexGroupTimeOffset,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1068, DicomConstants.DCM_DT_DS,StrMultiplexGroupTimeOffset);

		addDataElement(objIDataElement);
	}

	/**
	 * Return attribute Offset time in milliseconds from a reference time
	 * @return String - identifies the time in milliseconds from a reference time
	 */
	public String getMultiplexGroupTimeOffset()
	{
		Object objValue = getValue(0x0018,0x1068);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets attribute identifies Offset time in milliseconds from a synchronization
	 * trigger to the first sample of a waveform multiplex group. May be positive or negative.
	 * @param StrTriggerTimeOffset String Offset time in milliseconds
	 */	
	public void setTriggerTimeOffset(String StrTriggerTimeOffset)
	{
		/*Accept TriggerTimeOffset in String 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1069,StrTriggerTimeOffset,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1069, DicomConstants.DCM_DT_DS,StrTriggerTimeOffset);

		addDataElement(objIDataElement);
	}

	/**
	 * Return attribute identifies Offset time in milliseconds from a synchronization
	 * trigger to the first sample of a waveform multiplex group. May be positive or negative.
	 * @return String - Offset time in milliseconds
	 */
	public String getTriggerTimeOffset()
	{
		Object objValue = getValue(0x0018,0x1069);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Sample number whose time corresponds to a synchronization trigger
	 * @param shTriggerSamplePosition short value of Sample number
	 */
	public void setTriggerSamplePosition(short shTriggerSamplePosition)
	{
		/* Accept the TriggerSamplePosition 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x106E,shTriggerSamplePosition,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x106E,DicomConstants.DCM_DT_UL, shTriggerSamplePosition);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Sample number whose time corresponds to a synchronization trigger
	 * @return short - short value of Sample number
	 */
	public short getTriggerSamplePosition()
	{
		Object objValue = getValue(0x0018,0x106E);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}


	/**
	 * Sets Attribute Waveforn Originality 
	 * Enumerated values:
	 * ORIGINAL
	 * DERIVED
	 * @param enumWaveformOriginality short value of High Bit.
	 */
	public void setWaveformOriginality(EnumWaveformOriginality enumWaveformOriginality)
	{
		/* Accept WaveformOriginality in String 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0004,enumWaveformOriginality.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x003A,0x0004,DicomConstants.DCM_DT_CS,enumWaveformOriginality.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Attribute Waveforn Originality 
	 * @return String - value of Waveforn Originality 
	 */
	public String getWaveformOriginality()
	{
		Object objValue = getValue(0x003A,0x0004);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of channels for this multiplex group.
	 * @param shNumberofWaveformChannels short value of  Number of channels
	 */
	public void setNumberofWaveformChannels(short shNumberofWaveformChannels)
	{
		/* Accept the NumberofWaveformChannels 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0005,shNumberofWaveformChannels,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0005,DicomConstants.DCM_DT_US, shNumberofWaveformChannels);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of channels for this multiplex group. 
	 * @return short - short value of  Number of channels
	 */
	public short getNumberofWaveformChannels()
	{
		Object objValue = getValue(0x003A,0x0005);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Number of samples per channel in this multiplex group.
	 * @param shNumberofWaveformSamples short value of Number of samples
	 */
	public void setNumberofWaveformSamples(short shNumberofWaveformSamples)
	{
		/* Accept the NumberofWaveformSamples 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0010,shNumberofWaveformSamples,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0010,DicomConstants.DCM_DT_UL, shNumberofWaveformSamples);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of samples per channel in this multiplex group.
	 * @return short - short value of Number of samples
	 */
	public short getNumberofWaveformSamples()
	{
		Object objValue = getValue(0x003A,0x0010);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}


	/**
	 * Sets attribute identifies Sampling Frequency
	 * @param StrSamplingFrequency String identifies the Sampling Frequency
	 */	
	public void setSamplingFrequency(String StrSamplingFrequency)
	{
		/*Accept SamplingFrequency in String 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x001A,StrSamplingFrequency,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x003A,0x001A, DicomConstants.DCM_DT_DS,StrSamplingFrequency);

		addDataElement(objIDataElement);
	}

	/**
	 * Return attribute identifies Sampling Frequency
	 * @return String - identifies the Sampling Frequency
	 */
	public String getSamplingFrequency()
	{
		Object objValue = getValue(0x003A,0x001A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets attribute identifies Label for multiplex group
	 * @param StrMultiplexGroupLabel String Label for multiplex group
	 */	
	public void setMultiplexGroupLabel(String StrMultiplexGroupLabel)
	{
		/*Accept MultiplexGroupLabelin String  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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x003A,0x0020,StrMultiplexGroupLabel,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x003A,0x0020, DicomConstants.DCM_DT_SH,StrMultiplexGroupLabel);

		addDataElement(objIDataElement);
	}

	/**
	 * Return attribute identifies Label for multiplex group
	 * @return String - identifies Label for multiplex group
	 */
	public String getMultiplexGroupLabel()
	{
		Object objValue = getValue(0x003A,0x0020);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of Channel Definition 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 ChannelDefinitionSequence - instance of Channel Definition Sequence
	 */
	public ChannelDefinitionSequence getChannelDefinitionSequence()
	{
		ChannelDefinitionSequence objChannelDefinitionSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x003A,0x0200);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objChannelDefinitionSequence = (ChannelDefinitionSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objChannelDefinitionSequence = (ChannelDefinitionSequence)objDicomV3Factory.createSequence(0x003A,0x0200, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x003A,0x0200, objChannelDefinitionSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x003A,0x0200, DicomConstants.DCM_DT_SQ, objChannelDefinitionSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objChannelDefinitionSequence;
	}

	/**
	 * Sets Size of each waveform data sample within the Waveform Data
	 * @param enumWaveformBitsAllocated enum value of Bits Allocated
	 */
	public void setWaveformBitsAllocated(EnumWaveformBitsAllocated enumWaveformBitsAllocated)
	{
		/* Accept the WaveformBitsAllocated 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x5400,0x1004,enumWaveformBitsAllocated.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x5400,0x1004,DicomConstants.DCM_DT_US, enumWaveformBitsAllocated.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Size of each waveform data sample within the Waveform Data
	 * @return short - short value of  Bits Allocated
	 */
	public short getWaveformBitsAllocated()
	{
		Object objValue = getValue(0x5400,0x1004);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets attribute identifies Data representation of the waveform data points.
	 * @param enumWaveformSampleInterpretation Enum identifies the Waveform Sample Interpretation
	 */	
	public void setWaveformSampleInterpretation(EnumWaveformSampleInterpretation enumWaveformSampleInterpretation)
	{
		/*Accept WaveformSampleInterpretation in String 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x5400,0x1006,enumWaveformSampleInterpretation.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x5400,0x1006, DicomConstants.DCM_DT_CS,enumWaveformSampleInterpretation.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Return attribute identifies Data representation of the waveform data points.
	 * @return String - identifies the Waveform Sample Interpretation
	 */
	public String getWaveformSampleInterpretation()
	{
		Object objValue = getValue(0x5400,0x1006);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets attribute identifies Value of waveform samples inserted in channels when
	 * input is absent or invalid
	 * @param objWaveformPaddingValue ISingle Collection of bytes.
	 */
	//The element has OB or OW as data type as per Data Dictionary.
	//Implementing OB data type.
	public void setWaveformPaddingValue(ISingleCollection<byte[]> objWaveformPaddingValue)
	{
		/* Accept WaveformPaddingValue in IDicomValue  type OB.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x5400,0x100A,objWaveformPaddingValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x5400,0x100A, DicomConstants.DCM_DT_OB,objWaveformPaddingValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Return attribute identifies Value of waveform samples inserted in channels when
	 * input is absent or invalid
	 * @return Collection of byte[] - returns the collection of Waveforn Padding value
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getWaveformPaddingValue()
	{
		Object objValue = getValue(0x5400,0x100A);
		if(objValue != null)
			return (ISingleCollection<byte[]>)objValue;
		return null;
	}


	/**
	 * Sets Encoded data samples - channel multiplexed
	 * @param objWaveformData ISingle Collection of bytes.
	 */
	//The element has OB or OW as data type as per Data Dictionary.
	//Implementing OB data type.
	public void setWaveformData(ISingleCollection<byte[]> objWaveformData)
	{
		/* Accept WaveformData in IDicomValue  type OB.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x5400,0x1010,objWaveformData,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x5400,0x1010, DicomConstants.DCM_DT_OB,objWaveformData);

		addDataElement(objIDataElement);
	}

	/**
	 * Return value of the Encoded data samples - channel multiplexed
	 * @return Collection of byte[] - returns the collection of channel multiplexed
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getWaveformData()
	{
		Object objValue = getValue(0x5400,0x1010);
		if(objValue != null)
			return (ISingleCollection<byte[]>)objValue;
		return null;
	}
}
