/*******************************************************************************
 *  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.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumASLBolusCutoffFlag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumASLContext;
import cdac.medinfo.sdk.dcm30_04.enums.EnumASLCrusherFlag;
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 the methods to set, get and validate the attribute present in MR Arterial Spin Labeling.
 */


public class MRArterialSpinLabelingSequenceItem extends Item
{
	


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

	/**
	 * This constructor creates the item with default TransferSyntax 
	 */
	MRArterialSpinLabelingSequenceItem()
	{
		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 with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	MRArterialSpinLabelingSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/*
	 * (non-Javadoc) This method validates the contents of the Item
	 * @see cdac.medinfo.sdk.dcm30_04.commoninterface.IItem#validate()
	 */
	/**
	 * Validates all the attributes in MRArterialSpinLabelingSequenceItem 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(0x0018,0x9259))
				throw new MandatoryMissingException("0018,9259");
			if(!contains(0x0018,0x925C))
				throw new MandatoryMissingException("0018,925C");
			if(contains(0x0018,0x9257) && (getDataForTag(0x0018,0x9257).getValue().equals(EnumASLContext.CONTROL) || getDataForTag(0x0018,0x9257).getValue().equals(EnumASLContext.LABEL)) )
				if(!contains(0x0018,0x9260))
					throw new ConditionFailedException("0x0018,0x9260");
			if(contains(0x0018,0x9259) && (getDataForTag(0x0018,0x9259).getValue().equals(EnumASLCrusherFlag.YES)) )
				if(!contains(0x0018,0x925A))
					throw new ConditionFailedException("0x0018,0x925A");
			if(contains(0x0018,0x9259) && (getDataForTag(0x0018,0x9259).getValue().equals(EnumASLCrusherFlag.YES)) )
				if(!contains(0x0018,0x925B))
					throw new ConditionFailedException("0x0018,0x925B");
			if(contains(0x0018,0x925C) && (getDataForTag(0x0018,0x925C).getValue().equals(EnumASLBolusCutoffFlag.YES)) )
				if(!contains(0x0018,0x925D))
					throw new ConditionFailedException("0x0018,0x925D");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(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 ASL Technique Description.
	 * Text describing the used labeling technique in more detail.
	 * @param strASLTechniqueDescription String value of ASL Technique Description.
	 */
	public void setASLTechniqueDescription(String strASLTechniqueDescription)
	{
		/* Accept the ASL Technique Description in String format of type LO.
		 * 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,0x9252, strASLTechniqueDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9252,DicomConstants.DCM_DT_LO, strASLTechniqueDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return ASL Technique Description.
	 * Text describing the used labeling technique in more detail.
	 * @return String - value of ASL Technique Description.
	 */
	public String getASLTechniqueDescription()
	{
		Object objValue = getValue(0x0018,0x9252);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets ASL Context.
	 * The purpose of the Arterial Spin Labeling.
	 * Defined Terms:
	 * 1) LABEL.
	 * 2) CONTROL.
	 * 3) M_ZERO_SCAN.
	 * @param enumASLContext enum value of ASL Context.
	 */
	public void setASLContext(EnumASLContext enumASLContext)
	{
		/* Accept the ASL Context 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,0x9257, enumASLContext.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9257,DicomConstants.DCM_DT_CS, enumASLContext.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return ASL Context.
	 * The purpose of the Arterial Spin Labeling. 
	 * @return String - value of ASL Context.
	 */
	public String getASLContext()
	{
		Object objValue = getValue(0x0018,0x9257);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of ASL Slab Sequence.If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence.
	 * @return ASLSlabSequence - instance of ASLSlabSequence. 
	 */
	public ASLSlabSequence getASLSlabSequence()
	{
		ASLSlabSequence objASLSlabSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0018, 0x9260);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objASLSlabSequence = (ASLSlabSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objASLSlabSequence = (ASLSlabSequence)objDicomV3Factory.createSequence(0x0018, 0x9260, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0018, 0x9260, objASLSlabSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0018, 0x9260, DicomConstants.DCM_DT_SQ, objASLSlabSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objASLSlabSequence;
	}
	
	/**
	 * Sets ASL Crusher Flag.
	 * Indicates if an ASL Crusher Method has been used.
	 * Defined Terms:
	 * 1) YES.
	 * 2) NO.
	 * @param enumASLCrusherFlag enum value of ASL Crusher Flag.
	 */
	public void setASLCrusherFlag(EnumASLCrusherFlag enumASLCrusherFlag)
	{
		/* Accept the ASL Crusher Flag 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,0x9259, enumASLCrusherFlag.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9259,DicomConstants.DCM_DT_CS, enumASLCrusherFlag.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return ASL Crusher Flag. 
	 * Indicates if an ASL Crusher Method has been used.
	 * @return String - value of ASL Crusher Flag.
	 */
	public String getASLCrusherFlag()
	{
		Object objValue = getValue(0x0018,0x9259);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the ASL Crusher Flow Limit.
	 * Maximum Flow Limit (in cm/s).
	 * @param dblASLCrusherFlowLimit Double value of ASL Crusher Flow Limit
	 */
	public void setASLCrusherFlowLimit(double dblASLCrusherFlowLimit)
	{
		/*Accept the ASL Crusher Flow Limit 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x925A, dblASLCrusherFlowLimit,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x925A,DicomConstants.DCM_DT_FD, dblASLCrusherFlowLimit);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the ASL Crusher Flow Limit.
	 * Maximum Flow Limit (in cm/s).
	 * @return double - double value of ASL Crusher Flow Limit
	 */
	public double getASLCrusherFlowLimit()
	{
		Object objValue = getValue(0x0018,0x925A);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets ASL Crusher Description.
	 * Description of the ASL Crusher Method.
	 * @param strASLCrusherDescription String value of ASL Crusher Description.
	 */
	public void setASLCrusherDescription(String strASLCrusherDescription)
	{
		/* Accept the ASL Crusher Description in String format of type LO.
		 * 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,0x925B, strASLCrusherDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x925B,DicomConstants.DCM_DT_LO, strASLCrusherDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return ASL Crusher Description.
	 * Description of the ASL Crusher Method.
	 * @return String - value of ASL Crusher Description.
	 */
	public String getASLCrusherDescription()
	{
		Object objValue = getValue(0x0018,0x925B);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets ASL Bolus Cutoff Flag.
	 * Indicates if a Bolus Cut-off technique is used.
	 * Defined Terms:
	 * 1) YES.
	 * 2) NO.
	 * @param enumASLBolusCutoffFlag enum value of ASL Bolus Cutoff Flag.
	 */
	public void setASLBolusCutoffFlag(EnumASLBolusCutoffFlag enumASLBolusCutoffFlag)
	{
		/* Accept the ASL Bolus Cutoff Flag 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,0x925C, enumASLBolusCutoffFlag.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x925C,DicomConstants.DCM_DT_CS, enumASLBolusCutoffFlag.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return ASL Bolus Cutoff Flag. 
	 * Indicates if a Bolus Cut-off technique is used.
	 * @return String - value of ASL Bolus Cutoff Flag.
	 */
	public String getASLBolusCutoffFlag()
	{
		Object objValue = getValue(0x0018,0x925C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of ASL Bolus Cutoff Timing Sequence.If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence.
	 * @return ASLBolusCutoffTimingSequence - instance of ASLBolusCutoffTimingSequence. 
	 */
	public ASLBolusCutoffTimingSequence getASLBolusCutoffTimingSequence()
	{
		ASLBolusCutoffTimingSequence objASLBolusCutoffTimingSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0018, 0x925D); //getDataElement
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objASLBolusCutoffTimingSequence = (ASLBolusCutoffTimingSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objASLBolusCutoffTimingSequence = (ASLBolusCutoffTimingSequence)objDicomV3Factory.createSequence(0x0018, 0x925D, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0018, 0x925D, objASLBolusCutoffTimingSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0018, 0x925D, DicomConstants.DCM_DT_SQ, objASLBolusCutoffTimingSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objASLBolusCutoffTimingSequence;
	}

}
