/*******************************************************************************
 *  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.EnumDoubleExposureFlag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumDoubleExposureOrdering;
import cdac.medinfo.sdk.dcm30_04.enums.EnumVerificationImageTiming;
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 Delivery Verification ImageSequence .
 */
public class DeliveryVerificationImageSequenceItem extends Item
{


	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	DeliveryVerificationImageSequenceItem()
	{
		super();
	}


	/**
	 * This constructor creates the item and internal code sequence macro with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	DeliveryVerificationImageSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates DeliveryVerificationImageSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	DeliveryVerificationImageSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	/**
	 * Validates all the attributes in DeliveryVerificationImageSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			if(contains(0x300C,0x0008) && getDataForTag(0x0074,0x1032).getValue().equals(EnumVerificationImageTiming.BEFORE_BEAM) || 
					 getDataForTag(0x0074,0x1032).getValue().equals(EnumVerificationImageTiming.AFTER_BEAM))
					throw new ConditionFailedException("0x300C,0x0008") ;
			if(contains(0x3002,0x0032) && getDataForTag(0x0074,0x1032).getValue().equals(EnumVerificationImageTiming.DURING_BEAM))
					throw new ConditionFailedException("0x3002,0x0032") ;
			if(contains(0x300C,0x0009) && getDataForTag(0x0074,0x1032).getValue().equals(EnumVerificationImageTiming.BEFORE_BEAM) || 
					 getDataForTag(0x0074,0x1032).getValue().equals(EnumVerificationImageTiming.AFTER_BEAM))
					throw new ConditionFailedException("0x300C,0x0009") ;
			if(!contains(0x0074,0x1032))
				throw new MandatoryMissingException("0074,1032") ;
			if(!contains(0x0074,0x1034))
				throw new MandatoryMissingException("0074,1034") ;
			if(contains(0x0074,0x1036) && getDataForTag(0x0074,0x1034).getValue().equals(EnumDoubleExposureFlag.SINGLE))
					throw new ConditionFailedException("0x0074,0x1036") ;	
			if(contains(0x0074,0x1338) && getDataForTag(0x0074,0x1034).getValue().equals(EnumDoubleExposureFlag.SINGLE))
				throw new ConditionFailedException("0x0074,0x1338") ;	
			if(contains(0x0074,0x133A) && getDataForTag(0x0074,0x1034).getValue().equals(EnumDoubleExposureFlag.SINGLE))
				throw new ConditionFailedException("0x0074,0x133A") ;	
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	
	
	/**
	 * Sets the temporal relationship of the verification
	 * @param enumVerificationImageTiming Specifies type of the value encoded in this Item
	 */
	public void setVerificationImageTiming(EnumVerificationImageTiming enumVerificationImageTiming)
	{
		/*Accept the Value Type in String format of type CS.
		 * Indicates the temporal relationship of the verification image with respect to the current treatment beam delivery. 
		 * Value shall be DURING_BEAMfor Beams with a Beam Task Type (0074,1022) of VERIFY.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0074, 0x1032, enumVerificationImageTiming,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0074, 0x1032,DicomConstants.DCM_DT_CS, enumVerificationImageTiming);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getVerificationImageTiming()
	{
		Object objValue = getValue(0x0074, 0x1032);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Cumulative Meter set Weight within current Beam at which image acquisition will start.
	 * Required if Verification Image Timing (0074,1032) is DURING_BEAM
	 * @param strStartCumulativeMetersetWeight String value of Slice Thickness.
	 */
	public void setStartCumulativeMetersetWeight(String strStartCumulativeMetersetWeight)
	{
		/* Accept the strStartCumulativeMetersetWeight in String format of type DS.
		 * Cumulative Meter set Weight within current Beam at which image acquisition will start.
		 * Required if Verification Image Timing (0074,1032) is DURING_BEAM
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x300C,0x0008, strStartCumulativeMetersetWeight,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300C,0x0008,DicomConstants.DCM_DT_DS, strStartCumulativeMetersetWeight);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves cumulative Meter set Weight within current Beam at which image acquisition will start.
	 * @return String - value of Slice Thickness.
	 */
	public String getStartCumulativeMetersetWeight()
	{
		Object objValue = getValue(0x300C,0x0008);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Treatment machine Meter set duration over which image is to be acquired, specified in Monitor units (MU) or minutes as defined byPrimary Dosimeter Unit (300A,00B3) 
	 * specified in the Referenced RTPlan Sequence (300C,0002)..
	 * Required if Verification Image Timing (0074,1032) is DURING_BEAM
	 * @param strStartMetersetExposure String value.
	 */
	public void setMetersetExposure(String strMetersetExposure)
	{
		/* Accept the String MetersetExposure in String format of type DS.
		 * Cumulative Meter set Weight within current Beam at which image acquisition will start.
		 * Required if Verification Image Timing (0074,1032) is DURING_BEAM
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x3002,0x0032, strMetersetExposure,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3002,0x0032,DicomConstants.DCM_DT_DS, strMetersetExposure);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves cumulative Meter set Weight within current Beam at which image acquisition will start.
	 * @return String - value of Meter set Exposure.
	 */
	public String getMetersetExposure()
	{
		Object objValue = getValue(0x3002,0x0032);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Cumulative Meterset Weight within current Beam at which imageacquisition will end.
	 * Required if Verification Image Timing (0074,1032) is DURING_BEAM
	 * @param strStartMetersetExposure String value.
	 */
	public void setEndCumulativeMetersetWeight(String strEndCumulativeMetersetWeight)
	{
		/* Accept the String strEndCumulativeMetersetWeight in String format of type DS.
		 * Cumulative Meter set Weight within current Beam at which image acquisition will start.
		 * Required if Verification Image Timing (0074,1032) is DURING_BEAM
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x300C,0x0009, strEndCumulativeMetersetWeight,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300C,0x0009,DicomConstants.DCM_DT_DS, strEndCumulativeMetersetWeight);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves cumulative Meter set Weight within current Beam at which image acquisition will start.
	 * @return String - value of Meter set Exposure.
	 */
	public String getEndCumulativeMetersetWeight()
	{
		Object objValue = getValue(0x300C,0x0009);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Indicates whether the current verification image is a single or double exposure.
	 * @param strDoubleExposureFlag Specifies type of the value encoded in this Item
	 * Enum Value:SINGLE,DOUBLE
	 */
	public void setDoubleExposureFlag(EnumDoubleExposureFlag enumDoubleExposureFlag)
	{
		/* Accept the strDoubleExposureFlag in String format of type CS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Enum Value:SINGLE,DOUBLE
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0074, 0x1034, enumDoubleExposureFlag,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0074, 0x1034,DicomConstants.DCM_DT_CS, enumDoubleExposureFlag);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of Double Exposure Flag value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getDoubleExposureFlag()
	{
		Object objValue = getValue(0x0074, 0x1034);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Indicates the ordering of the open (double exposure) field with respect to the primary (collimated) field in a double exposure
	 * @param strDoubleExposureOrdering Specifies type of the value encoded in this Item
	 * Defined Terms:OPEN_FIRST,OPEN_SECOND
	 */
	public void setDoubleExposureOrdering(EnumDoubleExposureOrdering enumDoubleExposureOrdering)
	{
		/* Accept the strDoubleExposureFlag in String format of type CS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Enum Value:OPEN_FIRST,OPEN_SECOND
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0074, 0x1036, enumDoubleExposureOrdering,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0074, 0x1036,DicomConstants.DCM_DT_CS, enumDoubleExposureOrdering);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of Double Exposure Ordering value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getDoubleExposureOrdering()
	{
		Object objValue = getValue(0x0074, 0x1036);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Double Exposure Meterset in double format of type FD.
	 * @param DoubleExposureMeterset Double value of coordinates
	 */
	public void setDoubleExposureMeterset(double dblDoubleExposureMeterset)
	{
		/* Accept the Double Exposure Meterset in double format of type FD.
		 * If this field is present, an additional exposure with the indicated Meterset is requested
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0074,0x1338, dblDoubleExposureMeterset,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0074,0x1338,DicomConstants.DCM_DT_FD, dblDoubleExposureMeterset);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Minimal value of Chemical Shift Frequency in ppm.
	 * @return double - double value of ChemicalShiftsMinimumIntegrationLimitOnppm
	 */
	public double getDoubleExposureMeterset()
	{
		Object objValue = getValue(0x0074,0x1338);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Double Exposure Field Delta Double value format of type FD.
	 * Offsets of field-defining edges in mm for the double exposure in theIEC BEAM LIMITING DEVICE coordinate system in the IEC order X1,X2, Y1, Y2.
	 * Negative values for X1 and Y1 indicate an enlarged field,negative values for X2 and Y2 indicate a reduced field.
	 * @param Double Exposure Field Delta Double value of coordinates
	 */
	public void setDoubleExposureFieldDelta(double dblDoubleExposureFieldDelta)
	{
		/* Accept Double Exposure Field Delta Double value in double format of type FD.
		 * Offsets of field-defining edges in mm for the double exposure in theIEC BEAM LIMITING DEVICE coordinate system 
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0074,0x133A, dblDoubleExposureFieldDelta,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0074,0x133A,DicomConstants.DCM_DT_FD, dblDoubleExposureFieldDelta);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Minimal value of Chemical Shift Frequency in ppm.
	 * @return double - double value of Double Exposure Field Delta
	 */
	public double getDoubleExposureFieldDelta()
	{
		Object objValue = getValue(0x0074,0x133A);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Position in (x,y,z) coordinates in mm of origin of the IEC X-RAY IMAGE RECEPTOR System in the IEC GANTRY coordinate system.
	 * @param strXRayImageReceptorTranslation String value of Slice Thickness.
	 */
	public void setXRayImageReceptorTranslation(String strXRayImageReceptorTranslation)
	{
		/* Accept the XRayImageReceptorTranslation in String format of type DS.
		 * Position in (x,y,z) coordinates in mm of origin of the IEC X-RAY IMAGE RECEPTOR System in the IEC GANTRY coordinate system.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x3002,0x000D, strXRayImageReceptorTranslation,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3002,0x000D,DicomConstants.DCM_DT_DS, strXRayImageReceptorTranslation);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves XRayImageReceptorTranslation Thickness.
	 * @return String - value of XRayImageReceptorTranslation.
	 */
	public String getXRayImageReceptorTranslation()
	{
		Object objValue = getValue(0x3002,0x000D);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Related Reference RTImage 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 RelatedReferenceRTImageSequence - instance of RelatedReferenceRTImageSequence. 
	 */
	public RelatedReferenceRTImageSequence getRelatedReferenceRTImageSequence()
	{
		RelatedReferenceRTImageSequence objRelatedReferenceRTImageSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0074, 0x1040);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRelatedReferenceRTImageSequence = (RelatedReferenceRTImageSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRelatedReferenceRTImageSequence = (RelatedReferenceRTImageSequence)objDicomV3Factory.createSequence(0x0074, 0x1040, objITransferSyntax, objIDictionaryReader);

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