/*******************************************************************************
 *  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.EnumAlgorithmType;
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;
import cdac.medinfo.sdk.dcm30_04.utils.EnumFeatureList;

/**
* This item class contains the methods to set, get and validate the attribute present in X-Ray 3D Reconstruction Sequence Item  {@link XRay3DReconstructionSequence}.
 *
 */

public class XRay3DReconstructionSequenceItem extends Item
{
	

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

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

	/**
	 * This constructor creates XRay3DReconstructionSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	XRay3DReconstructionSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * Validates all the attributes in XRay3DReconstructionSequenceItem  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(0x0018,0x9524))
				throw new MandatoryMissingException("0018,9524");
			if(!contains(0x0018,0x9525))
				throw new MandatoryMissingException("0018,9525");
			if(!contains(0x0018,0x9526))
				throw new MandatoryMissingException("0018,9526");
			if(!contains(0x0018,0x9527))
				throw new MandatoryMissingException("0018,9527");
			if(!contains(0x0020,0x9518))
				throw new MandatoryMissingException("0020,9518");
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets Reconstruction Description
	 * @param strReconstructionDescription String value of Reconstruction Description
	 */
	public void setReconstructionDescription(String strReconstructionDescription)
	{
		/* Accept the Reconstruction Description 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(0x0018,0x9531, strReconstructionDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9531,DicomConstants.DCM_DT_LO, strReconstructionDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Reconstruction Description
	 * @return String - value of Reconstruction Description
	 */
	public String getReconstructionDescription()
	{
		Object objValue = getValue(0x0018,0x9531);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Application Name
	 * @param strApplicationName String value of Application Name
	 */
	public void setApplicationName(String strApplicationName)
	{
		/* Accept the Application Name 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(0x0018,0x9524, strApplicationName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9524,DicomConstants.DCM_DT_LO, strApplicationName);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Application Name
	 * @return String - value of Application Name
	 */
	public String getApplicationName()
	{
		Object objValue = getValue(0x0018,0x9524);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Application Version
	 * @param strApplicationVersion String value of Application Version
	 */
	public void setApplicationVersion(String strApplicationVersion)
	{
		/* Accept the Application Version 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(0x0018,0x9525, strApplicationVersion,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9525,DicomConstants.DCM_DT_LO, strApplicationVersion);

		addDataElement(objIDataElement);
	}
	/**
	 * Return Application Version
	 * @return String - value of Application Version
	 */
	public String getApplicationVersion()
	{
		Object objValue = getValue(0x0018,0x9525);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Application Manufacturer
	 * @param strApplicationManufacturer String value of Application Manufacturer
	 */
	public void setApplicationManufacturer(String strApplicationManufacturer)
	{
		/* Accept the Application Manufacturer 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(0x0018,0x9526, strApplicationManufacturer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9526,DicomConstants.DCM_DT_LO, strApplicationManufacturer);

		addDataElement(objIDataElement);
	}
	/**
	 * Return Application Manufacturer
	 * @return String - value of Application Manufacturer
	 */
	public String getApplicationManufacturer()
	{
		Object objValue = getValue(0x0018,0x9526);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Type of algorithm used to create the reconstruction.
	 * @param enumAlgorithmType enum value of Algorithm Type
	 */
	public void setAlgorithmType(EnumAlgorithmType enumAlgorithmType)
	{
		/* Accept the Setup Device 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(0x0018,0x9527, enumAlgorithmType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9527,DicomConstants.DCM_DT_CS, enumAlgorithmType.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return Type of algorithm used to create the reconstruction.
	 * @return String - value of Algorithm Type.
	 */
	public String getAlgorithmType()
	{
		Object objValue = getValue(0x0018,0x9527);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Algorithm Description
	 * @param strAlgorithmDescription String value of Algorithm Description
	 */
	public void setAlgorithmDescription(String strAlgorithmDescription)
	{
		/* Accept the Algorithm Description 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(0x0018,0x9528, strAlgorithmDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9528,DicomConstants.DCM_DT_LO, strAlgorithmDescription);

		addDataElement(objIDataElement);
	}
	/**
	 * Return Algorithm Description
	 * @return String - value of Algorithm Description
	 */
	public String getAlgorithmDescription()
	{
		Object objValue = getValue(0x0018,0x9528);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/*
	 * Sets Item number(s) of the X-Ray 3D Acquisition Sequence(0018,9507) that describes the acquisition context(s)contributing to this reconstruction.
	 */
	public void setAcquisitionIndex(short[] arrShAcquisitionIndex)
	{
		/*Accept the Acquisition Index in Array of short format of type US.
		 * 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(0x0020, 0x9518, arrShAcquisitionIndex,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0020, 0x9518,DicomConstants.DCM_DT_US, arrShAcquisitionIndex);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Item number(s) of the X-Ray 3D Acquisition Sequence(0018,9507) that describes the acquisition context(s)contributing to this reconstruction.
	 * @return short Array of Acquisition Index
	 */
	public short[] getAcquisitionIndex()
	{
		Object objValue = getValue(0x0020, 0x9518);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
		
}
