/*******************************************************************************
 *  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.EnumContourGeometricType;
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 ContourSequence}.
 */

public class ContourSequenceItem extends Item  
{
	


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


	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	ContourSequenceItem()
	{
		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.
	 */
	ContourSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates ContourSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax.
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used .
	 */
	ContourSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	
	/**
	 * Validates all the attributes in ContourSequenceItem according the rules defined by the Contour Sequence.
	 * @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,0x0042))
				throw new MandatoryMissingException("3006,0042");
			if(!contains(0x3006,0x0046))
				throw new MandatoryMissingException("3006,0046");
			if(!contains(0x3006,0x0050))
				throw new MandatoryMissingException("3006,0050");
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Sets identification number of the contour.
	 * @param strContourNumber String value of Contour Number.
	 */
	public void setContourNumber(String strContourNumber)
	{
		/*Accept the Contour 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,0x0048, strContourNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0048,DicomConstants.DCM_DT_IS, strContourNumber);

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves identification number of the contour.
	 * @return String - value of Contour Number.
	 */
	public String getContourNumber()
	{
		Object objValue = getValue(0x3006,0x0048);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets list of Contour Number (3006,0048) defining lower-numbered contour(s) to which the current contour is connected.
	 * @param arrStrAttachedContours String array value of Attached Contours .
	 */
	public void setAttachedContours(String[] arrStrAttachedContours)
	{
		/* Accept the Attached Contours 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, 0x0049, arrStrAttachedContours,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006, 0x0049,DicomConstants.DCM_DT_IS, arrStrAttachedContours);
		
		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves list of Contour Number (3006,0048) defining lower-numbered contour(s) to which the current contour is connected.
	 * @return String - array value of Attached Contours .
	 */
	public String[] getAttachedContours()
	{
		Object objValue = getValue(0x3006, 0x0049);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets geometric type of contour.
	 * @param enumContourGeometricType enum value of Contour Geometric Type.
	 */
	public void setContourGeometricType(EnumContourGeometricType enumContourGeometricType)
	{
		/*Accept the Contour Geometric 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,0x0042, enumContourGeometricType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0042,DicomConstants.DCM_DT_CS, enumContourGeometricType.getValue());

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves geometric type of contour.
	 * @return String - value of Contour Geometric Type.
	 */
	public String getContourGeometricType()
	{
		Object objValue = getValue(0x3006,0x0042);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Contour Slab Thickness which specifies the thickness of a slab.
	 * @param strContourSlabThickness String value of Contour Slab Thickness.
	 */
	public void setContourSlabThickness(String strContourSlabThickness)
	{
		/*Accept the Contour Slab Thickness 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,0x0044, strContourSlabThickness,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x0044,DicomConstants.DCM_DT_DS, strContourSlabThickness);

		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves Contour Slab Thickness which specifies the thickness of a slab.
	 * @return String - value of Contour Slab Thickness.
	 */
	public String getContourSlabThickness()
	{
		Object objValue = getValue(0x3006,0x0044);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets a Vector (x,y,z) in the the patient based coordinate system which is normal to plane of Contour Data
	 * (3006,0050), describing direction and magnitude of the offset (in mm) of each point of the central plane 
	 * of a contour slab from the corresponding original point of Contour Data (3006,0050).
	 * @param arrStrContourOffsetVector String array value of Contour Offset Vector.
	 */
	public void setContourOffsetVector(String[] arrStrContourOffsetVector)
	{
		/* Accept the Contour Offset Vector 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, 0x0045, arrStrContourOffsetVector,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006, 0x0045,DicomConstants.DCM_DT_DS, arrStrContourOffsetVector);
		
		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves a Vector (x,y,z) in the the patient based coordinate system which is normal to plane of Contour Data
	 * (3006,0050), describing direction and magnitude of the offset (in mm) of each point of the central plane 
	 * of a contour slab from the corresponding original point of Contour Data (3006,0050).
	 * @return String - array value of Contour Offset Vector.
	 */
	public String[] getContourOffsetVector()
	{
		Object objValue = getValue(0x3006, 0x0045);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets number of points (triplets) in Contour Data (3006,0050).
	 * @param strNumberOfContourPoints String value of Number of Contour Points.
	 */
	public void setNumberOfContourPoints(String strNumberOfContourPoints)
	{
		/*Accept the NumberOfContourPoints 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 (3006,0050).
	 * @return String - value of Number of Contour Points.
	 */
	public String getNumberOfContourPoints()
	{
		Object objValue = getValue(0x3006,0x0046);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Sequence of (x,y,z) triplets defining a contour in the patient based coordinate system.
	 * @param arrStrContourData String array value of Contour Data.
	 */
	public void setContourData(String[] arrStrContourData)
	{
		/* Accept the Contour 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(0x3006, 0x0050, arrStrContourData,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006, 0x0050,DicomConstants.DCM_DT_DS, arrStrContourData);
		
		addDataElement(objIDataElement);
	}
	
	/**
	 * Retrieves Sequence of (x,y,z) triplets defining a contour in the patient based coordinate system.
	 * @return String - array value of Contour Data.
	 */
	public String[] getContourData()
	{
		Object objValue = getValue(0x3006, 0x0050);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	
	/**
	 * Retrieves pre-initialized object of Contour Image 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 ContourImageSequence - instance of ContourImageSequence. 
	 */
	public ContourImageSequence getContourImageSequence()
	{
		ContourImageSequence objContourImageSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x3006,0x0016);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objContourImageSequence = (ContourImageSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objContourImageSequence = (ContourImageSequence)objDicomV3Factory.createSequence(0x3006,0x0016, objITransferSyntax, objIDictionaryReader);

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