/*******************************************************************************
 *  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.EnumApplicatorType;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
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 attributes in  Applicator Sequence.
 */
public class ApplicatorSequenceItem extends Item  
{
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item with default TransferSyntax 
	 */
	ApplicatorSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item with specified TS.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	ApplicatorSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Validates all the attributes in Applicator Sequence Item 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
		{
			super.validateForModule();
			if(!contains(0x300A,0x0108))
				throw new MandatoryMissingException("300A,0108");
			if(!contains(0x300A,0x0109))
				throw new MandatoryMissingException("300A,0109");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets user or machine supplied identifier for Applicator. Required if Applicator Sequence (300A,0107) is sent.
	 * @param strApplicatorID -String value of Applicator ID.
	 */
	public void setApplicatorID(String strApplicatorID)
	{
		/* Accept the Applicator ID in String format of type PN.
		 * 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(0x300A, 0x0108, strApplicatorID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x0108,DicomConstants.DCM_DT_SH, strApplicatorID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves user or machine supplied identifier for Applicator.
	 * @return String - value of Applicator ID.
	 */
	public String getApplicatorID()
	{
		Object objValue = getValue(0x300A,0x0108);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Type of Applicator. 
	 * Defined Terms:
	 * 1) ELECTRON_SQUARE = square electron applicator.
	 * 2) ELECTRON_RECT = rectangular electron applicator.
	 * 3) ELECTRON_CIRC = circular electron applicator.
	 * 4) ELECTRON_SHORT = short electron applicator.
	 * 5) ELECTRON_OPEN = open (dummy) electron applicator.
	 * 6) PHOTON_SQUARE = square photon applicator
	 * 7) PHOTON_RECT = rectangular photon applicator
	 * 8) PHOTON_CIRC  = circular photon applicator
	 * 9) INTRAOPERATIVE = intraoperative (custom) applicator.
	 * 10) STEREOTACTIC = stereotactic applicator.
	 * @param enumApplicatorType -Enum value of Applicator Type.
	 */
	public void setApplicatorType(EnumApplicatorType enumApplicatorType)
	{
		/* Accept the Applicator 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(0x300A, 0x0109, enumApplicatorType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x0109,DicomConstants.DCM_DT_CS, enumApplicatorType.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Type of Applicator.
	 * @return String value of Applicator Type.
	 */
	public String getApplicatorType()
	{
		Object objValue = getValue(0x300A,0x0109);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets User-defined description for Applicator 
	 * @param strApplicatorDescription -String value of Applicator Description.
	 */
	public void setApplicatorDescription(String strApplicatorDescription)
	{
		/* Accept the Applicator 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(0x300A, 0x010A, strApplicatorDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A, 0x010A,DicomConstants.DCM_DT_LO, strApplicatorDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves User-defined description for Applicator.
	 * @return String - value of Applicator Description.
	 */
	public String getApplicatorDescription()
	{
		Object objValue = getValue(0x300A, 0x010A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets an identifier for the accessory intended to be read by a device such as a bar-code reader.
	 * @param strAccessoryCode -String value of Accessory Code.
	 */
	public void setAccessoryCode(String strAccessoryCode)
	{
		/* Accept the Applicator ID 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(0x300A,0x00F9, strAccessoryCode,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00F9,DicomConstants.DCM_DT_LO, strAccessoryCode);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Accessory Code.
	 * @return String - value of Accessory Code.
	 */
	public String getAccessoryCode()
	{
		Object objValue = getValue(0x300A,0x00F9);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Applicator Geometry 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 ApplicatorGeometrySequence - instance of Applicator Geometry Sequence. 
	 */
	public ApplicatorGeometrySequence getApplicatorGeometrySequence()
	{
		ApplicatorGeometrySequence objApplicatorGeometrySequence = null;
		IDataElement objIDataElement =  getDataForTag(0x300A,0x0431);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objApplicatorGeometrySequence = (ApplicatorGeometrySequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objApplicatorGeometrySequence = (ApplicatorGeometrySequence)objDicomV3Factory.createSequence(0x300A,0x0431, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x300A,0x0431, objApplicatorGeometrySequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x300A,0x0431, DicomConstants.DCM_DT_SQ, objApplicatorGeometrySequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objApplicatorGeometrySequence;
	}
	
	/**
	 * Sets Source to Applicator Mounting Position Distance.
	 * @param fSourceToApplicatorMountingPositionDistance- float value of Source to Applicator Mounting Position Distance.
	 */
	public void setSourceToApplicatorMountingPositionDistance(float fSourceToApplicatorMountingPositionDistance)
	{
		/* Accept the Source to Applicator Mounting Position Distance in float 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(0x300A,0x0436, fSourceToApplicatorMountingPositionDistance,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0436,DicomConstants.DCM_DT_FL, fSourceToApplicatorMountingPositionDistance);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Source to Applicator Mounting Position Distance.
	 * @return float - value of Source to Applicator Mounting Position Distance.
	 */
	public float getSourceToApplicatorMountingPositionDistance()
	{
		Object objValue = getValue(0x300A,0x0436);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}
