/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2013  Oleg Linkin <MaledictusDeMagog@gmail.com>
 *
 * 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 <memory>
#include <QObject>
#include <QQueue>
#include <QDateTime>
#include <QUrl>
#include <QStringList>
#include <QVariant>
#include <QNetworkReply>
#include <interfaces/structures.h>

class QFile;

namespace LeechCraft
{
namespace NetStoreManager
{
namespace DBox
{
	class Account;
	class ChunkIODevice;

	struct DBoxItem
	{
		QString Id_;
		QString ParentID_;
		QByteArray Revision_;

		QString Name_;
		quint64 FileSize_;
		bool IsFolder_;
		bool IsDeleted_;
		QString FolderHash_;
		QDateTime ModifiedDate_;
		QString MimeType_;


		DBoxItem ()
		: FileSize_ (0)
		, IsFolder_ (false)
		{
		}

		bool operator== (const DBoxItem& item) const
		{
			return Id_ == item.Id_;
		}
	};

	enum class ShareType
	{
		Preview,
		Share
	};

	class DriveManager : public QObject
	{
		Q_OBJECT

		const QString DirectoryId_;

		Account *Account_;
		QQueue<std::function<void ()>> ApiCallQueue_;
		QHash<QNetworkReply*, QString> Reply2Id_;
		QHash<QNetworkReply*, QString> Reply2FilePath_;
		QHash<QNetworkReply*, QString> Reply2ParentId_;
		QHash<QNetworkReply*, quint64> Reply2Offset_;
		bool SecondRequestIfNoItems_;
		const int ChunkUploadBound_;

	public:
		DriveManager (Account *acc, QObject *parent = 0);

		void RequestUserId ();

		void RefreshListing (const QByteArray& parentId = QByteArray ());
		void ShareEntry (const QString& id, ShareType type);
		void CreateDirectory (const QString& name,
				const QString& parentId = QString ());
		void RemoveEntry (const QByteArray& id);
		void Copy (const QByteArray& id, const QString& parentId);
		void Move (const QByteArray& id, const QString& parentId);

		void Upload (const QString& filePath,
				const QStringList& parentId = QStringList ());
		void Download (const QString& id, const QString& filePath,
				TaskParameters tp, bool open = false);
	private:
		std::shared_ptr<void> MakeRunnerGuard ();
		void RequestAccountInfo ();
		void RequestFiles (const QByteArray& parntId);
		void RequestSharingEntry (const QString& id, ShareType type);
		void RequestCreateDirectory (const QString& name, const QString& parentId);
		void RequestEntryRemoving (const QString& id);
		void RequestCopyItem (const QString& id, const QString& parentId);
		void RequestMoveItem (const QString& id, const QString& parentId);

		void RequestUpload (const QString& filePath, const QString& parent);
		void RequestChunkUpload (const QString& filePath, const QString& parent,
				const QString& uploadId = QString (), quint64 offset = 0);
		void DownloadFile (const QString& id, const QString& filePath,
				TaskParameters tp, bool open = false);

		void ParseError (const QVariantMap& map);

	private slots:
		void handleGotAccountInfo ();
		void handleGotFiles ();
		void handleRequestFileSharing ();
		void handleCreateDirectory ();
		void handleRequestEntryRemoving ();
		void handleCopyItem ();
		void handleMoveItem ();
		void handleUploadFinished ();
		void handleChunkUploadFinished ();
		void handleUploadProgress (qint64 uploaded, qint64 total);
		void handleUploadError (QNetworkReply::NetworkError error);

	signals:
		void gotFiles (const QList<DBoxItem>& items);
		void gotSharedFileUrl (const QUrl& url, const QDateTime& expiredDate);
		void uploadProgress (qint64 sent, qint64 total, const QString& filePath);
		void uploadStatusChanged (const QString& status, const QString& filePath);
		void uploadError (const QString& str, const QString& filePath);
		void finished (const QString& id, const QString& path);

		void gotNewItem (const DBoxItem& item);
	};
}
}
}
