package com.tandbergtv.watchpoint.pmm.title;

import java.util.Collection;
import java.util.Map;

import org.w3c.dom.Document;

import com.tandbergtv.metadatamanager.exception.MetadataException;
import com.tandbergtv.metadatamanager.exception.SearchException;
import com.tandbergtv.metadatamanager.exception.TranslationException;
import com.tandbergtv.metadatamanager.model.Asset;
import com.tandbergtv.metadatamanager.model.Field;
import com.tandbergtv.watchpoint.pmm.core.PMMException;
import com.tandbergtv.watchpoint.pmm.core.TitleValidationException;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.workflow.core.service.Service;

public interface ITitleService extends Service {

	/**
	 * Root asset whose corresponding title needs to be looked up
	 * 
	 * @param rootAsset
	 * @return Title with asset id the same as the rootAsset id that was passed
	 *         in. If not found throws an exception
	 */
	Title getTitle(Asset rootAsset);
	
	/**
	 * Gets the title for the given metadata in the specified spec. 
	 * 
	 * @param metadata
	 * @param spec
	 * @return
	 */
	Collection<Title> getTitle(Document metadata);

	/**
	 * Gets fields from all levels for this title.
	 *  
	 * @param id
	 * @return
	 */
	Collection<Field> getAllDecendantFields(long id);

	/**
	 * Finds assets that refer to the given fileName in their metadata properties.
	 * Returns asset at any level (need not be the root asset).
	 */
	Collection<Asset> findAssetsWithFileName(String fileName);

	/**
	 * Creates a new title.
	 */
	public void create(Title title,
			String sourceComponentName, String sourceEntityName, String sourceId)
			throws TitleValidationException;

	/**
	 * Updates the title to the database.
	 */
	public void update(Title title,
			String sourceComponentName, String sourceEntityName, String sourceId)
			throws TitleValidationException;
	
	/**
	 * Updates the title metadata to the database.
	 */
	public Title updateMetadata(Title title,
			String sourceComponentName, String sourceEntityName, String sourceId)
			throws TitleValidationException;

	/**
	 * Saves the given title as draft (Status: NEW) 
	 */
	public void saveAsDraft(Title title);

	/**
	 * Nationalizes and saves the given title.
	 */
	public void nationalizeAndSave(Long titleId,
			String sourceComponentName, String sourceEntityName, String sourceId);

	/**
	 * Rolls back the title with the given id to the given version.
	 */
	Title rollback(Long titleId, String version) throws MetadataException,
			SearchException, TranslationException;

	/**
	 * Evaluates the value of the given job parameter menu option from the given title.
	 *  
	 * @param title
	 * @param jobParameterMenuOptionName
	 * @return
	 */
	String evaluateJobParameterMenuOption(Title title, String jobParameterMenuOptionName);

	/**
	 * Sets the title status as APPROVED.
	 * 
	 * @throws PMMException	if status is not currently READY
	 */
	Title approve(long titleID) throws PMMException;
	
	/**
	 * Sets the title status to NEW / READY based on the title.
	 * 
	 * @throws PMMException	if status is not currently APPROVED
	 */
	Title disapprove(long titleID) throws PMMException;

	/**
	 * Find a unique external Title from an external source given the keys to find the title.
	 * 
	 * @param specification The name of the specification the external title obeys
	 * @param externalLocationId The key for the external location (Title Provider Instance Id) from
	 *        which to query the external title
	 * @param keys Map of the metadata keys and their values based on the Title specification.
	 * @return The unique external Title
	 */
	@Deprecated
	Title getExternalTitle(String specificationName, String externalLocationId,
			Map<String, String> keys);	
	
	/**
	 * Imports a new External Title to the PMM persistence repository. Validates that this title is
	 * not already present in the PMM title collection.
	 * 
	 * @param title The external Title to import
	 * @return The persisted Title
	 */
	@Deprecated
	Title importTitle(Title title);

	/**
	 * Sync the title with the external repository.
	 * 
	 * @param title The Title to synchronize
	 * @return The updated Title
	 */
	@Deprecated
	Title syncTitle(Title title);

}
