/*******************************************************************************
 *  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 item introduces sequence of verification images obtained during delivery of current beam.
 * The sequence may contain one or more items.
 */
public class ReferencedVerificationImageSequenceItem extends Item  
{
	


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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	ReferencedVerificationImageSequenceItem()
	{
		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
	 */
	ReferencedVerificationImageSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Sets Referenced SOP Class UID which Uniquely identifies the referenced SOP Class
	 * @param strReferencedSOPClassUID String value of Referenced SOP Class UID
	 */
	public void setReferencedSOPClassUID(String strReferencedSOPClassUID)
	{
		/*Accept the ReferencedSOPClassUID in String format of type UI.
		 * 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(0x0008, 0x1150, strReferencedSOPClassUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x1150,DicomConstants.DCM_DT_UI, strReferencedSOPClassUID);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Referenced SOP Class UID.Uniquely identifies the referenced SOP Class.
	 * @return String - value of Referenced SOP Class UID.
	 */
	public String getReferencedSOPClassUID()
	{
		Object objValue = getValue(0x0008, 0x1150);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Referenced SOP Instance UID which Uniquely identifies the referenced SOP Instance
	 * @param strReferencedSOPInstanceUID String value of Referenced SOP Instance UID
	 */
	public void setReferencedSOPInstanceUID(String strReferencedSOPInstanceUID)
	{
		/*Accept the ReferencedSOPInstanceUID in String format of type UI.
		 * 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(0x0008,0x1155, strReferencedSOPInstanceUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x1155,DicomConstants.DCM_DT_UI, strReferencedSOPInstanceUID);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Referenced SOP Instance UID.Uniquely identifies the referenced SOP Instance.
	 * @return String - value of Referenced SOP Instance UID.
	 */
	public String getReferencedSOPInstanceUID()
	{
		Object objValue = getValue(0x0008,0x1155);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Start Meterset . Cumulative Meterset Weight within Beam referenced by Referenced Beam Number at which image acquisition starts.
	 * @param strStartMeterset String value of Start Meterset.
	 */
	public void setStartMeterset(String strStartMeterset)
	{
		/*Accept the StartMeterset 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(0x3008,0x0078, strStartMeterset,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0078,DicomConstants.DCM_DT_DS, strStartMeterset);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Cumulative Meterset Weight within Beam referenced by Referenced Beam Number at which image acquisition starts.
	 * @return String - value of Start Meterset.
	 */
	public String getStartMeterset()
	{
		Object objValue = getValue(0x3008,0x0078);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets End Meterset . Cumulative Meterset Weight within Beam referenced by Referenced Beam Number at which image acquisition ends.
	 * @param strEndMeterset String value of End Meterset.
	 */
	public void setEndMeterset(String strEndMeterset)
	{
		/*Accept the EndMeterset 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(0x3008,0x007A, strEndMeterset,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x007A,DicomConstants.DCM_DT_DS, strEndMeterset);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Cumulative Meterset Weight within Beam referenced by Referenced Beam Number at which image acquisition ends.
	 * @return String - value of End Meterset.
	 */
	public String getEndMeterset()
	{
		Object objValue = getValue(0x3008,0x007A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in ReferencedVerificationImageSequenceItem that are present in the DataSet
	 * @return int - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			if(!contains(0x0008,0x1150))
				throw new ConditionFailedException("conditional Element 0008,1150 should present if  300C,0040 is present") ;
			if(!contains(0x0008,0x1155))
				throw new ConditionFailedException("conditional Element 0008,1155 should present if  300C,0040 is present") ;

		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
