/**********************************************************************
 * 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 <memory>
#include <functional>
#include <QObject>
#include <QMap>
#include <QLinkedList>
#include <util/sll/util.h>

template<typename T>
class QFuture;

namespace LeechCraft
{
namespace Util
{
	/** @brief Garbage collection for a set of network disk caches.
	 *
	 * This GC manager class aids having multiple network disk caches at
	 * the same path and running garbage collection periodically on them,
	 * but only once per each path.
	 *
	 * @ingroup NetworkUtil
	 */
	class NetworkDiskCacheGC : public QObject
	{
		Q_OBJECT

		using CacheSizeGetters_t = QLinkedList<std::function<int ()>>;
		QMap<QString, CacheSizeGetters_t> Directories_;

		QMap<QString, qint64> LastSizes_;

		bool IsCollecting_ = false;

		NetworkDiskCacheGC ();
	public:
		NetworkDiskCacheGC (const NetworkDiskCacheGC&) = delete;
		NetworkDiskCacheGC& operator= (const NetworkDiskCacheGC&) = delete;

		/** @brief Returns a single global instance of the GC manager.
		 *
		 * @return The single global instance of the GC manager.
		 */
		static NetworkDiskCacheGC& Instance ();

		/** @brief Schedules calculation of the \em path total size.
		 *
		 * The calculation is performed asynchronously in a separate
		 * thread, and a future object is returned which can be used to
		 * be notified when the calculation finishes.
		 *
		 * @param[in] path The path which total size should be calculated
		 * @return The future object for the asynchronous path size
		 * calculation.
		 */
		QFuture<qint64> GetCurrentSize (const QString& path) const;

		/** @brief Registers the given cache \em path.
		 *
		 * Registers the given \em path to be collected periodically and
		 * returns a guard object that unregisters the path when it is
		 * destroyed.
		 *
		 * The \em path stops being collected as soon as the last guard
		 * object returned from this method is destroyed.
		 *
		 * This function also takes a size getter functor returning the
		 * desired cache size for the given \em path. If the same path is
		 * registered multiple times with size getters returning
		 * different values, the minimum one is used.
		 *
		 * @param[in] path The path to register for garbage collection.
		 * @param[in] sizeGetter The functor returning the desired total
		 * size of files under the \em path.
		 * @return A guard object unregistering the path when it is
		 * destroyed.
		 */
		Util::DefaultScopeGuard RegisterDirectory (const QString& path,
				const std::function<int ()>& sizeGetter);
	private:
		void UnregisterDirectory (const QString&, CacheSizeGetters_t::iterator);
	private slots:
		void handleCollect ();
	};
}
}
