/*******************************************************************************
 *  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.EnumInplanePhaseEncodingDirection;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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 contains methods to validate, set and get attributes in {@link MRFOVGeometrySequence}. 
*/
public class MRFOVGeometrySequenceItem extends Item
{
	    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal MR FOV/Geometry Macro with default TransferSyntax 
	 */
	MRFOVGeometrySequenceItem()
	{
		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 specified TS
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	MRFOVGeometrySequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates the item and internal MR FOV/Geometry Macro with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	MRFOVGeometrySequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in MR FOV/Geometry Macro 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
		{
			/*
			 * Since the conditions specified for attributes are not cleared, validation is not done.
			 * Refer 3rd PDF
			 * Page No - 608. 
			 */
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		catch( InvalidMacroException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ime.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ime);
		}
		return true;
	}
	
	/**
	 * Sets The axes of the in-plane phase encoding with respect to the frame.
	 * @param enumShapeType Enum value for geometric interpretation of the Contour Data
	 */
	public void setInplanePhaseEncodingDirection(EnumInplanePhaseEncodingDirection enumInplanePhaseEncodingDirection)
	{
		/*Accept the In-plane Phase Encoding Direction 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(0x0018,0x1312, enumInplanePhaseEncodingDirection.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1312,DicomConstants.DCM_DT_CS, enumInplanePhaseEncodingDirection.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves The axes of the in-plane phase encoding with respect to the frame.
	 * @return String - value for The axes of the in-plane phase encoding with respect to the frame.
	 */
	public String getInplanePhaseEncodingDirection()
	{
		Object objValue = getValue(0x0018,0x1312);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of Frequency Encoding steps (kx) acquired
	 * Required if the Annotation Content Sequence is sent.
	 * @param shMRAcquisitionFrequencyEncodingSteps short value of MR Acquisition Frequency Encoding Steps.
	 */
	public void setMRAcquisitionFrequencyEncodingSteps(short shMRAcquisitionFrequencyEncodingSteps)
	{
		/* Accept the MR Acquisition Frequency Encoding Steps 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(0x0018,0x9058,shMRAcquisitionFrequencyEncodingSteps,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9058,DicomConstants.DCM_DT_US, shMRAcquisitionFrequencyEncodingSteps);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of Frequency Encoding steps (kx) acquired 
	 * @return short - value of MR Acquisition Frequency Encoding Steps.
	 */
	public short getMRAcquisitionFrequencyEncodingSteps()
	{
		Object objValue = getValue(0x0018,0x9058);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Number of In-Plane Phase Encoding steps (ky) acquired
	 * @param shMRAcquisitionPhaseEncodingStepsinplane short value of MR Acquisition Phase Encoding Steps in-plane.
	 */
	public void setMRAcquisitionPhaseEncodingStepsinplane(short shMRAcquisitionPhaseEncodingStepsinplane)
	{
		/* Accept the MR Acquisition Phase Encoding Steps in-plane 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(0x0018,0x9231,shMRAcquisitionPhaseEncodingStepsinplane,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9231,DicomConstants.DCM_DT_US, shMRAcquisitionPhaseEncodingStepsinplane);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of In-Plane Phase Encoding steps (ky) acquired 
	 * @return short - value of MR Acquisition Phase Encoding Steps in-plane.
	 */
	public short getMRAcquisitionPhaseEncodingStepsinplane()
	{
		Object objValue = getValue(0x0018,0x9231);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Number of Out-of-Plane Phase Encoding steps (kz) acquired
	 * @param shMRAcquisitionPhaseEncodingStepsoutofplane short value of MR Acquisition Phase Encoding Steps out-of-plane.
	 */
	public void setMRAcquisitionPhaseEncodingStepsoutofplane(short shMRAcquisitionPhaseEncodingStepsoutofplane)
	{
		/* Accept the MR Acquisition Phase Encoding Steps out-of-plane 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(0x0018,0x9232,shMRAcquisitionPhaseEncodingStepsoutofplane,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9232,DicomConstants.DCM_DT_US, shMRAcquisitionPhaseEncodingStepsoutofplane);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of Out-of-Plane Phase Encoding steps (kz) acquired
	 * @return short - value of MR Acquisition Phase Encoding Steps out-of-plane.
	 */
	public short getMRAcquisitionPhaseEncodingStepsoutofplane()
	{
		Object objValue = getValue(0x0018,0x9232);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Fraction of acquisition matrix lines acquired, expressed as a percent.
	 * @param strPercentSampling String value of Percent Sampling.
	 */
	public void setPercentSampling(String strPercentSampling)
	{
		/*Accept the PercentSampling 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,0x0093, strPercentSampling,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x0093,DicomConstants.DCM_DT_DS, strPercentSampling);

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves Fraction of acquisition matrix lines acquired, expressed as a percent.
	 * @return String - value of Percent Sampling.
	 */
	public String getPercentSampling()
	{
		Object objValue = getValue(0x0018,0x0093);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Fraction of acquisition matrix lines acquired, expressed as a percent.
	 * @param strPercentPhaseFieldofView String value of Percent Phase Field of View.
	 */
	public void setPercentPhaseFieldofView(String strPercentPhaseFieldofView)
	{
		/*Accept the PercentPhaseFieldofView 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,0x0094, strPercentPhaseFieldofView,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x0094,DicomConstants.DCM_DT_DS, strPercentPhaseFieldofView);

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves Fraction of acquisition matrix lines acquired, expressed as a percent.
	 * @return String - value of Percent Phase Field of View.
	 */
	public String getPercentPhaseFieldofView()
	{
		Object objValue = getValue(0x0018,0x0094);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
