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

#include "vkauthmanager.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkCookie>
#include <QtDebug>
#include <QTimer>
#include <QEvent>
#include <QWebView>
#include <util/network/customcookiejar.h>
#include <util/sll/queuemanager.h>
#include <util/sll/urloperator.h>
#include <util/xpc/util.h>
#include <interfaces/core/ientitymanager.h>
#include <xmlsettingsdialog/basesettingsmanager.h>

namespace LeechCraft
{
namespace Util
{
namespace SvcAuth
{
	namespace
	{
		QUrl URLFromClientID (const QString& id, const QStringList& scope)
		{
			auto url = QUrl::fromEncoded ("https://oauth.vk.com/authorize?redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&state=");
			UrlOperator { url }
					("client_id", id)
					("scope", scope.join (","));
			return url;
		}
	}

	VkAuthManager::VkAuthManager (const QString& accName,
			const QString& id, const QStringList& scope,
			const QByteArray& cookies, ICoreProxy_ptr proxy,
			QueueManager *queueMgr, QObject *parent)
	: QObject (parent)
	, Proxy_ (proxy)
	, AccountHR_ (accName)
	, AuthNAM_ (new QNetworkAccessManager (this))
	, Cookies_ (new Util::CustomCookieJar (this))
	, Queue_ (queueMgr)
	, ValidFor_ (0)
	, IsRequesting_ (false)
	, ID_ (id)
	, URL_ (URLFromClientID (ID_, scope))
	, IsRequestScheduled_ (false)
	, ScheduleTimer_ (new QTimer (this))
	{
		AuthNAM_->setCookieJar (Cookies_);
		Cookies_->Load (cookies);

		ScheduleTimer_->setSingleShot (true);
		connect (ScheduleTimer_,
				SIGNAL (timeout ()),
				this,
				SLOT (execScheduledRequest ()));
	}

	bool VkAuthManager::IsAuthenticated () const
	{
		return !Token_.isEmpty () &&
			(!ValidFor_ || ReceivedAt_.secsTo (QDateTime::currentDateTime ()) < ValidFor_);
	}

	bool VkAuthManager::HadAuthentication () const
	{
		return !Token_.isEmpty () || !Cookies_->allCookies ().isEmpty ();
	}

	void VkAuthManager::UpdateScope (const QStringList& scope)
	{
		const auto& newUrl = URLFromClientID (ID_, scope);
		if (URL_ == newUrl)
			return;

		URL_ = newUrl;
		Token_.clear ();
		ReceivedAt_ = QDateTime ();
		ValidFor_ = 0;
	}

	void VkAuthManager::GetAuthKey ()
	{
		if (SilentMode_)
		{
			PrioManagedQueues_.clear ();
			ManagedQueues_.clear ();
			return;
		}

		if (!IsAuthenticated ())
		{
			RequestAuthKey ();
			return;
		}

		InvokeQueues (Token_);
		emit gotAuthKey (Token_);
	}

	auto VkAuthManager::ManageQueue (VkAuthManager::RequestQueue_ptr queue) -> ScheduleGuard_t
	{
		if (!Queue_)
		{
			qWarning () << Q_FUNC_INFO
					<< "cannot manage request queue if queue manager wasn't set";
			return {};
		}

		ManagedQueues_ << queue;

		return Util::MakeScopeGuard ([this, queue] { ManagedQueues_.removeAll (queue); });
	}

	auto VkAuthManager::ManageQueue (VkAuthManager::PrioRequestQueue_ptr queue) -> ScheduleGuard_t
	{
		if (!Queue_)
		{
			qWarning () << Q_FUNC_INFO
					<< "cannot manage request queue if queue manager wasn't set";
			return {};
		}

		PrioManagedQueues_ << queue;

		return Util::MakeScopeGuard ([this, queue] { PrioManagedQueues_.removeAll (queue); });
	}

	void VkAuthManager::SetSilentMode (bool silent)
	{
		SilentMode_ = silent;
	}

	void VkAuthManager::InvokeQueues (const QString& token)
	{
		ScheduleTrack (token);

		for (auto queue : PrioManagedQueues_)
			while (!queue->isEmpty ())
			{
				const auto& pair = queue->takeFirst ();
				const auto& f = pair.first;
				Queue_->Schedule ([f, token] { f (token); }, nullptr, pair.second);
			}

		for (auto queue : ManagedQueues_)
			while (!queue->isEmpty ())
			{
				const auto& f = queue->takeFirst ();
				Queue_->Schedule ([f, token] { f (token); });
			}
	}

	void VkAuthManager::RequestURL (const QUrl& url)
	{
		qDebug () << Q_FUNC_INFO << url;
		auto reply = AuthNAM_->get (QNetworkRequest (url));
		connect (reply,
				SIGNAL (finished ()),
				this,
				SLOT (handleGotForm ()));
	}

