/*******************************************************************************
 *  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.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 provides methods to set,get and validate the attributes in {@link DataInformationSequence}
 */
public class DataInformationSequenceItem extends Item  
{
	

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader.
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	DataInformationSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}
	/**
	 * This constructor creates Item with specified transferSyntax and specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	DataInformationSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in Data Information Sequence Item 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(0x0018,0x1063))
				throw new MandatoryMissingException("0018,1063");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets Nominal time per individual frame in msec. Required if a sequence Item is present.
	 * @param strFrameTime String value of Frame Time.
	 */
	public void setFrameTime(String strFrameTime)
	{
		/*Accept the Frame Time in String format of type DS.
		 * 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,0x1063, strFrameTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1063,DicomConstants.DCM_DT_DS, strFrameTime);

		addDataElement(objIDataElement);
	}
	/**
	 * Return Nominal time per individual frame in msec
	 * @return String - value of Frame Time.
	 */
	public String getFrameTime()
	{
		Object objValue = getValue(0x0018,0x1063);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Average duration of accepted beats, in msec.
	 * @param strNominalInterval String value of Nominal Interval.
	 */
	public void setNominalInterval(String strNominalInterval)
	{
		/*Accept the Nominal Interval in String format of type IS.
		 * 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,0x1062, strNominalInterval,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1062,DicomConstants.DCM_DT_IS, strNominalInterval);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Average duration of accepted beats, in msec.
	 * @return String - value of Nominal Interval.
	 */
	public String getNominalInterval()
	{
		Object objValue = getValue(0x0018,0x1062);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets R-R interval low limit for beat rejection, in msec.
	 * @param strLowRRValue String value of Low R-R Value. 
	 */
	public void setLowRRValue(String strLowRRValue)
	{
		/*Accept Low R-R Value in String format of type IS.
		 * 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
		{	
			objIDataElement = new ImplicitDE(0x0018, 0x1081,strLowRRValue,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0018, 0x1081, DicomConstants.DCM_DT_IS,strLowRRValue);
		}
		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves R-R interval low limit for beat rejection, in msec.
	 * @return String - value of Low R-R Value. 
	 */
	public String getLowRRValue()
	{
		Object objValue = getValue(0x0018,0x1081);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets R-R interval high limit for beat rejection, in msec
	 * @param strHighRRValue String value of High R-R Value. 
	 */
	public void setHighRRValue(String strHighRRValue)
	{
		/*Accept High R-R Value in String format of type IS.
		 * 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
		{	
			objIDataElement = new ImplicitDE(0x0018, 0x1082,strHighRRValue,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0018, 0x1082, DicomConstants.DCM_DT_IS,strHighRRValue);
		}
		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves R-R interval low limit for beat rejection, in msec.
	 * @return String - value of High R-R Value.
	 */
	public String getHighRRValue()
	{
		Object objValue = getValue(0x0018,0x1082);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of R-R intervals acquired.
	 * @param strIntervalsAcquired String value of  Intervals Acquired. 
	 */
	public void setIntervalsAcquired(String strIntervalsAcquired)
	{
		/*Accept Intervals Acquired in String format of type IS.
		 * 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
		{	
			objIDataElement = new ImplicitDE(0x0018, 0x1083,strIntervalsAcquired,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0018, 0x1083, DicomConstants.DCM_DT_IS,strIntervalsAcquired);
		}
		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Number of R-R intervals acquired.
	 * @return String - value of  Intervals Acquired.
	 */
	public String getIntervalsAcquired()
	{
		Object objValue = getValue(0x0018,0x1083);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of R-R intervals rejected.
	 * @param strIntervalsRejected String value of Intervals Rejected.
	 */
	public void setIntervalsRejected(String strIntervalsRejected)
	{
		/*Accept Intervals Rejected in String format of type IS.
		 * 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
		{	
			objIDataElement = new ImplicitDE(0x0018, 0x1084,strIntervalsRejected,objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0018, 0x1084, DicomConstants.DCM_DT_IS,strIntervalsRejected);
		}
		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Number of R-R intervals rejected.
	 * @return String - value of Intervals Rejected.
	 */
	public String getIntervalsRejected()
	{
		Object objValue = getValue(0x0018,0x1084);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

}
