/**********************************************************************
 * 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 <functional>
#include <boost/optional.hpp>
#include <QObject>
#include <QDateTime>
#include <QPointer>
#include <QPair>
#include "sllconfig.h"

class QTimer;

namespace LeechCraft
{
namespace Util
{
	/** @brief The priority of the action in the queue.
	 */
	enum class QueuePriority
	{
		/** @brief Standard priority.
		 */
		Normal,

		/** @brief Higher priority.
		 */
		High
	};

	/** @brief A simple scheduling manager for a queue of functors.
	 *
	 * This class manages execution of functors that should be called
	 * with some minimal timeout between them.
	 */
	class UTIL_SLL_API QueueManager : public QObject
	{
		Q_OBJECT

		const int Timeout_;
		QTimer * const ReqTimer_;
		QDateTime LastRequest_;

		bool Paused_;

		typedef boost::optional<QPointer<QObject>> OptionalTracker_t;
		QList<QPair<std::function<void ()>, boost::optional<QPointer<QObject>>>> Queue_;
	public:
		/** @brief Creates a queue manager with the given \em timeout.
		 *
		 * @param[in] timeout The timeout between invoking the functions
		 * in milliseconds.
		 * @param[in] parent The parent object of this queue manager.
		 */
		QueueManager (int timeout, QObject *parent = nullptr);

		/** @brief Adds the given \em functor.
		 *
		 * This function adds the given \em functor to the execution
		 * queue, or executes it right at the point of adding if more
		 * than \em timeout has passed since executing the last functor.
		 *
		 * \em dependent is an object this \em functor depends upon. If
		 * \em dependent object is destructed by the time queue reaches
		 * the passed \em functor, the functor will be skipped and next
		 * scheduled functor will be executed (if any).
		 *
		 * @param[in] functor The functor to add to the queue.
		 * @param[in] dependent The dependent object, or nullptr if this
		 * \em functor doesn't depend on anything.
		 * @param[in] prio The priority of the \em functor. Functors with
		 * high priority are added to the beginning of the queue.
		 */
		void Schedule (std::function<void ()> functor,
				QObject *dependent = nullptr,
				QueuePriority prio = QueuePriority::Normal);

		/** @brief Clears the queue.
		 *
		 * Clears the remaining items in the queue, but doesn't abort the
		 * current operation.
		 */
		void Clear ();

		/** @brief Pauses the queue rotation.
		 *
		 * If the queue is already paused, this function does nothing.
		 *
		 * @sa IsPaused(), Resume()
		 */
		void Pause ();

		/** @brief Checks if the queue is paused.
		 *
		 * @return Whether the queue is paused.
		 *
		 * @sa Pause(), Resume()
		 */
		bool IsPaused () const;

		/** @brief Continues the queue rotation.
		 *
		 * If the queue is already running, this function does nothing.
		 *
		 * @sa IsPaused(), Pause()
		 */
		void Resume ();
	private slots:
		void exec ();
	};
}
}
