/*******************************************************************************
 *  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.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumAnchorPointVisibility;
import cdac.medinfo.sdk.dcm30_04.enums.EnumBoundingBoxAnnotationUnits;
import cdac.medinfo.sdk.dcm30_04.enums.EnumBoundingBoxTextHorizontalJustification;
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;




/**
 * Sequence that describes a text annotation
 */
public class TextObjectSequenceItem extends Item  
{
	

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

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

	/**
	 * Validates all the attributes in TextObjectSequenceItem  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(0x0070,0x0006))
				throw new MandatoryMissingException("0070,0006");
			if(((contains(0x0070,0x0010)) || (contains(0x0070,0x0011))) && (!contains(0x0070,0x0003)))
				throw new ConditionFailedException("Value of element 0070,0003 is required if 0070,0010 or 0070,0011 is present") ;
			if((contains(0x0070,0x0014)) && (!contains(0x0070,0x0004)))
				throw new ConditionFailedException("Value of element 0070,0004 is required if 0070,0014 is present") ;
			if((!contains(0x0070,0x0014)) && (!contains(0x0070,0x0010)))
				throw new ConditionFailedException("Value of element 0070,0010 is required if 0070,0014 is not present") ;
			if((!contains(0x0070,0x0014)) && (!contains(0x0070,0x0011)))
				throw new ConditionFailedException("Value of element 0070,0011 is required if 0070,0014 is not present") ;
			if((contains(0x0070,0x0010)) && (!contains(0x0070,0x0012)))
				throw new ConditionFailedException("Value of element 0070,0012 is required if 0070,0010 is present") ;
			if(((!contains(0x0070,0x0010)) && (!contains(0x0070,0x0011))) && (!contains(0x0070,0x0014)))
				throw new ConditionFailedException("Value of element 0070,0014 is required if 0070,0010 and 0070,0011 are not present") ;

		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me); 
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}

		return true;
	}

	/**
	 * Sets Units of measure for the axes of the text bounding box.
	 * @param enumBoundingBoxAnnotationUnits Enum value of Units
	 */
	public void setBoundingBoxAnnotationUnits(EnumBoundingBoxAnnotationUnits enumBoundingBoxAnnotationUnits)
	{
		/*Accept the Bounding Box Annotation 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(0x0070,0x0003, enumBoundingBoxAnnotationUnits.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0003,DicomConstants.DCM_DT_CS, enumBoundingBoxAnnotationUnits.getValue());

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves Units of measure for the axes of the text bounding box.
	 * @return String - value of Units.
	 */
	public String getBoundingBoxAnnotationUnits()
	{
		Object objValue = getValue(0x0070,0x0003);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Units of measure for the axes of the text anchor point annotation
	 * @param enumAnchorPointAnnotationUnits Enum value of Units
	 */
	public void setAnchorPointAnnotationUnits(EnumBoundingBoxAnnotationUnits enumAnchorPointAnnotationUnits)
	{
		/*
		 * Enumerated Values for Anchor Point Annotation Units (0070,0004) are the same as for Bounding Box Annotation Units (0070,0003).
		 */
		/*Accept the Anchor Point Annotation 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(0x0070,0x0004, enumAnchorPointAnnotationUnits.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0004,DicomConstants.DCM_DT_CS, enumAnchorPointAnnotationUnits.getValue());

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves Units of measure for the axes of the text anchor point annotation
	 * @return String - value of Units.
	 */
	public String getAnchorPointAnnotationUnits()
	{
		Object objValue = getValue(0x0070,0x0004);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Text data which is unformatted and whose manner of display within the defined bounding box
	 * or relative to the specified anchor point is implementation dependent.
	 * @param strUnformattedTextValue String value of Text Data
	 */
	public void setUnformattedTextValue(String strUnformattedTextValue)
	{
		/*Accept the Unformatted Text Value 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(0x0070,0x0006, strUnformattedTextValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0006,DicomConstants.DCM_DT_ST, strUnformattedTextValue);

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves Text data which is unformatted and whose manner of display within the defined bounding box
	 * or relative to the specified anchor point is implementation dependent.
	 * @return String - value of Text Data
	 */
	public String getUnformattedTextValue()
	{
		Object objValue = getValue(0x0070,0x0006);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Location of the Top Left Hand Corner of the bounding box in which Unformatted Text Value is to
	 * be displayed, in Bounding Box Annotation Units , given as column\row.
	 * @param arrfBoundingBoxTopLeftHandCorner float array of Hand Corner of the bounding box
	 */
	public void setBoundingBoxTopLeftHandCorner(float[] arrfBoundingBoxTopLeftHandCorner)
	{
		/* 
		 * Accept the Bounding Box Top Left Hand Corner in float array format of type FL.
		 * 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(0x0070,0x0010, arrfBoundingBoxTopLeftHandCorner,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0010,DicomConstants.DCM_DT_FL, arrfBoundingBoxTopLeftHandCorner);

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves the top left pixel in the referenced image to be displayed, given as column\row.
	 * @return float[] - float array of Hand Corner of the bounding box
	 */
	public float[] getBoundingBoxTopLeftHandCorner()
	{
		Object objValue = getValue(0x0070,0x0010);
		if(objValue != null)
			return (float[])objValue;
		return null;
	}

	/**
	 * Sets Location of the Bottom Right Hand Corner of the bounding box in which Unformatted Text Value is to
	 * be displayed, in Bounding Box Annotation Units , given as column\row.
	 * @param arrfBoundingBoxBottomRightHandCorner float array of Hand Corner of the bounding box
	 */
	public void setBoundingBoxBottomRightHandCorner(float[] arrfBoundingBoxBottomRightHandCorner)
	{
		/* 
		 * Accept the Bounding Box Bottom Right Hand Corner in float array format of type FL.
		 * 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(0x0070,0x0011, arrfBoundingBoxBottomRightHandCorner,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0011,DicomConstants.DCM_DT_FL, arrfBoundingBoxBottomRightHandCorner);

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves the Bottom Right pixel in the referenced image to be displayed, given as column\row.
	 * @return float[] - float array of Hand Corner of the bounding box
	 */
	public float[] getBoundingBoxBottomRightHandCorner()
	{
		Object objValue = getValue(0x0070,0x0011);
		if(objValue != null)
			return (float[])objValue;
		return null;
	}

	/**
	 * Sets Location of the text relative to the vertical edges of the bounding box.
	 * @param enumBoundingBoxTextHorizontalJustification Enum value of Location
	 */
	public void setBoundingBoxTextHorizontalJustification(EnumBoundingBoxTextHorizontalJustification enumBoundingBoxTextHorizontalJustification)
	{
		/*Accept the Bounding Box Text Horizontal Justification 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(0x0070,0x0012, enumBoundingBoxTextHorizontalJustification.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0012,DicomConstants.DCM_DT_CS, enumBoundingBoxTextHorizontalJustification.getValue());

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves Location of the text relative to the vertical edges of the bounding box.
	 * @return String - value of Location
	 */
	public String getBoundingBoxTextHorizontalJustification()
	{
		Object objValue = getValue(0x0070,0x0012);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Location of a point in the image or Specified Displayed Area to which the Unformatted Text Value is related.
	 * @param arrfAnchorPoint float array of Location of a point
	 */
	public void setAnchorPoint(float[] arrfAnchorPoint)
	{
		/* 
		 * Accept the Anchor Point in float array format of type FL.
		 * 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(0x0070,0x0014, arrfAnchorPoint,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0014,DicomConstants.DCM_DT_FL, arrfAnchorPoint);

		addDataElement(objIDataElement);

	}

	/**
	 * Retrieves Location of a point in the image or Specified Displayed Area to which the Unformatted Text Value is related.
	 * @return float[] - float array of Location of a point
	 */
	public float[] getAnchorPoint()
	{
		Object objValue = getValue(0x0070,0x0014);
		if(objValue != null)
			return (float[])objValue;
		return null;
	}

	/**
	 * Sets Flag to indicate whether or not a visible indication of the relationship between the text and the anchor point is to be displayed.
	 * @param enumAnchorPointVisibility Enum value of Flag
	 */
	public void setAnchorPointVisibility(EnumAnchorPointVisibility enumAnchorPointVisibility)
	{
		/*Accept the Anchor Point Visibility 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(0x0070,0x0015, enumAnchorPointVisibility.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0015,DicomConstants.DCM_DT_CS, enumAnchorPointVisibility.getValue());

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves Flag to indicate whether or not a visible indication of the relationship between the text and the anchor point is to be displayed.
	 * @return String - value of Flag.
	 */
	public String getAnchorPointVisibility()
	{
		Object objValue = getValue(0x0070,0x0015);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
