/**********************************************************************
 * 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 "fua.h"
#include <QStandardItemModel>
#include <QUrl>
#include <QSettings>
#include <QFile>
#include <QCoreApplication>
#include <util/util.h>
#include <util/sll/parsejson.h>
#include <xmlsettingsdialog/xmlsettingsdialog.h>
#include <interfaces/poshuku/iproxyobject.h>
#include "settings.h"
#include "xmlsettingsmanager.h"

namespace LC
{
namespace Util
{
	class XmlSettingsDialog;
};
namespace Poshuku
{
namespace Fua
{
	void FUA::Init (ICoreProxy_ptr)
	{
		Util::InstallTranslator ("poshuku_fua");

		Model_ = std::make_shared<QStandardItemModel> ();
		Model_->setHorizontalHeaderLabels ({ tr ("Domain"), tr ("Agent"), tr ("Identification string") });

		XmlSettingsDialog_ = std::make_shared<Util::XmlSettingsDialog> ();
		XmlSettingsDialog_->RegisterObject (XmlSettingsManager::Instance (),
				"poshukufuasettings.xml");
		XmlSettingsDialog_->SetCustomWidget ("Settings", new Settings { Model_.get (), this });
	}

	void FUA::SecondInit ()
	{
	}

	void FUA::Release ()
	{
	}

	QByteArray FUA::GetUniqueID () const
	{
		return "org.LeechCraft.Poshuku.FUA";
	}

	QString FUA::GetName () const
	{
		return "Poshuku FUA";
	}

	QString FUA::GetInfo () const
	{
		return tr ("Allows one to set fake user agents for different sites.");
	}

	QIcon FUA::GetIcon () const
	{
		static QIcon icon { "lcicons:/poshuku/fua/resources/images/poshuku_fua.svg" };
		return icon;
	}

	QSet<QByteArray> FUA::GetPluginClasses () const
	{
		QSet<QByteArray> result;
		result << "org.LeechCraft.Poshuku.Plugins/1.0";
		return result;
	}

	Util::XmlSettingsDialog_ptr FUA::GetSettingsDialog () const
	{
		return XmlSettingsDialog_;
	}

	void FUA::hookUserAgentForUrlRequested (LC::IHookProxy_ptr proxy,
			const QUrl& url)
	{
		const auto& host = url.host ();
		for (int i = 0; i < Model_->rowCount (); ++i)
		{
			const auto item = Model_->item (i);
			QRegExp re { item->text (), Qt::CaseSensitive, QRegExp::Wildcard };
			if (re.exactMatch (host))
			{
				proxy->CancelDefault ();
				proxy->SetReturnValue (Model_->item (i, 2)->text ());
				return;
			}
		}
	}

	void FUA::Save () const
	{
		QSettings settings { QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Poshuku_FUA" };
		settings.beginWriteArray ("Fakes");
		settings.remove ("");
		for (int i = 0; i < Model_->rowCount (); ++i)
		{
			settings.setArrayIndex (i);
			settings.setValue ("domain", Model_->item (i, 0)->text ());
			settings.setValue ("identification", Model_->item (i, 2)->text ());
		}
		settings.endArray ();
	}

	const QList<QPair<QString, QString>>& FUA::GetBrowser2ID () const
	{
		return Browser2ID_;
	}

	const QMap<QString, QString>& FUA::GetBackLookupMap () const
	{
		return BackLookup_;
	}

	namespace
	{
		QList<QPair<QString, QString>> LoadFromJson ()
		{
			QFile file { ":/poshuku/fua/resources/data/substs.json" };
			if (!file.open (QIODevice::ReadOnly))
			{
				qWarning () << Q_FUNC_INFO
						<< "cannot open file:"
						<< file.errorString ();
				return {};
			}

			const auto& json = Util::ParseJson (&file, Q_FUNC_INFO);

			QList<QPair<QString, QString>> result;

			for (const auto& itemVar : json.toList ())
			{
				const auto& map = itemVar.toMap ();
				const auto& name = map ["n"].toString ();
				const auto& value = map ["v"].toString ();
				if (name.isEmpty () || value.isEmpty ())
				{
					qWarning () << Q_FUNC_INFO
							<< "cannot parse"
							<< itemVar;
					continue;
				}

				result.append ({ name, value });
			}

			return result;
		}

		QMap<QString, QString> MakeLookupMap (const QList<QPair<QString, QString>>& pairs)
		{
			QMap<QString, QString> result;
			for (const auto& pair : pairs)
				result [pair.second] = pair.first;
			return result;
		}
	}

	void FUA::initPlugin (QObject*)
	{
		Browser2ID_ = LoadFromJson ();
		Browser2ID_.prepend ({ tr ("LeechCraft (this machine)"), "" });

		BackLookup_ = MakeLookupMap (Browser2ID_);

		QSettings settings { QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Poshuku_FUA" };
		int size = settings.beginReadArray ("Fakes");

		for (int i = 0; i < size; ++i)
		{
			settings.setArrayIndex (i);
			const auto& domain = settings.value ("domain").toString ();
			const auto& identification = settings.value ("identification").toString ();
			const QList<QStandardItem*> items
			{
				new QStandardItem { domain },
				new QStandardItem { BackLookup_ [identification] },
				new QStandardItem { identification }
			};
			Model_->appendRow (items);
		}
		settings.endArray ();
	}
}
}
}

LC_EXPORT_PLUGIN (leechcraft_poshuku_fua, LC::Poshuku::Fua::FUA);
