/*******************************************************************************
 *  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 Wave form Presentation Group Sequence  {@link WaveformPresentationGroupSequence}.
 *
 */

public class WaveformPresentationGroupSequenceItem extends Item{

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

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

	/**
	 * Sets a UID that can be used to uniquely reference this signature.
	 * @param strStudyInstanceUID String value of Digital Signature UID.
	 */
	public void setStudyInstanceUID(String strStudyInstanceUID)
	{
		/* Accept the Digital Signature UID in String format of type UI.
		 * Unique identifier for a Study that contains the images to which the Presentation applies.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0020, 0x000D, strStudyInstanceUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0020, 0x000D,DicomConstants.DCM_DT_UI, strStudyInstanceUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves a UID that can be used to uniquely reference this signature.
	 * @return String - value of Study Instance UID.
	 */
	public String getStudyInstanceUID()
	{
		Object objValue = getValue(0x0020, 0x000D);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of significant bits within the Presentation Group Number
	 * @param shPresentationGroupNumber short value of Number of significant bits
	 */
	public void setPresentationGroupNumber(short shPresentationGroupNumber)
	{
		/* Accept the WaveformBitsStored of type US.
		 * A number that identifies the Presentation Group
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x003A,0x0241,shPresentationGroupNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x003A,0x0241,DicomConstants.DCM_DT_US, shPresentationGroupNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of significant bits within the Presentation Group Number
	 * @return short - short value of Number of significant bits
	 */
	public short getPresentationGroupNumber()
	{
		Object objValue = getValue(0x003A,0x0241);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Retrieves pre-initialized object of Channel Display 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 ChannelDisplaySequence - instance of ChannelDisplaySequence. 
	 */
	public ChannelDisplaySequence getChannelDisplaySequence()
	{
		ChannelDisplaySequence objChannelDisplaySequence = null;
		IDataElement objIDataElement =  getDataForTag(0x003A, 0x0242);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objChannelDisplaySequence = (ChannelDisplaySequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objChannelDisplaySequence = (ChannelDisplaySequence)objDicomV3Factory.createSequence(0x003A, 0x0242, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Validates all the attributes in WaveformPresentationGroupSequenceItem 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(0x003A,0x0241))
				throw new MandatoryMissingException("003A,0241") ;
			if(!contains(0x003A,0x0242))
				throw new MandatoryMissingException("003A,0242") ;
			
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}

}
