/**********************************************************************
 * 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 <qwebpage.h>
#include <QUrl>
#include <QNetworkRequest>
#include <interfaces/structures.h>
#include <interfaces/core/ihookproxy.h>
#include <interfaces/core/icoreproxyfwd.h>
#include <interfaces/poshuku/poshukutypes.h>
#include <interfaces/poshuku/ilinkopenmodifier.h>

namespace LeechCraft
{
namespace Poshuku
{
class IProxyObject;

namespace WebKitView
{
	class JSProxy;
	class ExternalProxy;
	class CustomWebView;

	class CustomWebPage : public QWebPage
	{
		Q_OBJECT

		const ICoreProxy_ptr Proxy_;
		IProxyObject * const PoshukuProxy_;

		QUrl LoadingURL_;
		std::shared_ptr<JSProxy> JSProxy_;
		std::shared_ptr<ExternalProxy> ExternalProxy_;
		PageFormsData_t FilledState_;

		QMap<ErrorDomain, QMap<int, QStringList>> Error2Suggestions_;

		const ILinkOpenModifier_ptr LinkOpenModifier_;
	public:
		CustomWebPage (const ICoreProxy_ptr&, IProxyObject*, QObject* = nullptr);

		void HandleViewReady ();

		QUrl GetLoadingURL () const;

		bool supportsExtension (Extension) const override;
		bool extension (Extension, const ExtensionOption*, ExtensionReturn*) override;
	private slots:
		void handleContentsChanged ();
		void handleDatabaseQuotaExceeded (QWebFrame*, QString);
		void handleDownloadRequested (QNetworkRequest);
		void handleFrameCreated (QWebFrame*);
		void handleJavaScriptWindowObjectCleared ();
		void handleGeometryChangeRequested (const QRect&);
		void handleLinkClicked (const QUrl&);
		void handleLinkHovered (const QString&, const QString&, const QString&);
		void handleLoadFinished (bool);
		void handleUnsupportedContent (QNetworkReply*);
		void handleWindowCloseRequested ();
		void fillForms (QWebFrame*);
	protected:
		bool acceptNavigationRequest (QWebFrame*, const QNetworkRequest&, QWebPage::NavigationType) override;
		QString chooseFile (QWebFrame*, const QString&) override;
		QObject* createPlugin (const QString&, const QUrl&, const QStringList&, const QStringList&) override;
		QWebPage* createWindow (WebWindowType) override;
		void javaScriptAlert (QWebFrame*, const QString&) override;
		bool javaScriptConfirm (QWebFrame*, const QString&) override;
		void javaScriptConsoleMessage (const QString&, int, const QString&) override;
		bool javaScriptPrompt (QWebFrame*, const QString&, const QString&, QString*) override;
		QString userAgentForUrl (const QUrl&) const override;
	private:
		bool HandleExtensionProtocolUnknown (const ErrorPageExtensionOption*);
		void FillErrorSuggestions ();
		QString MakeErrorReplyContents (int, const QUrl&,
				const QString&, ErrorDomain = WebKit) const;
		QWebFrame* FindFrame (const QUrl&);
		void HandleForms (QWebFrame*, const QNetworkRequest&,
				QWebPage::NavigationType);
	signals:
		void loadingURL (const QUrl&);
		void storeFormData (const PageFormsData_t&);
		void delayedFillForms (QWebFrame*);

		void webViewCreated (const std::shared_ptr<CustomWebView>&, bool);

		// Hook support signals
		void hookAcceptNavigationRequest (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebFrame *frame,
				QNetworkRequest request,
				QWebPage::NavigationType type);
		void hookChooseFile (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebFrame *frame,
				QString suggested);
		void hookContentsChanged (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page);
		void hookCreatePlugin (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QString clsid,
				QUrl url,
				QStringList params,
				QStringList values);
		void hookCreateWindow (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebPage::WebWindowType type);
		void hookDatabaseQuotaExceeded (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *sourcePage,
				QWebFrame *sourceFrame,
				QString databaseName);
		void hookDownloadRequested (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *sourcePage,
				QNetworkRequest downloadRequest);
		void hookExtension (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebPage::Extension extension,
				const QWebPage::ExtensionOption* extensionOption,
				QWebPage::ExtensionReturn* extensionReturn);
		void hookFrameCreated (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebFrame *frameCreated);
		void hookGeometryChangeRequested (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QRect rect);
		void hookJavaScriptAlert (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebFrame *frame,
				QString msg);
		void hookJavaScriptConfirm (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebFrame *frame,
				QString msg);
		void hookJavaScriptConsoleMessage (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QString msg,
				int line,
				QString sourceId);
		void hookJavaScriptPrompt (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QWebFrame *frame,
				QString msg,
				QString defValue,
				QString resultString);
		void hookJavaScriptWindowObjectCleared (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *sourcePage,
				QWebFrame *frameCleared);
		void hookLinkClicked (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QUrl url);
		void hookLinkHovered (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QString link,
				QString title,
				QString textContent);
		void hookLoadFinished (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				bool result);
		void hookSupportsExtension (LeechCraft::IHookProxy_ptr proxy,
				const QWebPage *page,
				QWebPage::Extension extension) const;
		void hookUnsupportedContent (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page,
				QNetworkReply *reply);
		void hookWebPageConstructionBegin (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page);
		void hookWebPageConstructionEnd (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page);
		void hookWindowCloseRequested (LeechCraft::IHookProxy_ptr proxy,
				QWebPage *page);
	};
}
}
}
