/*******************************************************************************
 *  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.EnumShapeType;
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;




/**
 * A sequence that specifies one or more fiducials, one item per fiducial.
 */
public class FiducialSequenceItem extends Item  
{
	        

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

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

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

	/**
	 * This constructor creates Fiducial Sequence Item with specified TransferSyntax and specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	FiducialSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	/**
	 * Validates all the attributes in Fiducial Sequence Item  that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			//validation of mandatory elements
			if(!contains(0x0070,0x0310))
				throw new MandatoryMissingException("0070,0310");
			if(!contains(0x0070,0x0306))
				throw new MandatoryMissingException("0070,0306");
			
			//validation of conditional elements
			if((!contains(0x0070,0x0310)) && (!contains(0x0070,0x0311)))
				throw new ConditionFailedException("Element 0070,0311 is required if Identifier 0070,0310 is absent");
			if((contains(0x3006,0x0050)) && (!contains(0x3006,0x0046)))
				throw new ConditionFailedException("Element 3006,0046 is required if Identifier 3006,0050 is present");
			if((!contains(0x3006,0x0050)) && (!contains(0x0070,0x0318)))
				throw new ConditionFailedException("Element 0070,0318 is required if Identifier 3006,0050 is absent");
			
			/*
			 * Condition for Contour Data is remaining to be implemented
			 * This attribute is depending upon parent sequence attribute.So it has been handled in parent sequence.
			 * Refer p.d.f Chapter 3
			 * Page No :- 808
			 */
		}
		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 a fiducial assignment identifier that is unique within this Fiducial Sequence item but may match the fiducial identifier of an equivalent feature in another item.
	 * @param strFiducialIdentifier String value of fiducial assignment identifier 
	 */
	public void setFiducialIdentifier(String strFiducialIdentifier)
	{
		/*Accept the Fiducial Identifier 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(0x0070,0x0310, strFiducialIdentifier,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0310,DicomConstants.DCM_DT_SH, strFiducialIdentifier);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves a fiducial assignment identifier that is unique within this Fiducial Sequence item but may match the fiducial identifier of an equivalent feature in another item.
	 * @return String - value of fiducial assignment identifier 
	 */
	public String getFiducialIdentifier()
	{
		Object objValue = getValue(0x0070,0x0310);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Globally unique identifier for the fiducial instance of this fiducial assignment.
	 * @param strFiducialUID String value of Globally unique identifier
	 */
	public void setFiducialUID(String strFiducialUID)
	{
		/*Accept the Fiducial UID 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(0x0070,0x031A, strFiducialUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x031A,DicomConstants.DCM_DT_UI, strFiducialUID);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Globally unique identifier for the fiducial instance of this fiducial assignment.
	 * @return String - value of Globally unique identifier
	 */
	public String getFiducialUID()
	{
		Object objValue = getValue(0x0070,0x031A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the User description or comments about the fiducial.
	 * @param strFiducialDescription String value of User description
	 */
	public void setFiducialDescription(String strFiducialDescription)
	{
		/*Accept the Fiducial Description 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,0x030F, strFiducialDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x030F,DicomConstants.DCM_DT_ST, strFiducialDescription);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the User description or comments about the fiducial.
	 * @return String - value of User description
	 */
	public String getFiducialDescription()
	{
		Object objValue = getValue(0x0070,0x030F);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Shape Type (0070,0306) defines the geometric interpretation of the Contour 
	 * Data (3006,0050) in terms of EnumShapeType.
	 * @param enumShapeType Enum value for geometric interpretation of the Contour Data
	 */
	public void setShapeType(EnumShapeType enumShapeType)
	{
		/*Accept the Shape 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(0x0070,0x0306, enumShapeType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0306,DicomConstants.DCM_DT_CS, enumShapeType.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Shape Type (0070,0306) defines the geometric interpretation of the Contour 
	 * Data (3006,0050.
	 * @return String - value for geometric interpretation of the Contour Data
	 */
	public String getShapeType()
	{
		Object objValue = getValue(0x0070,0x0306);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Number of points (triplets) in Contour Data
	 * @param strNumberofContourPoints String value of Number of points
	 */
	public void setNumberofContourPoints(String strNumberofContourPoints)
	{
		/*Accept the Number of Contour Points 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,0x0046, strNumberofContourPoints,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0046,DicomConstants.DCM_DT_IS, strNumberofContourPoints);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Number of points (triplets) in Contour Data
	 * @return String - value of Number of points
	 */
	public String getNumberofContourPoints()
	{
		Object objValue = getValue(0x3006,0x0046);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the coordinates of this item's fiducial.
	 * @param arrstrContourData String array of coordinates
	 */
	public void setContourData(String[] arrstrContourData)
	{
		/*Accept the Contour Data in String array 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,0x0050, arrstrContourData,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0050,DicomConstants.DCM_DT_DS, arrstrContourData);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the coordinates of this item's fiducial.
	 * @return []String - value of coordinates
	 */
	public String[] getContourData()
	{
		Object objValue = getValue(0x3006,0x0050);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets the coordinates of this item's fiducial.
	 * @param dblContourUncertaintyRadius String array of coordinates
	 */
	public void setContourUncertaintyRadius(double dblContourUncertaintyRadius)
	{
		/*Accept the Contour Uncertainty Radius in double format of type FD.
		 * 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,0x0312, dblContourUncertaintyRadius,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0312,DicomConstants.DCM_DT_FD, dblContourUncertaintyRadius);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the coordinates of this item's fiducial.
	 * @return double - double value of coordinates
	 */
	public double getContourUncertaintyRadius()
	{
		Object objValue = getValue(0x0070,0x0312);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Retrieves pre-initialized object of Fiducial Identifier 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 FiducialIdentifierCodeSequence - instance of Fiducial Identifier Code Sequence. 
	 */
	public FiducialIdentifierCodeSequence getFiducialIdentifierCodeSequence()
	{
		FiducialIdentifierCodeSequence objFiducialIdentifierCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0070,0x0311);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objFiducialIdentifierCodeSequence = (FiducialIdentifierCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objFiducialIdentifierCodeSequence = (FiducialIdentifierCodeSequence)objDicomV3Factory.createSequence(0x0070,0x0311, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0070,0x0311, objFiducialIdentifierCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0070,0x0311, DicomConstants.DCM_DT_SQ, objFiducialIdentifierCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objFiducialIdentifierCodeSequence;
	}
	
	/**
	 * Retrieves pre-initialized object of Graphic Coordinates Data 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 GraphicCoordinatesDataSequence - instance of Graphic Coordinates Data Sequence. 
	 */
	public GraphicCoordinatesDataSequence getGraphicCoordinatesDataSequence()
	{
		GraphicCoordinatesDataSequence objGraphicCoordinatesDataSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0070,0x0318);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objGraphicCoordinatesDataSequence = (GraphicCoordinatesDataSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objGraphicCoordinatesDataSequence = (GraphicCoordinatesDataSequence)objDicomV3Factory.createSequence(0x0070,0x0318, objITransferSyntax, objIDictionaryReader);

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