/*******************************************************************************
 *  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.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 methods to validate,set and get the attributes in {@link RTROIObservationsSequence}.
 */

public class RTROIObservationsSequenceItem extends Item  
{
	


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


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

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

	/**
	 * Validates all the attributes in {@link RTROIObservationsSequenceItem} 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(0x3006,0x0082))
				throw new MandatoryMissingException("3006,0082");
			if(!contains(0x3006,0x0084))
				throw new MandatoryMissingException("3006,0084");
			if(!contains(0x3006,0x00A4))
				throw new MandatoryMissingException("3006,00A4");
			if(!contains(0x3006,0x00A6))
				throw new MandatoryMissingException("3006,00A6");

		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(TemporalCoordinatesMacro.class.getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets identifies a related ROI Observation.
	 * @param strObservationNumber String value of Observation Number.
	 */
	public void setObservationNumber(String strObservationNumber)
	{
		/*Accept the Observation Number 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(0x3006,0x0082, strObservationNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0082,DicomConstants.DCM_DT_IS, strObservationNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves identifies a related ROI Observation.
	 * @return String - value of Observation Number.
	 */
	public String getObservationNumber()
	{
		Object objValue = getValue(0x3006,0x0082);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets value that uniquely identifies the referenced ROI described in the Structure Set ROI Sequence (3006,0020).
	 * @param strReferencedROINumber String value of Referenced ROI Number.
	 */
	public void setReferencedROINumber(String strReferencedROINumber)
	{
		/*Accept the Referenced ROI Number 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(0x3006,0x0084, strReferencedROINumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0084,DicomConstants.DCM_DT_IS, strReferencedROINumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves  value that uniquely identifies the referenced ROI described in the Structure Set ROI Sequence (3006,0020).
	 * @return String - value of Referenced ROI Number.
	 */
	public String getReferencedROINumber()
	{
		Object objValue = getValue(0x3006,0x0084);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets user-defined label for ROI Observation.
	 * @param strROIObservationLabel String value of ROI Observation Label.
	 */
	public void setROIObservationLabel(String strROIObservationLabel)
	{
		/*Accept the ROI Observation Label in String format of type SH.
		 * 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(0x3006,0x0085, strROIObservationLabel,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0085,DicomConstants.DCM_DT_SH, strROIObservationLabel);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves user-defined label for ROI Observation.
	 * @return String - value of ROI Observation Label.
	 */
	public String getROIObservationLabel()
	{
		Object objValue = getValue(0x3006,0x0085);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets user-defined label for ROI Observation.
	 * @param strROIObservationDescription String value of ROI Observation Description.
	 */
	public void setROIObservationDescription(String strROIObservationDescription)
	{
		/*Accept the ROI Observation Description in String format of type SH.
		 * 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(0x3006,0x0088, strROIObservationDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0088,DicomConstants.DCM_DT_ST, strROIObservationDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves user-defined label for ROI Observation.
	 * @return String - value of ROI Observation Description.
	 */
	public String getROIObservationDescription()
	{
		Object objValue = getValue(0x3006,0x0088);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Retrieves pre-initialized object of RT Related 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 RTRelatedROISequence - instance of RTRelatedROISequence. 
	 */
	public RTRelatedROISequence getRTRelatedROISequence()
	{
		RTRelatedROISequence objRTRelatedROISequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3006,0x0030);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRTRelatedROISequence = (RTRelatedROISequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRTRelatedROISequence = (RTRelatedROISequence)objDicomV3Factory.createSequence(0x3006,0x0030, objITransferSyntax, objIDictionaryReader);

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


	/**
	 * Retrieves pre-initialized object of Segmented Property Category Code 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 SegmentedPropertyCategoryCodeSequence - instance of SegmentedPropertyCategoryCodeSequence. 
	 */
	public SegmentedPropertyCategoryCodeSequence getSegmentedPropertyCategoryCodeSequence()
	{
		SegmentedPropertyCategoryCodeSequence objSegmentedPropertyCategoryCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0062,0x0003);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objSegmentedPropertyCategoryCodeSequence = (SegmentedPropertyCategoryCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objSegmentedPropertyCategoryCodeSequence = (SegmentedPropertyCategoryCodeSequence)objDicomV3Factory.createSequence(0x0062,0x0003, objITransferSyntax, objIDictionaryReader);

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

	
	/**
	 * Retrieves pre-initialized object of RT ROI Identification Code 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 RTROIIdentificationCodeSequence - instance of RTROIIdentificationCodeSequence. 
	 */
	public RTROIIdentificationCodeSequence getRTROIIdentificationCodeSequence()
	{
		RTROIIdentificationCodeSequence objRTROIIdentificationCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3006,0x0086);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRTROIIdentificationCodeSequence = (RTROIIdentificationCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRTROIIdentificationCodeSequence = (RTROIIdentificationCodeSequence)objDicomV3Factory.createSequence(0x3006,0x0086, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Retrieves pre-initialized object of Additional RT ROI Identification Code 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 AdditionalRTROIIdentificationCodeSequence - instance of AdditionalRTROIIdentificationCodeSequence. 
	 */
	public AdditionalRTROIIdentificationCodeSequence getAdditionalRTROIIdentificationCodeSequence()
	{
		AdditionalRTROIIdentificationCodeSequence objAdditionalRTROIIdentificationCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3006,0x00B9);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objAdditionalRTROIIdentificationCodeSequence = (AdditionalRTROIIdentificationCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objAdditionalRTROIIdentificationCodeSequence = (AdditionalRTROIIdentificationCodeSequence)objDicomV3Factory.createSequence(0x3006,0x00B9, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Retrieves pre-initialized object of Related RT ROI Observations 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 RelatedRTROIObservationsSequence - instance of RelatedRTROIObservationsSequence. 
	 */
	public RelatedRTROIObservationsSequence getRelatedRTROIObservationsSequence()
	{
		RelatedRTROIObservationsSequence objRelatedRTROIObservationsSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3006,0x00A0);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRelatedRTROIObservationsSequence = (RelatedRTROIObservationsSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRelatedRTROIObservationsSequence = (RelatedRTROIObservationsSequence)objDicomV3Factory.createSequence(0x3006,0x00A0, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Sets type of ROI.
	 * @param strRTROIInterpretedType String value of RT ROI Interpreted Type.
	 */
	public void setRTROIInterpretedType(String strRTROIInterpretedType)
	{
		/*Accept the RT ROI Interpreted 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(0x3006,0x00A4, strRTROIInterpretedType,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00A4,DicomConstants.DCM_DT_CS, strRTROIInterpretedType);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves type of ROI.
	 * @return String - value of RT ROI Interpreted Type.
	 */
	public String getRTROIInterpretedType()
	{
		Object objValue = getValue(0x3006,0x00A4);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets name of person performing the interpretation.
	 * @param strROIInterpreter String value of ROI Interpreter.
	 */
	public void setROIInterpreter(String strROIInterpreter)
	{
		/*Accept the ROI Interpreter 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(0x3006,0x00A6, strROIInterpreter,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00A6,DicomConstants.DCM_DT_PN, strROIInterpreter);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves name of person performing the interpretation.
	 * @return String - value of ROI Interpreter.
	 */
	public String getROIInterpreter()
	{
		Object objValue = getValue(0x3006,0x00A6);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets user-supplied identifier for ROI material.
	 * @param strMaterialID String value of Material ID.
	 */
	public void setMaterialID(String strMaterialID)
	{
		/*Accept the Material ID 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(0x3006,0x00E1, strMaterialID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00E1,DicomConstants.DCM_DT_SH, strMaterialID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves user-supplied identifier for ROI material.
	 * @return String - value of Material ID.
	 */
	public String getMaterialID()
	{
		Object objValue = getValue(0x3006,0x00E1);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Retrieves pre-initialized object of ROI Physical Properties 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 ROIPhysicalPropertiesSequence - instance of ROIPhysicalPropertiesSequence. 
	 */
	public ROIPhysicalPropertiesSequence getROIPhysicalPropertiesSequence()
	{
		ROIPhysicalPropertiesSequence objROIPhysicalPropertiesSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3006,0x00B0);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objROIPhysicalPropertiesSequence = (ROIPhysicalPropertiesSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objROIPhysicalPropertiesSequence = (ROIPhysicalPropertiesSequence)objDicomV3Factory.createSequence(0x3006,0x00B0, objITransferSyntax, objIDictionaryReader);

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