/*******************************************************************************
 *  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.EnumApplicationSetupType;
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 ApplicationSetupSequence}.
 */
public class ApplicationSetupSequenceItem extends Item  
{
	

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


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

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


	/**
	 * Validates all the attributes in ApplicationSetupSequenceItem that are present in the DataSet.
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			if(!contains(0x300A,0x0232))
				throw new MandatoryMissingException("300A,0232");
			if(!contains(0x300A,0x0234))
				throw new MandatoryMissingException("300A,0234");
			if(!contains(0x300A,0x0250))
				throw new MandatoryMissingException("300A,0250");
			if(!contains(0x300A,0x0280))
				throw new MandatoryMissingException("300A,0280");
			if(contains(0x300C,0x0042))
				getReferencedReferenceImageSequence().validateForModule();
			if(contains(0x300A,0x0280))
				getChannelSequence().validateForModule();
			if(contains(0x300A,0x0260))
				getBrachyAccessoryDeviceSequence().validateForModule();
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Sets Application Setup Type.Uniquely identifies Application Setup Type(300A,0232) in Application Setup 
	 * Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param enumApplicationSetupType -Enum value of Application Setup Type.
	 */
	public void setApplicationSetupType(EnumApplicationSetupType enumApplicationSetupType)
	{
		/*Accept the ApplicationSetupType in Enum 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,0x0232, enumApplicationSetupType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0232,DicomConstants.DCM_DT_CS, enumApplicationSetupType.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Application Setup Type.
	 * @return String - value of Application Setup Type.
	 */
	public String getApplicationSetupType()
	{
		Object objValue = getValue(0x300A,0x0232);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Application Setup Number.Uniquely identifies Application Setup Number(300A,0234) in Application Setup Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strApplicationSetupNumber -String value of Application Setup Number.
	 */
	public void setApplicationSetupNumber(String strApplicationSetupNumber)
	{
		/*Accept the Application Setup 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(0x300A,0x0234, strApplicationSetupNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0234,DicomConstants.DCM_DT_IS, strApplicationSetupNumber);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Application Setup Number.
	 * @return String - value of Application Setup Number.
	 */
	public String strApplicationSetupNumber()
	{
		Object objValue = getValue(0x300A,0x0234);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Application Setup Name.Uniquely identifies Application Setup Name(300A,0236) in Application Setup
	 * Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strApplicationSetupName -String value of Application Setup Name.
	 */
	public void setApplicationSetupName(String strApplicationSetupName)
	{
		/*Accept the Application Setup 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(0x300A,0x0236, strApplicationSetupName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0236,DicomConstants.DCM_DT_LO, strApplicationSetupName);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Application Setup Name.
	 * @return String - value of Application Setup Name.
	 */
	public String getApplicationSetupName()
	{
		Object objValue = getValue(0x300A,0x0236);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Application Setup Manufacturer.Uniquely identifies Application Setup Manufacturer(300A,0238) 
	 * in Application Setup Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strApplicationSetupManufacturer- String value of Application Setup Manufacturer.
	 */
	public void setApplicationSetupManufacturer(String strApplicationSetupManufacturer)
	{
		/*Accept the ApplicationSetupManufacturer 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,0x0238, strApplicationSetupManufacturer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0238,DicomConstants.DCM_DT_LO, strApplicationSetupManufacturer);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Application Setup Manufacturer.
	 * @return String - value of Application Setup Manufacturer.
	 */
	public String getApplicationSetupManufacturer()
	{
		Object objValue = getValue(0x300A,0x0238);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Template Number.Uniquely identifies Template Number(300A,0240) in Application Setup Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strTemplateNumber -String value of Template Number.
	 */
	public void setTemplateNumber(String strTemplateNumber)
	{
		/*Accept the Template 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(0x300A,0x0240, strTemplateNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0240,DicomConstants.DCM_DT_IS, strTemplateNumber);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Template Number.
	 * @return String - value of Template Number.
	 */
	public String getTemplateNumber()
	{
		Object objValue = getValue(0x300A,0x0240);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Template Type.Uniquely identifies Template Type(300A,0242) in Application Setup 
	 * Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strTemplateType- String value of Template Type.
	 */
	public void setTemplateType(String strTemplateType)
	{
		/*Accept the TemplateType 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(0x300A,0x0242, strTemplateType,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0242,DicomConstants.DCM_DT_SH, strTemplateType);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Template Type.
	 * @return String - value of Template Type.
	 */
	public String getTemplateType()
	{
		Object objValue = getValue(0x300A,0x0242);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Template Name.Uniquely identifies Template Name(300A,0244) in Application Setup 
	 * Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strTemplateName -String value of Template Name.
	 */
	public void setTemplateName(String strTemplateName)
	{
		/*Accept the TemplateName 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(0x300A,0x0244, strTemplateName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0244,DicomConstants.DCM_DT_SH, strTemplateName);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Template Name.
	 * @return String - value of Template Name.
	 */
	public String getTemplateName()
	{
		Object objValue = getValue(0x300A,0x0244);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Total Reference Air Kerma.Uniquely identifies Total Reference Air Kerma(300A,0250) in 
	 * Application Setup Sequence(300A,0230) in RT BRACHY APPLICATION SETUPS MODULE of RT Plan IOD.
	 * @param strTotalReferenceAirKerma -String value of Total Reference Air Kerma.
	 */
	public void setTotalReferenceAirKerma(String strTotalReferenceAirKerma)
	{
		/*Accept the TotalReferenceAirKerma 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(0x300A,0x0250, strTotalReferenceAirKerma,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0250,DicomConstants.DCM_DT_DS, strTotalReferenceAirKerma);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Total Reference Air Kerma.
	 * @return String - value of Total Reference Air Kerma.
	 */
	public String getTotalReferenceAirKerma()
	{
		Object objValue = getValue(0x300A,0x0250);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Retrieves pre-initialized object of ReferencedReferenceImageSequence.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 ReferencedReferenceImageSequence - instance of Referenced Reference Image Sequence. 
	 */
	public ReferencedReferenceImageSequence getReferencedReferenceImageSequence()
	{
		ReferencedReferenceImageSequence objReferencedReferenceImageSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x300C,0x0042);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objReferencedReferenceImageSequence = (ReferencedReferenceImageSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objReferencedReferenceImageSequence = (ReferencedReferenceImageSequence)objDicomV3Factory.createSequence(0x300C,0x0042, objITransferSyntax, objIDictionaryReader);

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


	/**
	 * Retrieves pre-initialized object of Brachy Accessory Device 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 BrachyAccessoryDeviceSequence - instance of Brachy Accessory Device Sequence. 
	 */
	public BrachyAccessoryDeviceSequence getBrachyAccessoryDeviceSequence()
	{
		BrachyAccessoryDeviceSequence objBrachyAccessoryDeviceSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x300A,0x0260);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objBrachyAccessoryDeviceSequence = (BrachyAccessoryDeviceSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objBrachyAccessoryDeviceSequence = (BrachyAccessoryDeviceSequence)objDicomV3Factory.createSequence(0x300A,0x0260, objITransferSyntax, objIDictionaryReader);

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


	/**
	 * Retrieves pre-initialized object of Channel 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 ChannelSequence - instance of Channel Sequence. 
	 */
	public ChannelSequence getChannelSequence()
	{
		ChannelSequence objChannelSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x300A,0x0280);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objChannelSequence = (ChannelSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objChannelSequence = (ChannelSequence)objDicomV3Factory.createSequence(0x300A,0x0280, objITransferSyntax, objIDictionaryReader);

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