/*******************************************************************************
 *  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.EnumDVHType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumDVHVolumeUnits;
import cdac.medinfo.sdk.dcm30_04.enums.EnumDoseType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumDoseUnits;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
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 the methods to set,get and validate the attribute present in DVH Sequence.
 */

public class DVHSequenceItem extends Item  
{
	

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


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

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

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


	/**
	 * Retrieves pre-initialized object of DVH Referenced ROI 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 DVHReferencedROISequence - instance of DVH Referenced ROI Sequence. 
	 */
	public DVHReferencedROISequence getDVHReferencedROISequence()
	{
		DVHReferencedROISequence objDVHReferencedROISequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3004,0x0060);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objDVHReferencedROISequence = (DVHReferencedROISequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objDVHReferencedROISequence = (DVHReferencedROISequence)objDicomV3Factory.createSequence(0x3004,0x0060, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Sets Type of DVH.
	 * Enumerated Values:
	 * DIFFERENTIAL = differential dose-volume histogram,
	 * CUMULATIVE = cumulative dose-volume histogram,
	 * NATURAL = natural dose volume histogram.
	 * @param enumDVHType Enum value of DVH Type
	 */
	public void setDVHType(EnumDVHType enumDVHType)
	{
		/*Accept the DVH  Type 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(0x3004,0x0001, enumDVHType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0001,DicomConstants.DCM_DT_CS, enumDVHType.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Type of DVH 
	 * @return String - value of DVH  Type.
	 */
	public String getDVHType()
	{
		Object objValue = getValue(0x3004,0x0001);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Dose axis units.
	 * Enumerated Values:
	 * GY = Gray,
	 * RELATIVE = dose relative to reference value specified in DVH Normalization Dose Value (3004,0042).
	 * @param enumDoseUnits Enum value of Dose Units
	 */
	public void setDoseUnits(EnumDoseUnits enumDoseUnits)
	{
		/*Accept the Dose Units 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(0x3004,0x0002, enumDoseUnits.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0002,DicomConstants.DCM_DT_CS, enumDoseUnits.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Dose axis units.
	 * @return String - value of Dose Units.
	 */
	public String getDoseUnits()
	{
		Object objValue = getValue(0x3004,0x0002);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Dose Type.
	 * Defined Terms:
	 * PHYSICAL = physical dose
	 * EFFECTIVE = physical dose after correction for biological effect using userdefined modeling technique
	 * ERROR = difference between desired and planned dose
	 * @param enumDoseType Enum value of Dose Type
	 */
	public void setDoseType(EnumDoseType enumDoseType)
	{
		/*Accept the Dose Type 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(0x3004,0x0004, enumDoseType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0004,DicomConstants.DCM_DT_CS, enumDoseType.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Dose Type.
	 * @return String - value of Dose Type.
	 */
	public String getDoseType()
	{
		Object objValue = getValue(0x3004,0x0004);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Scaling factor that when multiplied by the dose bin widths found in DVH Data (3004,0058), yields dose bin widths in the
	 * dose units as specified by Dose Units 3004,0002.
	 * @param strDVHDoseScaling String value of DVH Dose Scaling.
	 */
	public void setDVHDoseScaling(String strDVHDoseScaling)
	{
		/*Accept the DVH Dose Scaling 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(0x3004,0x0052, strDVHDoseScaling,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0052,DicomConstants.DCM_DT_DS, strDVHDoseScaling);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves DVH Dose Scaling factor.
	 * @return String - value of DVH Dose Scaling.
	 */
	public String getDVHDoseScaling()
	{
		Object objValue = getValue(0x3004,0x0052);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Volume axis units.
	 * Defined Terms:
	 * CM3 = cubic centimeters,
	 * PERCENT = percent,
	 * PER_U= volume per u with u(dose)=dose-3/2
	 * @param enumDVHVolumeUnits Enum value of DVH Volume Units
	 */
	public void setDVHVolumeUnits(EnumDVHVolumeUnits enumDVHVolumeUnits)
	{
		/*Accept the DVH Volume Units 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(0x3004,0x0054, enumDVHVolumeUnits.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0054,DicomConstants.DCM_DT_CS, enumDVHVolumeUnits.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Volume axis units.
	 * @return String - value of DVH Volume Units.
	 */
	public String getDVHVolumeUnits()
	{
		Object objValue = getValue(0x3004,0x0054);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Number of bins n used to store DVH Data (3004,0058).
	 * @param strDVHNumberOfBins String value of DVH Number of Bins.
	 */
	public void setDVHNumberOfBins(String strDVHNumberOfBins)
	{
		/*Accept the DVH Number Of Bins 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(0x3004,0x0056, strDVHNumberOfBins,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0056,DicomConstants.DCM_DT_IS, strDVHNumberOfBins);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of bins n used to store DVH Data (3004,0058).
	 * @return String - value of DVH Number of Bins.
	 */
	public String getDVHNumberOfBins()
	{
		Object objValue = getValue(0x3004,0x0056);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets A data stream describing the dose bin widths Dn and associated volumes Vn in DVH Volume Units (3004,0054) in the order D1V1, D2V2, ... DnVn.
	 * @param arrDVHData String array value of DVH Data.
	 */
	public void setDVHData(String arrDVHData[])
	{
		/*Accept the DVH Data 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(0x3004,0x0058, arrDVHData,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0058,DicomConstants.DCM_DT_DS, arrDVHData);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves  a data stream describing the dose bin widths Dn and associated volumes Vn in DVH Volume Units (3004,0054) in the order D1V1, D2V2, ... DnVn.
	 * @return String - value of DVH Data.
	 */
	public String[] getDVHData()
	{
		Object objValue = getValue(0x3004,0x0058);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}


	/**
	 * Sets Minimum calculated dose to ROI(s) described by DVH Referenced ROI Sequence (3004,0060).
	 * @param strDVHMinimumDose String value of DVH Minimum Dose.
	 */
	public void setDVHMinimumDose(String strDVHMinimumDose)
	{
		/*Accept the DVH Minimum Dose 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(0x3004,0x0070, strDVHMinimumDose,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0070,DicomConstants.DCM_DT_DS, strDVHMinimumDose);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Minimum calculated dose to ROI(s) described by DVH Referenced ROI Sequence (3004,0060).
	 * @return String - value of DVH Minimum Dose.
	 */
	public String getDVHMinimumDose()
	{
		Object objValue = getValue(0x3004,0x0070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Maximum calculated dose to ROI(s) described by DVH Referenced ROI Sequence (3004,0060).
	 * @param strDVHMaximumDose String value of DVH Maximum Dose.
	 */
	public void setDVHMaximumDose(String strDVHMaximumDose)
	{
		/*Accept the DVH Maximum Dose 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(0x3004,0x0072, strDVHMaximumDose,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0072,DicomConstants.DCM_DT_DS, strDVHMaximumDose);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Maximum calculated dose to ROI(s) described by DVH Referenced ROI Sequence (3004,0060).
	 * @return String - value of DVH Maximum Dose.
	 */
	public String getDVHMaximumDose()
	{
		Object objValue = getValue(0x3004,0x0072);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Mean calculated dose to ROI(s) described by DVH Referenced ROI Sequence (3004,0060).
	 * @param strDVHMeanDose String value of DVH Mean Dose.
	 */
	public void setDVHMeanDose(String strDVHMeanDose)
	{
		/*Accept the DVH Mean Dose 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(0x3004,0x0074, strDVHMeanDose,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3004,0x0074,DicomConstants.DCM_DT_DS, strDVHMeanDose);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Mean calculated dose to ROI(s) described by DVH Referenced ROI Sequence (3004,0060).
	 * @return String - value of  DVH Mean Dose.
	 */
	public String getDVHMeanDose()
	{
		Object objValue = getValue(0x3004,0x0074);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Validates all the attributes in DVHSequenceItem  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(0x3004,0x0060))
				throw new MandatoryMissingException("3004,0060");
			else
				getDVHReferencedROISequence().validateForModule();
			if(!contains(0x3004,0x0001))
				throw new MandatoryMissingException("3004,0001");
			if(!contains(0x3004,0x0002))
				throw new MandatoryMissingException("3004,0002");
			if(!contains(0x3004,0x0004))
				throw new MandatoryMissingException("3004,0004");
			if(!contains(0x3004,0x0052))
				throw new MandatoryMissingException("3004,0052");
			if(!contains(0x3004,0x0054))
				throw new MandatoryMissingException("3004,0054");
			if(!contains(0x3004,0x0056))
				throw new MandatoryMissingException("3004,0056");
			if(!contains(0x3004,0x0058))
				throw new MandatoryMissingException("3004,0058");
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
