/*******************************************************************************
 *  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.EnumTestImageValidation;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
/**
* This item class contains methods to validate, set and get the attributes of Referenced Image Sequence.
 */

public class ReferencedImageSequenceItem extends ImageSOPInstanceReferenceMacroDependentItem  
{
	private static final long serialVersionUID = 1L;

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

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates Item with specified transferSyntax
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	ReferencedImageSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates 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 
	 */
	ReferencedImageSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in Referenced Image 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
	{
		return true;
	}
	
	/**
	 * Sets Container Identifier.
	 * The identifier for the container that contains the specimen(s) imaged.
	 * @param strContainerIdentifier String value of Container Identifier.
	 */
	public void setContainerIdentifier(String strContainerIdentifier)
	{
		/* Accept the Container Identifier 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(0x0040,0x0512, strContainerIdentifier,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x0512,DicomConstants.DCM_DT_LO, strContainerIdentifier);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Container Identifier.
	 * The identifier for the container that contains the specimen(s) imaged.
	 * @return String - value of Container Identifier.
	 */
	public String getContainerIdentifier()
	{
		Object objValue = getValue(0x0040,0x0512);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	
	/**
	 * Retrieves pre-initialized object of Specimen Description 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 SpecimenDescriptionSequence - instance of Specimen Description Sequence 
	 */
	public SpecimenDescriptionSequence getSpecimenDescriptionSequence()
	{
		SpecimenDescriptionSequence objSpecimenDescriptionSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040,0x0560);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objSpecimenDescriptionSequence = (SpecimenDescriptionSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objSpecimenDescriptionSequence = (SpecimenDescriptionSequence)objDicomV3Factory.createSequence(0x0040,0x0560, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040,0x0560, objSpecimenDescriptionSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040,0x0560, DicomConstants.DCM_DT_SQ, objSpecimenDescriptionSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objSpecimenDescriptionSequence;
	}
	/**
	 * Retrieves pre-initialized object of Purpose Of Reference 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 PurposeOfReferenceCodeSequence - instance of PurposeOfReferenceCodeSequence. 
	 */
	public PurposeOfReferenceCodeSequence getPurposeOfReferenceCodeSequence()
	{
		PurposeOfReferenceCodeSequence objPurposeOfReferenceCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0xA170);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objPurposeOfReferenceCodeSequence = (PurposeOfReferenceCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objPurposeOfReferenceCodeSequence = (PurposeOfReferenceCodeSequence)objDicomV3Factory.createSequence(0x0040, 0xA170, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0xA170, objPurposeOfReferenceCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0xA170, DicomConstants.DCM_DT_SQ, objPurposeOfReferenceCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objPurposeOfReferenceCodeSequence;
	}
	/**
	 * Sets whether the test image was validated as matching the characteristics of the display system (e.g., resolution and bit depth
	 * @param enumTestImageValidation Enum value ofTest Image Validation
	 */
	public void setTestImageValidation(EnumTestImageValidation enumTestImageValidation)
	{
		/*Accept theTest Image Validation 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(0x0028,0x702B, enumTestImageValidation.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x702B,DicomConstants.DCM_DT_CS, enumTestImageValidation.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves whether the test image was validated as matching the characteristics of the display system (e.g., resolution and bit depth
	 * @return String - value ofTest Image Validation
	 */
	public String getTestImageValidation()
	{
		Object objValue = getValue(0x0028,0x702B);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
