/**********************************************************************
 * 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.
 **********************************************************************/

#ifndef PLUGINS_BITTORRENT_TORRENTFILESMODEL_H
#define PLUGINS_BITTORRENT_TORRENTFILESMODEL_H
#include <unordered_map>
#include <QAbstractItemModel>
#include <libtorrent/torrent_info.hpp>
#include <interfaces/structures.h>
#include "fileinfo.h"

namespace LeechCraft
{
	namespace Util
	{
		class TreeItem;
	};

	namespace Plugins
	{
		namespace BitTorrent
		{
			struct Hash : public std::unary_function<boost::filesystem::path, size_t>
			{
				std::hash<std::string> H_;

				size_t operator() (const boost::filesystem::path& p) const
				{
					return H_ (p.string ());
				}
			};

			struct MyEqual : public std::binary_function<boost::filesystem::path,
				boost::filesystem::path,
				bool>
			{
				bool operator() (const boost::filesystem::path& p1,
						const boost::filesystem::path& p2) const
				{
					return p1.string () == p2.string ();
				}
			};

			typedef std::unordered_map<boost::filesystem::path,
					LeechCraft::Util::TreeItem*,
					Hash,
					MyEqual> Path2TreeItem_t;

			typedef std::unordered_map<boost::filesystem::path,
					int,
					Hash,
					MyEqual> Path2Position_t;

			class TorrentFilesModel : public QAbstractItemModel
			{
				Q_OBJECT

				LeechCraft::Util::TreeItem *RootItem_;
				bool AdditionDialog_;
				Path2TreeItem_t Path2TreeItem_;
				Path2Position_t Path2OriginalPosition_;
				int FilesInTorrent_;
				boost::filesystem::path BasePath_;
				const int Index_;
			public:
				enum
				{
					RawDataRole = Qt::UserRole + 1,
					RolePath,
					RoleSize,
					RoleProgress
				};
				enum
				{
					ColumnPath,
					ColumnPriority,
					ColumnProgress
				};

				TorrentFilesModel (QObject *parent = 0);
				TorrentFilesModel (int);
				virtual ~TorrentFilesModel ();

				virtual int columnCount (const QModelIndex&) const;
				virtual QVariant data (const QModelIndex&, int = Qt::DisplayRole) const;
				virtual Qt::ItemFlags flags (const QModelIndex&) const;
				virtual QVariant headerData (int, Qt::Orientation, int = Qt::DisplayRole) const;
				virtual QModelIndex index (int, int, const QModelIndex& = QModelIndex ()) const;
				virtual QModelIndex parent (const QModelIndex&) const;
				virtual int rowCount (const QModelIndex& = QModelIndex ()) const;
				virtual bool setData (const QModelIndex&, const QVariant&, int = Qt::EditRole);

				void Clear ();
				void ResetFiles (libtorrent::torrent_info::file_iterator,
						libtorrent::torrent_info::file_iterator,
						const libtorrent::file_storage&);
				void ResetFiles (const boost::filesystem::path&, const QList<FileInfo>&);
				void UpdateFiles (const boost::filesystem::path&, const QList<FileInfo>&);
				QVector<bool> GetSelectedFiles () const;
				void MarkAll ();
				void UnmarkAll ();
				void MarkIndexes (const QList<QModelIndex>&);
				void UnmarkIndexes (const QList<QModelIndex>&);

				void HandleFileActivated (QModelIndex) const;
			public slots:
				void update ();
			private:
				void MkParentIfDoesntExist (const boost::filesystem::path&);
				void UpdateSizeGraph (LeechCraft::Util::TreeItem*);
			signals:
				void gotEntity (const LeechCraft::Entity&) const;
			};
		};
	};
};

#endif

