/**********************************************************************
 * 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 "vkprotocol.h"
#include <QIcon>
#include <QApplication>
#include <QSettings>
#include "vkaccount.h"
#include "mucjoinwidget.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Murm
{
	VkProtocol::VkProtocol (ICoreProxy_ptr proxy, IProxyObject *azothProxy, QObject *plugin)
	: QObject (plugin)
	, Proxy_ (proxy)
	, AzothProxy_ (azothProxy)
	, Plugin_ (plugin)
	{
		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Azoth_Murm");
		settings.beginGroup ("Accounts");
		for (const auto& id : settings.childKeys ())
		{
			const auto& data = settings.value (id).toByteArray ();
			if (auto acc = VkAccount::Deserialize (data, this, proxy))
				AddAccount (acc);
		}
		settings.endGroup ();
	}

	VkProtocol::~VkProtocol ()
	{
		for (auto acc : Accounts_)
			emit accountRemoved (acc);
	}

	IProxyObject* VkProtocol::GetAzothProxy () const
	{
		return AzothProxy_;
	}

	QObject* VkProtocol::GetQObject ()
	{
		return this;
	}

	IProtocol::ProtocolFeatures VkProtocol::GetFeatures () const
	{
		return PFSupportsMUCs | PFMUCsJoinable;
	}

	QList<QObject*> VkProtocol::GetRegisteredAccounts ()
	{
		QList<QObject*> result;
		for (auto acc : Accounts_)
			result << acc;
		return result;
	}

	QObject* VkProtocol::GetParentProtocolPlugin () const
	{
		return Plugin_;
	}

	QString VkProtocol::GetProtocolName () const
	{
		return tr ("VKontakte");
	}

	QIcon VkProtocol::GetProtocolIcon () const
	{
		static QIcon icon ("lcicons:/azoth/murm/resources/images/vk.svg");
		return icon;
	}

	QByteArray VkProtocol::GetProtocolID () const
	{
		return "Murm.VK";
	}

	QList<QWidget*> VkProtocol::GetAccountRegistrationWidgets (IProtocol::AccountAddOptions)
	{
		return {};
	}

	void VkProtocol::RegisterAccount (const QString& name, const QList<QWidget*>&)
	{
		auto acc = new VkAccount (name, this, Proxy_, {}, {});
		acc->Init ();
		saveAccount (acc);
		AddAccount (acc);
	}

	QWidget* VkProtocol::GetMUCJoinWidget ()
	{
		return new MucJoinWidget (Proxy_);
	}

	void VkProtocol::RemoveAccount (QObject *accObj)
	{
		auto acc = qobject_cast<VkAccount*> (accObj);

		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Azoth_Murm");
		settings.beginGroup ("Accounts");
		settings.remove (acc->GetAccountID ());
		settings.endGroup ();

		emit accountRemoved (accObj);

		Accounts_.removeAll (acc);

		acc->deleteLater ();
	}

	void VkProtocol::AddAccount (VkAccount *acc)
	{
		Accounts_ << acc;
		emit accountAdded (acc);

		connect (acc,
				SIGNAL (accountChanged (VkAccount*)),
				this,
				SLOT (saveAccount (VkAccount*)));
	}

	void VkProtocol::saveAccount (VkAccount *account)
	{
		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Azoth_Murm");
		settings.beginGroup ("Accounts");
		settings.setValue (account->GetAccountID (), account->Serialize ());
		settings.endGroup ();
	}
}
}
}
