/*******************************************************************************
 *  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.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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;


/**
 * Waveform Reference Macro specifies the Attributes that convey a reference to a DICOM waveform.
 */
public class WaveformReferenceMacro extends Macro  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	CompositeObjectReferenceMacro objCompositeObjectReferenceMacro = null;

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public WaveformReferenceMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}

	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public WaveformReferenceMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public WaveformReferenceMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public WaveformReferenceMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Retrieves the instance of Composite Object Reference Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Composite Object Reference Macro.
	 * @return - instance of CompositeObjectReferenceMacro.
	 */
	public CompositeObjectReferenceMacro getCompositeObjectReferenceMacro()
	{
		if(objCompositeObjectReferenceMacro == null)
		{
			if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
				objCompositeObjectReferenceMacro = new CompositeObjectReferenceMacro(objIDataSet);
			else 
				objCompositeObjectReferenceMacro = new CompositeObjectReferenceMacro(objIItem);

			return objCompositeObjectReferenceMacro;
		}
		return objCompositeObjectReferenceMacro;
	}

	/**
	 * List of channels in Waveform to which the reference applies.
	 * @param arrShReferencedWaveformChannels  short array of Referenced Waveform Channels
	 */
	public void setReferencedWaveformChannels(short[] arrShReferencedWaveformChannels)
	{
		/* Accept the Referenced Waveform Channels 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(0x0040,0xA0B0, arrShReferencedWaveformChannels,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA0B0,DicomConstants.DCM_DT_US, arrShReferencedWaveformChannels);

		addDataElement(objIDataElement);
	}

	/**
	 * Return array of Referenced Waveform Channels
	 * @return short[] - short array of Referenced Waveform Channels
	 */
	public short[] getReferencedWaveformChannels()
	{
		Object objValue = getValue(0x0040,0xA0B0);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}

	/**
	 * Validates all the attributes in Waveform Reference Macro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}

	/**
	 * Validates all the attributes in Waveform Reference Macro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{
			/*
			 * please refer p.d.f. Chapter 3 page No 7989
			 * 0040,A0B0  Referenced Waveform Channels
			 * Required if the Referenced SOP Instance is a Waveform that contains multiple Channels and
			 * not all Channels in the Waveform are referenced.
			 */
			WaveformReferenceMacro objWaveformReferenceMacro = new WaveformReferenceMacro(objIDataSet);
			objWaveformReferenceMacro.getCompositeObjectReferenceMacro().validate();
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(WaveformReferenceMacro.class.getSimpleName(), me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in Waveform Reference Macro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return boolean Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{
			/*
			 * please refer p.d.f. Chapter 3 page No 7989
			 * 0040,A0B0  Referenced Waveform Channels
			 * Required if the Referenced SOP Instance is a Waveform that contains multiple Channels and
			 * not all Channels in the Waveform are referenced.
			 */
			WaveformReferenceMacro objWaveformReferenceMacro = new WaveformReferenceMacro(objIItem);
			objWaveformReferenceMacro.getCompositeObjectReferenceMacro().validate();
		}
		catch( ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(cfe.getMessage());
			}
			throw new InvalidMacroException("Waveform Reference Macro",cfe);
		}
		return true;
	}
}
