/*******************************************************************************
 *  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.EnumInstanceAvailability;
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 set,get and validate the attributes in Referenced SOP Sequence.
 */
public class ReferencedSOPSequenceItem extends SOPInstanceReferenceMacroDependentItem   
{
	

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

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

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

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

	/**
	 * Sets Instance Availability.
	 * The availability of the referenced Instance.
	 * Defined Terms:
	 * 1) ONLINE.
	 * 2) NEARLINE.
	 * 3) OFFLINE
	 * 4) UNAVAILABLE.
	 * @param enumSetupDeviceType enum value of Instance Availability.
	 */
	public void setInstanceAvailability(EnumInstanceAvailability enumSetupDeviceType)
	{
		/* Accept the Instance Availability 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(0x0008,0x0056, enumSetupDeviceType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0056,DicomConstants.DCM_DT_CS, enumSetupDeviceType.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return Instance Availability.
	 * The availability of the referenced Instance. 
	 * @return String - value of Instance Availability.
	 */
	public String getInstanceAvailability()
	{
		Object objValue = getValue(0x0008,0x0056);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Application Entity Title where the Image Storage SOP Instance may be
	 * retrieved via a network based retrieve service.
	 * @param strArrRetrieveAETitle String array of Retrieve AE Title.
	 */
	public void setRetrieveAETitle(String[] strArrRetrieveAETitle)
	{
		/* 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, strArrRetrieveAETitle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0054,DicomConstants.DCM_DT_AE, strArrRetrieveAETitle);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Application Entity Title where the Image Storage SOP Instance may be retrieved
	 * @return String - value of Retrieve AE Title.
	 */
	public String[] getRetrieveAETitle()
	{
		Object objValue = getValue(0x0008,0x0054);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	
	/**
	 * Sets Retrieve Location UID.
	 * Unique identifier of the system where the Composite Object(s) maybe retrieved on the network.
	 * @param strRetrieveLocationUID String value of Retrieve Location UID.
	 */
	public void setRetrieveLocationUID(String strRetrieveLocationUID)
	{
		/*Accept the Retrieve Location 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(0x0040, 0xE011, strRetrieveLocationUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040, 0xE011,DicomConstants.DCM_DT_UI, strRetrieveLocationUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Retrieve Location UID.
	 * Unique identifier of the system where the Composite Object(s) maybe retrieved on the network.
	 * @return String - value of Retrieve Location UID.
	 */
	public String getRetrieveLocationUID()
	{
		Object objValue = getValue(0x0040, 0xE011);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Retrieve URI.
	 * @param strRetrieveURI String value of Retrieve URI.
	 */
	public void setRetrieveURI(String strRetrieveURI)
	{
		/*Accept the Retrieve URI in String format of type UR.
		 * 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,0xE010, strRetrieveURI,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0xE010,DicomConstants.DCM_DT_UR, strRetrieveURI);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Retrieve URI.
	 * @return String - value of Retrieve URI.
	 */
	public String getRetrieveURI()
	{
		Object objValue = getValue(0x0040,0xE010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets URI/URL specifying the location of the referenced instance(s).
	 * @param strRetrieveURL String value of Retrieve URL.
	 */
	public void setRetrieveURL(String strRetrieveURL)
	{
		/*Accept the Retrieve URL in String format of type UR.
		 * 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,0x1190, strRetrieveURL,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0008,0x1190,DicomConstants.DCM_DT_UR, strRetrieveURL);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves URI/URL specifying the location of the referenced instance(s).
	 * @return String - value of Retrieve URL.
	 */
	public String getRetrieveURL()
	{
		Object objValue = getValue(0x0008,0x1190);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * The user or implementation specific human readable identifier that identifies a Storage Media on which this SOP Instance resides
	 * @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 a Storage Media on which this SOP Instance resides
	 * @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;
	}

	/**
	 * Uniquely identifies a Storage Media on which this SOP Instance resides.
	 * @param strStorageMediaFileSetID String value of Storage Media File-Set UID.
	 */
	public void setStorageMediaFileSetUID(String strStorageMediaFileSetID)
	{
		/* 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, strStorageMediaFileSetID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0088, 0x0140,DicomConstants.DCM_DT_UI, strStorageMediaFileSetID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves a Unique ID that identifies a Storage Media on which this SOP Instance resides.
	 * @return String - value of Storage Media File-Set UID.
	 */
	public String getStorageMediaFileSetUID()
	{
		Object objValue = getValue(0x0088,0x0140);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Media Application Profile used for this SOP Instance.
	 * @param strRequestedMediaApplicationProfile String value of Media Application Profile
	 */
	public void setRequestedMediaApplicationProfile(String strRequestedMediaApplicationProfile)
	{
		/* Accept the RequestedMediaApplicationProfile 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(0x2200,0x000C, strRequestedMediaApplicationProfile,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2200,0x000C,DicomConstants.DCM_DT_LO, strRequestedMediaApplicationProfile);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the Media Application Profile used for this SOP Instance.
	 * @return String - value of Media Application Profile
	 */
	public String getRequestedMediaApplicationProfile()
	{
		Object objValue = getValue(0x2200,0x000C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of Icon Image 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 IconImageSequence - instance of IconImageSequence. 
	 */
	public IconImageSequence getIconImageSequence()
	{
		IconImageSequence objIconImageSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0088,0x0200);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objIconImageSequence = (IconImageSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objIconImageSequence = (IconImageSequence)objDicomV3Factory.createSequence(0x0088,0x0200, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Validates all the attributes in ReferencedSOPSequenceItem 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(!super.validateForModule())
				return false;
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
