/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#pragma once

#include <QString>
#include "audiostructs.h"

class QObject;
class QIcon;

namespace Media
{
	/** @brief Pending audio search handle.
	 *
	 * Interface for a handle to a pending audio search in an IAudioPile.
	 * A descendant of this class is returned from IAudioPile::Search()
	 * and is used to track the status of audio search requests.
	 *
	 * This class has some signals (ready() and error()), and one can use
	 * the GetQObject() method to get an object of this class as a
	 * QObject and connect to those signals.
	 *
	 * @note The object of this class should schedule its deletion (via
	 * <code>QObject::deleteLater()</code>, for example) after ready() or
	 * error() signal is emitted. Thus the calling code should never
	 * delete it explicitly, neither it should use this object after
	 * ready() or error() signals or connect to this signals via
	 * <code>Qt::QueuedConnection</code>.
	 *
	 * @sa IAudioPile
	 */
	class Q_DECL_EXPORT IPendingAudioSearch
	{
	public:
		virtual ~IPendingAudioSearch () {}

		/** @brief Returns this object as a QObject.
		 *
		 * This function can be used to connect to the signals of this
		 * class.
		 *
		 * @return This object as a QObject.
		 */
		virtual QObject* GetQObject () = 0;

		/** @brief A structure describing a single entry in search result.
		 */
		struct Result
		{
			/** @brief The information about the found audio track.
			 */
			AudioInfo Info_;

			/** @brief The URL of this audio track.
			 */
			QUrl Source_;
		};

		/** @brief Returns the list of audio tracks.
		 *
		 * This function returns the list of audio tracks that were found
		 * during this search, or an empty list if no tracks are found,
		 * an error occured on the search isn't finished yet.
		 *
		 * @return The list of found audio tracks.
		 */
		virtual QList<Result> GetResults () const = 0;
	protected:
		/** @brief Emitted when the search is completed without error.
		 *
		 * The object will be invalid after this signal is emitted and
		 * the event loop is run.
		 */
		virtual void ready () = 0;

		/** @brief Emitted when the search is cancelled due to errors.
		 *
		 * Empty result set is \em not an error. ready() will be
		 * emitted in that case.
		 *
		 * The object will be invalid after this signal is emitted and
		 * the event loop is run.
		 */
		virtual void error () = 0;
	};

	/** @brief Describes a request for an audio search in an IAudioPile.
	 *
	 * Different audio piles support filtering by different criteria, so
	 * calling plugins should not rely on all criteria being fulfilled.
	 * But at least filtering by either title, artist or free form
	 * request should be supported.
	 *
	 * @sa IAudioPile
	 */
	struct AudioSearchRequest
	{
		/** @brief The title of a track.
		 *
		 * At least this or Artist_ field should not be empty.
		 */
		QString Title_;

		/** @brief The artist performing the track.
		 *
		 * At least this or Title_ field should not be empty.
		 */
		QString Artist_;

		/** @brief The album containing this track.
		 */
		QString Album_;

		/** @brief The approximate length of the track.
		 *
		 * Set this to 0 to disable by-track filtering.
		 */
		int TrackLength_;

		/** @brief Free form engine-specific request.
		 *
		 * Calling plugins should set this instead of Title_ or Artist_
		 * fields if they are not sure what user has entered.
		 */
		QString FreeForm_;

		/** @brief Creates an empty audio search request.
		 */
		AudioSearchRequest ()
		: TrackLength_ (0)
		{
		}
	};

	/** @brief Interface for plugins supporting searching for tracks.
	 *
	 * Plugins that support searching for audio tracks in huge loosely
	 * categorized audio collections like VKontakte should implement this
	 * interface.
	 */
	class Q_DECL_EXPORT IAudioPile
	{
	public:
		virtual ~IAudioPile () {}

		/** @brief Returns the name of this service.
		 *
		 * This function returns the name of the service this IAudioPile
		 * represents, like "VKontakte".
		 *
		 * @return The well-known service name.
		 */
		virtual QString GetServiceName () const = 0;

		/** @brief Returns the icon of this service.
		 *
		 * This function returns the icon of the service this IAudioPile
		 * represents.
		 *
		 * @return The service icon.
		 */
		virtual QIcon GetServiceIcon () const = 0;

		/** @brief Requests a search by the given request.
		 *
		 * This function initiates a search by the given request and
		 * returns a handle that can be used to track the search result
		 * state. The handle owns itself and deletes itself after the
		 * results are available — see its documentation for details.
		 *
		 * @param[in] request The structure describing the search request.
		 * @return The pending audio search handle.
		 */
		virtual IPendingAudioSearch* Search (const AudioSearchRequest& request) = 0;
	};
}

Q_DECLARE_INTERFACE (Media::IPendingAudioSearch, "org.LeechCraft.Media.IPendingAudioSearch/1.0")
Q_DECLARE_INTERFACE (Media::IAudioPile, "org.LeechCraft.Media.IAudioPile/1.0")
