/*******************************************************************************
 *  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.EnumROIGenerationAlgorithm;
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.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 StructureSetROISequence}.
 */
public class StructureSetROISequenceItem extends Item   
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	StructureSetROISequenceItem()
	{
		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
	 */
	StructureSetROISequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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



	/**
	 * Validates all the attributes in StructureSetROISequenceItem 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(0x3006,0x0022))
				throw new MandatoryMissingException("3006,0022") ;
			if(!contains(0x3006,0x0024))
				throw new MandatoryMissingException("3006,0024") ;
			if(!contains(0x3006,0x0026))
				throw new MandatoryMissingException("3006,0026") ;
			if(!contains(0x3006,0x0036))
				throw new MandatoryMissingException("3006,0036") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}

	/**
	 * Sets ROI Number .Uniquely identifies ROI Number(3006,0022) in Structure Set ROI Sequence (3006,0020) in Structure Set Module of RT Structure Set IOD.
	 * @param strROINumber String value of ROI Number.
	 */
	public void setROINumber(String strROINumber)
	{
		/*Accept the Value Type 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,0x0022, strROINumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0022,DicomConstants.DCM_DT_IS, strROINumber);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves ROI Number.
	 * @return String - value of ROI Number.
	 */
	public String getROINumber()
	{
		Object objValue = getValue(0x3006,0x0022);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Referenced Frame of Reference UID .Uniquely identifies Referenced Frame of Reference UID(3006,0024) in Structure Set ROI Sequence (3006,0020) in Structure Set Module of RT Structure Set IOD.
	 * @param strReferencedFrameofReferenceUID String value of Referenced Frame of Reference UID.
	 */
	public void setReferencedFrameofReferenceUID(String strReferencedFrameofReferenceUID)
	{
		/*Accept the Value Type in String format of type UI.
		 * 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,0x0024, strReferencedFrameofReferenceUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0024,DicomConstants.DCM_DT_UI, strReferencedFrameofReferenceUID);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Referenced Frame of Reference UID.
	 * @return String - value of Referenced Frame of Reference UID.
	 */
	public String getReferencedFrameofReferenceUID()
	{
		Object objValue = getValue(0x3006,0x0024);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets ROI Name .Uniquely identifies ROI Name(3006,0026) in Structure Set ROI Sequence (3006,0020) in Structure Set Module of RT Structure Set IOD.
	 * @param strROIName String value of ROI Name.
	 */
	public void setROIName(String strROIName)
	{
		/*Accept the Value Type 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(0x3006,0x0026, strROIName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0026,DicomConstants.DCM_DT_LO, strROIName);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves ROI Name.
	 * @return String value of ROI Name.
	 */
	public String getROIName()
	{
		Object objValue = getValue(0x3006,0x0026);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets ROI Description .Uniquely identifies ROI Description(3006,0028) in Structure Set ROI Sequence (3006,0020) in Structure Set Module of RT Structure Set IOD.
	 * @param strROIDescription String value of ROI Description.
	 */
	public void setROIDescription(String strROIDescription)
	{
		/*Accept the Value Type in String format of type ST.
		 * 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,0x0028, strROIDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0028,DicomConstants.DCM_DT_ST, strROIDescription);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves ROI Description.
	 * @return String - value of ROI Description.
	 */
	public String getROIDescription()
	{
		Object objValue = getValue(0x3006,0x0028);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets ROI Volume .Uniquely identifies ROI Volume(3006,002C) in Structure Set ROI Sequence (3006,0020) in Structure Set Module of RT Structure Set IOD.
	 * @param strROIVolume String value of ROI Volume.
	 */
	public void setROIVolume(String strROIVolume)
	{
		/*Accept the Value Type 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(0x3006,0x002C, strROIVolume,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x002C,DicomConstants.DCM_DT_DS, strROIVolume);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves ROI Volume.
	 * @return String - value of ROI Volume.
	 */
	public String getROIVolume()
	{
		Object objValue = getValue(0x3006,0x002C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Type of ROI Generation Algorithm.
	 * @param enumROIGenerationAlgorithm Enum value of ROI Generation Algorithm.
	 */
	public void setROIGenerationAlgorithm(EnumROIGenerationAlgorithm enumROIGenerationAlgorithm)
	{
		/*Accept the Value 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,0x0036, enumROIGenerationAlgorithm.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0036,DicomConstants.DCM_DT_CS, enumROIGenerationAlgorithm.getValue());

		addDataElement(objIDataElement);

	}

	/**
	 * Retrieves ROI Generation Algorithm.
	 * @return String - value of ROI Generation Algorithm.
	 */
	public String getROIGenerationAlgorithm()
	{
		Object objValue = getValue(0x3006,0x0036);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets ROI Generation Description.Uniquely identifies ROI Generation Description(3006,0038) in Structure Set ROI Sequence (3006,0020) in Structure Set Module of RT Structure Set IOD.
	 * @param strROIGenerationDescription String value of ROI Generation Description.
	 */
	public void setROIGenerationDescription(String strROIGenerationDescription)
	{
		/*Accept the Value Type 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(0x3006,0x0038,strROIGenerationDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0038,DicomConstants.DCM_DT_LO,strROIGenerationDescription);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves ROI Generation Description.
	 * @return String - value of ROI Generation Description.
	 */
	public String getROIGenerationDescription()
	{
		Object objValue = getValue(0x3006,0x0038);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Derivation 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 DerivationCodeSequence - instance of Derivation Code Sequence. 
	 */
	public DerivationCodeSequence getDerivationCodeSequence()
	{
		DerivationCodeSequence objDerivationCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0008,0x9215);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objDerivationCodeSequence = (DerivationCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objDerivationCodeSequence = (DerivationCodeSequence)objDicomV3Factory.createSequence(0x0008,0x9215, objITransferSyntax, objIDictionaryReader);

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