/*******************************************************************************
 *  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.dicommodel.modules;



import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRespiratoryMotionCompensation;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRespiratorySignalSource;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRespiratoryTriggerType;

import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 \* This module specifies the attributes of the Respiratory Synchronization Module.
 */

public class RespiratorySynchronizationModule extends Module  
{


	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0018,0x9170));
		objSingleCollection.add(new Tag(0x0018,0x9171));
	}

	/**
	 * Instantiates a new respiratory synchronization module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public RespiratorySynchronizationModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return - returns validation status as success or failure
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			
			return true;

		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());
			String strRespiratoryMotionCompensationTechnique = getRespiratoryMotionCompensationTechnique();

			if((!strRespiratoryMotionCompensationTechnique.equals(EnumRespiratoryMotionCompensation.NONE))||(!strRespiratoryMotionCompensationTechnique.equals(EnumRespiratoryMotionCompensation.REALTIME))||(!strRespiratoryMotionCompensationTechnique.equals(EnumRespiratoryMotionCompensation.BREATH_HOLD)))
				if(!isPresent(0x0020,0x9256))
					throw new ConditionFailedException("0020,9256 is Required if Respiratory Motion Compensation Technique (0018,9170) equals otherthan NONE, REALTIME or BREATH_HOLD");

			String strRespiratoryTriggerType = getRespiratoryTriggerType();
			if((strRespiratoryTriggerType.equals(EnumRespiratoryTriggerType.TIME)))
				throw new ConditionFailedException("0020,9250 is Required if the value is not TIME, may be present otherwise.");

			throw new InvalidModuleException(this.getClass().getSimpleName(),mme);
		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());			
				throw new InvalidModuleException(this.getClass().getSimpleName(),cfe);

		}
	}

	/**
	 * Sets applied technique to reduce respiratory motion artifacts.
	 * Defined Terms:
	 * 1) NONE
	 * 2) BREATH_HOLD
	 * 3) REALTIME = image acquisition shorter than respiratory cycle.
	 * 4) GATING = Prospective gating.
	 * 5) TRACKING = prospective throughplane or in-plane motion tracking.
	 * 6) PHASE_ORDERING = prospective phase ordering.
	 * 7) PHASE_RESCANNING = prospective techniques, such as real-time averaging, diminishing variance and motion adaptive gating.
	 * 8) RETROSPECTIVE = retrospective gating.
	 * 9) CORRECTION = retrospective image correction.
	 * 
	 * @param enumRespiratoryMotionCompensationTechnique - Enum value of Respiratory Motion Compensation Technique.
	 */	
	public void setRespiratoryMotionCompensationTechnique(EnumRespiratoryMotionCompensation enumRespiratoryMotionCompensationTechnique)
	{
		/* Accept the Respiratory Motion Compensation Technique value 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 Hash Map
		 */
		String strRespiratoryMotionCompensationTechnique = enumRespiratoryMotionCompensationTechnique.getValue();
		addDataElement(0x0018, 0x9170, DicomConstants.DCM_DT_CS, strRespiratoryMotionCompensationTechnique);
	}

	/**
	 * Retrieves applied technique to reduce respiratory motion artifacts.
	 * 
	 * @return - String value of Respiratory Motion Compensation Technique.
	 */
	public String getRespiratoryMotionCompensationTechnique()
	{
		Object objectValue = getValue(0x0018, 0x9170);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets signal source from which respiratory motion is derived.
	 * Defined Terms:
	 * 1) NONE
	 * 2) BELT
	 * 3) NASAL_PROBE
	 * 4) CO2_SENSOR
	 * 5) NAVIGATOR = MR navigator and organ edge detection.
	 * 6) MR_PHASE = phase (of center k-space line).
	 * 7) ECG = baseline demodulation of the ECG.
	 * 
	 * @param enumRespiratorySignalSource - Enum value of Respiratory Signal Source.
	 */	
	public void setRespiratorySignalSource(EnumRespiratorySignalSource enumRespiratorySignalSource)
	{
		/* Accept the Respiratory Signal Source value 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 Hash Map
		 */
		String strRespiratorySignalSource = enumRespiratorySignalSource.getValue();
		addDataElement(0x0018, 0x9171, DicomConstants.DCM_DT_CS, strRespiratorySignalSource);
	}

	/**
	 * Retrieves signal source from which respiratory motion is derived.
	 * 
	 * @return - String value of Respiratory Signal Source.
	 */
	public String getRespiratorySignalSource()
	{
		Object objectValue = getValue(0x0018, 0x9171);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Sets Respiratory trigger threshold in percent of the chest expansion for the frame relative to the last Respiratory-Peak.
	 * @param dblRespiratoryTriggerDelayThreshold Double value of RespiratoryTriggerDelayThreshold
	 */
	public void setRespiratoryTriggerDelayThreshold(double dblRespiratoryTriggerDelayThreshold)
	{
		/*Accept the Respiratory trigger threshold in double format of type FD.
		 * 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
		 */
		addDataElement(0x0020, 0x9256, DicomConstants.DCM_DT_FL, dblRespiratoryTriggerDelayThreshold);
	}
	
	/**
	 * Retrieves Respiratory trigger threshold in percent of the chest expansion for the frame relative to the last Respiratory-Peak.
	 * @return double - double value of RespiratoryTriggerDelayThreshold
	 */
	public double getRespiratoryTriggerDelayThreshold()
	{
		Object objValue = getValue(0x0020,0x9256);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	
	/**
	 * Sets Characteristic of the respiratory signal used to the define the respiratory triggering.
	 * Enumerated Values:
	 * TIME.
	 * AMPLITUDE.
	 * BOTH
	 * @param enumRespiratoryTriggerType String value of Stereo Pairs Present.
	 */
	public void setRespiratoryTriggerType(EnumRespiratoryTriggerType enumRespiratoryTriggerType)
	{
		/* Accept StereoPairsPresent 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 Hash Map
		 */
		addDataElement(0x0020,0x9250, DicomConstants.DCM_DT_CS, enumRespiratoryTriggerType.getValue());
	}
	
	/**
	 * Returns Characteristic of the respiratory signal used to the define the respiratory triggering.
	 * 
	 * @return String - String value of RespiratoryTriggerType.
	 */
	public String getRespiratoryTriggerType()
	{
		 Object objValue = getValue(0x0020,0x9250);
		 if(objValue!=null)
			 return objValue.toString();
		 return null;

	}	

	/**
	 * This method returns the vector of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return - the customized collection containing the dataelement tags
	 * that beling to this module
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