	void VkAuthManager::RequestAuthKey ()
	{
		if (IsRequestScheduled_ && ScheduleTimer_->isActive ())
			ScheduleTimer_->stop ();

		if (IsRequesting_)
			return;

		RequestURL (URL_);
		IsRequesting_ = true;
	}

	bool VkAuthManager::CheckReply (QUrl location)
	{
		if (location.path () != "/blank.html")
			return CheckError (location);

		location = QUrl::fromEncoded (location.toEncoded ().replace ('#', '?'));
#if QT_VERSION < 0x050000
		Token_ = location.queryItemValue ("access_token");
		ValidFor_ = location.queryItemValue ("expires_in").toInt ();
#else
		const QUrlQuery query { location };
		Token_ = query.queryItemValue ("access_token");
		ValidFor_ = query.queryItemValue ("expires_in").toInt ();
#endif
		ReceivedAt_ = QDateTime::currentDateTime ();
		qDebug () << Q_FUNC_INFO << Token_ << ValidFor_;
		IsRequesting_ = false;

		InvokeQueues (Token_);
		emit gotAuthKey (Token_);
		emit justAuthenticated ();

		return true;
	}

	bool VkAuthManager::CheckError (const QUrl& url)
	{
		if (url.path () != "/error")
			return false;

#if QT_VERSION < 0x050000
		const auto errNum = url.queryItemValue ("err").toInt ();
#else
		const auto errNum = QUrlQuery { url }.queryItemValue ("err").toInt ();
#endif

		IsRequesting_ = false;

		qWarning () << Q_FUNC_INFO
				<< "got error"
				<< errNum;
		if (errNum == 2)
		{
			clearAuthData ();

			RequestAuthKey ();
			return true;
		}

		const auto& e = Util::MakeNotification ("VK.com",
				tr ("VK.com authentication for %1 failed because of error %2. "
					"Report upstream please.")
					.arg (AccountHR_)
					.arg (errNum),
				PCritical_);
		Proxy_->GetEntityManager ()->HandleEntity (e);

		return true;
	}

	void VkAuthManager::ScheduleTrack (const QString& key)
	{
		if (HasTracked_)
			return;

		if (!Proxy_->GetSettingsManager ()->property ("TrackVK").toBool ())
			return;

		HasTracked_ = true;

		QUrl url { "https://api.vk.com/method/stats.trackVisitor" };
		Util::UrlOperator { url }
				("access_token", key);

		auto reply = AuthNAM_->get (QNetworkRequest { url });
		connect (reply,
				SIGNAL (finished ()),
				reply,
				SLOT (deleteLater ()));
	}

	void VkAuthManager::clearAuthData ()
	{
		Cookies_->Load ({});
		Token_.clear ();
		ReceivedAt_ = QDateTime ();
		ValidFor_ = 0;
	}

	namespace
	{
		class CloseEventFilter : public QObject
		{
			const std::function<void ()> Handler_;
		public:
			CloseEventFilter (const std::function<void ()>& handler, QObject *handlee)
			: QObject { handlee }
			, Handler_ { handler }
			{
				handlee->installEventFilter (this);
			}

			bool eventFilter (QObject*, QEvent *event)
			{
				if (event->type () == QEvent::Close)
					Handler_ ();
				return false;
			}
		};
	}

	void VkAuthManager::reauth ()
	{
		auto view = new QWebView;
		view->setWindowTitle (tr ("VK.com authentication for %1")
				.arg (AccountHR_));
		view->setWindowFlags (Qt::Window);
		view->resize (800, 600);
		view->page ()->setNetworkAccessManager (AuthNAM_);
		view->show ();

		view->setUrl (URL_);

		connect (view,
				SIGNAL (urlChanged (QUrl)),
				this,
				SLOT (handleViewUrlChanged (QUrl)));

		new CloseEventFilter ([this] { emit authCanceled (); }, view);
	}

	void VkAuthManager::execScheduledRequest ()
	{
		IsRequestScheduled_ = false;

		RequestAuthKey ();
	}

	void VkAuthManager::handleGotForm ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

		if (reply->error () != QNetworkReply::NoError)
		{
			qWarning () << Q_FUNC_INFO
					<< reply->error ()
					<< reply->errorString ();

			IsRequesting_ = false;

			if (!IsRequestScheduled_)
			{
				IsRequestScheduled_ = true;
				ScheduleTimer_->start (30000);
			}

			return;
		}

		const auto& location = reply->header (QNetworkRequest::LocationHeader).toUrl ();
		if (location.isEmpty ())
		{
			reauth ();
			return;
		}

		if (CheckReply (location))
			return;

		RequestURL (location);
	}

	void VkAuthManager::handleViewUrlChanged (const QUrl& url)
	{
		if (!CheckReply (url))
			return;

		emit cookiesChanged (Cookies_->Save ());
		sender ()->deleteLater ();
	}
}
}
}
