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




/**
 * A Sequence of Items where each Item includes the Attributes of one Series to
 * which the Presentation applies.
 */
public class ReferencedSeriesSequenceItem extends Item  
{
	

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax. 
	 */
	ReferencedSeriesSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax. 
	 */
	ReferencedSeriesSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Sets unique identifier of a Series that is part of the Study to which the Presentation
	 * applies, defined by Study Instance UID (0020,000D).
	 * @param strSeriesInstanceUID String value of Series Instance UID.
	 */
	public void setSeriesInstanceUID(String strSeriesInstanceUID)
	{
		/*Accept the Series Instance UID in String format of type UI.
		 * 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, 0x000E, strSeriesInstanceUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0020, 0x000E,DicomConstants.DCM_DT_UI, strSeriesInstanceUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves unique identifier of a Series that is part of the Study to which the Presentation
	 * applies, defined by Study Instance UID (0020,000D).
	 * @return String - value of Series Instance UID.
	 */
	public String getSeriesInstanceUID()
	{
		Object objValue = getValue(0x0020,0x000E);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets title of the DICOM Application Entity where the Composite Object(s) may be retrieved on the network.
	 * @param strRetrieveAETitle String value of Retrieve AE Title.
	 */
	public void setRetrieveAETitle(String strRetrieveAETitle)
	{
		/*Accept the Retrieve AE Title in String format of type AE.
		 * 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(0x0008, 0x0054, strRetrieveAETitle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0054,DicomConstants.DCM_DT_AE, strRetrieveAETitle);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves title of the DICOM Application Entity where the Composite Object(s) may be retrieved on the network.
	 * @return String - value of Retrieve AE Title.
	 */
	public String getRetrieveAETitle()
	{
		Object objValue = getValue(0x0008,0x0054);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the user or implementation specific human readable identifier that identifies the Storage
	 * Media on which the Composite Object (s) reside.
	 * @param strStorageMediaFileSetID String value of Storage Media File-Set ID.
	 */
	public void setStorageMediaFileSetID(String strStorageMediaFileSetID)
	{
		/*Accept the Storage Media File-Set ID 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(0x0088, 0x0130, strStorageMediaFileSetID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0088, 0x0130,DicomConstants.DCM_DT_SH, strStorageMediaFileSetID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the user or implementation specific human readable identifier that identifies the Storage
	 * Media on which the Composite Object (s) reside.
	 * @return String - value of Storage Media File-Set ID.
	 */
	public String getStorageMediaFileSetID()
	{
		Object objValue = getValue(0x0088,0x0130);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets uniquely identifies the Storage Media on which the Composite Object (s) reside.
	 * @param strStorageMediaFileSetUID String value of Storage Media File-Set UID. 
	 */
	public void setStorageMediaFileSetUID(String strStorageMediaFileSetUID)
	{
		/*Accept the Storage Media File-Set UID in String format of type UI.
		 * 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(0x0088, 0x0140, strStorageMediaFileSetUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0088, 0x0140,DicomConstants.DCM_DT_UI, strStorageMediaFileSetUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves pre-initialized object of Referenced SOP 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 ReferencedSOPSequence - instance of ReferencedSOPSequence. 
	 */
	public ReferencedSOPSequence getReferencedSOPSequence()
	{
		ReferencedSOPSequence objReferencedSOPSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0008,0x1199);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objReferencedSOPSequence = (ReferencedSOPSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objReferencedSOPSequence = (ReferencedSOPSequence)objDicomV3Factory.createSequence(0x0008,0x1199, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Retrieves uniquely identifies the Storage Media on which the Composite Object (s) reside.
	 * @return String - value of torage Media File-Set UID. 
	 */
	public String getStorageMediaFileSetUID()
	{
		Object objValue = getValue(0x0088,0x0140);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in ReferencedSeriesSequenceItem 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(0x0020,0x000E))
				throw new MandatoryMissingException("0020,000E");
			if(!contains(0x0008,0x1140))	
				throw new MandatoryMissingException("0008,1140");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
