/*******************************************************************************
 *  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.ConditionFailedException;
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 class contains the set and get method for all the attribute present in Calibration Data Sequence
 */
public class CalibrationDataSequenceItem extends Item  
{
	        

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

	/**
		 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
		 * and default DictionaryReader
		 */
		CalibrationDataSequenceItem()
		{
			super();
		}
		
		/**
		 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
		 * and default DictionaryReader
		 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
		 */
		CalibrationDataSequenceItem(ITransferSyntax objITransferSyntax)
		{
			super(objITransferSyntax);
		}
		
		/**
		 * This constructor creates SQ with specified transferSyntax and specified DictionaryReader
		 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
		 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
		 */
		CalibrationDataSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
		{
			super(objTransferSyntax,objDictionaryReader);
		}
		
		/**
		 * Validates all the attributes in AnatomicRegionSequence
		 * @return boolean - Success -1, Failure -0.
		 * @throws InvalidItemException throws InvalidItemException when Item validation fails.
		 */
		public boolean validateForModule() throws InvalidItemException
		{
			try{
				if(!contains(0x0054,0x0308))
					throw new MandatoryMissingException("0054,0308");
			}
			catch(ConditionFailedException ce)
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger.getLogger().log(ce.getMessage());
				}
				throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
			}
			catch( MandatoryMissingException me)
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger.getLogger().log(me.getMessage());
				}
				throw new InvalidItemException(this.getClass().getSimpleName(),me);
			}
			return true;
		}
		
		/**
		 * The Item number in the Energy Window Information Sequence to which the following calibration data relates. The
		 * Items are numbered starting from 1. Required if a sequence Item is present.
		 * @param shEnergyWindowNumber -short value of item number in the Energy Window Information Sequence
		 */
		public void setEnergyWindowNumber(short shEnergyWindowNumber)
		{
			/*Accept the Energy Window Number 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
			 */
			int iVRTransferSyntax=0;
			IDataElement objIDataElement = null;
			
			if(objITransferSyntax != null)
				iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			else
			{
				//Transfer Syntax not set
			}
			
			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)	
				objIDataElement = new ImplicitDE(0x0054, 0x0308, shEnergyWindowNumber,objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0054, 0x0308,DicomConstants.DCM_DT_US, shEnergyWindowNumber);
			
			addDataElement(objIDataElement);
		}
		
		/**
		 * Retrieves Item number in the Energy Window Information Sequence
		 * @return short - Energy Window Number
		 */
		public short getEnergyWindowNumber()
		{
			if(getDataForTag(0x0054, 0x0308)!=null)
			{
				if(getDataForTag(0x0054, 0x0308).getValue()!=null)
				{
					return Short.parseShort(getDataForTag(0x0054, 0x0308).getValue().toString());
				}
			}
			return DicomConstants.DCM_NO_VALUE;
		}
		
		/**
		 * Sets Pre-injection syringe count rate in counts/sec.
		 * @param strSyringeCounts- String value of syringe count rate in counts/sec.
		 */
		public void setSyringeCounts(String strSyringeCounts)
		{
			/*Accept the Syringe Counts 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
			 */
			int iVRTransferSyntax=0;
			IDataElement objIDataElement = null;
			
			if(objITransferSyntax != null)
				iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			else
			{
				//Transfer Syntax not set
			}
			
			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)	
				objIDataElement = new ImplicitDE(0x0018, 0x1045, strSyringeCounts,objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0018, 0x1045,DicomConstants.DCM_DT_IS, strSyringeCounts);
			
			addDataElement(objIDataElement);
		}
		
		/**
		 * Retrieves syringe count rate in counts/sec.
		 * @return String - syringe count rate in counts/sec.
		 */
		public String getSyringeCounts()
		{
			Object objValue = getValue(0x0018, 0x1045);
			if(objValue!=null)
				objValue.toString();
			return null;
		}
		
		/**
		 * Sets Post-injection residue syringe count rate in counts/sec.
		 * @param strResidualSyringeCounts -String value of residue syringe count rate
		 */
		public void setResidualSyringeCounts(String strResidualSyringeCounts)
		{
			/*Accept the ResidualSyringeCounts 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
			 */
			int iVRTransferSyntax=0;
			IDataElement objIDataElement = null;
			
			if(objITransferSyntax != null)
				iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			else
			{
				//Transfer Syntax not set
			}
			
			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)	
				objIDataElement = new ImplicitDE(0x0054, 0x0017, strResidualSyringeCounts,objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0054, 0x0017,DicomConstants.DCM_DT_IS, strResidualSyringeCounts);
			
			addDataElement(objIDataElement);
		}
		
		/**
		 * Returns Post-injection residue syringe count rate in counts/sec.
		 * @return String - residue syringe count rate
		 */
		public String getResidualSyringeCounts()
		{
			Object objValue = getValue(0x0054, 0x0017);
			if(objValue!=null)
				objValue.toString();
			return null;
		}
}
