/*******************************************************************************
 *  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.enums.EnumPhaseDescription;
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.exceptions.ValidationException;
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 methods to set,get and validate attributes in Phase Information Sequence.
 */
public class PhaseInformationSequenceItem extends Item  
{
	

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader.
	 */
	PhaseInformationSequenceItem()
	{
		super();
	}

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	PhaseInformationSequenceItem(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 
	 */
	PhaseInformationSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in PhaseInformationSequenceItem 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(0x0054,0x0036))
				throw new MandatoryMissingException("0054,0036");
			if(!contains(0x0018,0x1242))
				throw new MandatoryMissingException("0018,1242");
			if(!contains(0x0054,0x0038))
				throw new MandatoryMissingException("0054,0038");
			if(!contains(0x0054,0x0033))
				throw new MandatoryMissingException("0054,0033");
			if(contains(0x0054,0x0210))
				if(!contains(0x0054,0x0211))
					throw new ConditionFailedException("0x0054,0x0211");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}

	/**
	 * Sets time paused between the last frame of the previous phase and the first frame of
	 * this phase, in msec. Required if a sequence Item is present.
	 * @param strPhaseDelay String value of Phase Delay.
	 */
	public void setPhaseDelay(String strPhaseDelay)
	{
		/*Accept the Phase Delay 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(0x0054,0x0036, strPhaseDelay,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0054,0x0036,DicomConstants.DCM_DT_IS, strPhaseDelay);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves time paused between the last frame of the previous phase and the first frame of
	 * this phase, in msec.
	 * @return String - value of Phase Delay.
	 */
	public String getPhaseDelay()
	{
		Object objValue = getValue(0x0054,0x0036);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/** 
	 * Sets Nominal time of acquisition per individual frame, in msec. Required if a sequence
	 * Item is present.
	 * @param strActualFrameDuration String value of Actual Frame Duration.
	 */
	public void setActualFrameDuration(String strActualFrameDuration)
	{
		/*Accept the Actual Frame Duration 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,0x1242, strActualFrameDuration,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1242,DicomConstants.DCM_DT_IS, strActualFrameDuration);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Nominal time of acquisition per individual frame, in msec.
	 * @return String - value of Actual Frame Duration.
	 */
	public String getActualFrameDuration()
	{
		Object objValue = getValue(0x0018,0x1242);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets time paused between each frame of this phase (in msec). Required if a sequence
	 * Item is present.
	 * @param strPauseBetweenFrames String value of Pause Between Frames.
	 */
	public void setPauseBetweenFrames(String strPauseBetweenFrames)
	{
		/*Accept the Pause Between Frames 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(0x0054,0x0038, strPauseBetweenFrames,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0054,0x0038,DicomConstants.DCM_DT_IS, strPauseBetweenFrames);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves time paused between each frame of this phase (in msec).
	 * @return String - value of Pause Between Frames.
	 */
	public String getPauseBetweenFrames()
	{
		Object objValue = getValue(0x0054,0x0038);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of frames in this phase. Required if a sequence Item is present.
	 * @param shNumberOfFramesInPhase short value of Number Of Frames in Phase.
	 */
	public void setNumberOfFramesInPhase(short shNumberOfFramesInPhase)
	{
		/*Accept the Number Of Frames In Phase 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0054, 0x0033, shNumberOfFramesInPhase,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0054, 0x0033,DicomConstants.DCM_DT_US, shNumberOfFramesInPhase);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of frames in this phase.
	 * @return short - short value of Number Of Frames in Phase.
	 */
	public short getNumberOfFramesInPhase()
	{
		Object objValue = getValue(0x0054,0x0033);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets an array of trigger times when gating information is acquired simultaneously
	 * with the dynamic image data.
	 * @param arrStrTriggerVector String array of Trigger Vector.
	 */
	public void setTriggerVector(String[] arrStrTriggerVector)
	{
		/*Accept the Trigger Vector 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(0x0054,0x0210, arrStrTriggerVector,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0054,0x0210,DicomConstants.DCM_DT_IS, arrStrTriggerVector);

		/*
		 * Instead of directly accessing the internal cllection of Item, we need use its own method
		 * to add the dataelement
		 */
		//objHashMap.put(objITag, objIDataElement);
		addDataElement(objIDataElement);
	}
	/**
	 * Return an array of trigger times when gating information is acquired simultaneously
	 * with the dynamic image data.
	 * @return String[] - String array of Trigger Vector.
	 */
	public String[] getTriggerVector()
	{
		Object objValue = getValue(0x0054,0x0210);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets the number of entries in the Trigger Vector (0054,0210) for this phase.
	 * Required if Trigger Vector (0054,0210) is present.
	 * @param shNumberOfTriggersInPhase short value of Number Of Triggers in Phase.
	 */
	public void setNumberOfTriggersInPhase(short shNumberOfTriggersInPhase)
	{
		/*Accept the Number Of Triggers In Phase 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0054, 0x0211, shNumberOfTriggersInPhase,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0054, 0x0211,DicomConstants.DCM_DT_US, shNumberOfTriggersInPhase);

		addDataElement(objIDataElement);
	}
	/**
	 * Return number of entries in the Trigger Vector.
	 * @return short - short value of Number Of Triggers in Phase.
	 */
	public short setNumberOfTriggersInPhase()
	{
		Object objValue = getValue(0x0054,0x0211);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets description of this phase of the Dynamic image.Defined Terms:FLOW, WASHOUT, UPTAKE,EMPTYING,EXCRETION
	 * @param enumPhaseDescription Enum value of Phase Description.
	 */
	public void setPhaseDescription(EnumPhaseDescription enumPhaseDescription)
	{
		/*Accept the Phase Description in String format of type CS.
		 * 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(0x0054,0x0039, enumPhaseDescription.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0054,0x0039,DicomConstants.DCM_DT_CS, enumPhaseDescription.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieve description of this phase of the Dynamic image.
	 * @return String - value of Phase Description.
	 */
	public String setPhaseDescription()
	{
		Object objValue = getValue(0x0054,0x0039);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
