/**********************************************************************
 * 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 "flickrservice.h"
#include <QIcon>
#include <QSettings>
#include <QCoreApplication>
#include <QtDebug>
#include "flickraccount.h"

namespace LeechCraft
{
namespace Blasq
{
namespace Spegnersi
{
	FlickrService::FlickrService (ICoreProxy_ptr proxy)
	: Proxy_ (proxy)
	{
		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Blasq_Spegnersi");
		settings.beginGroup ("Accounts");
		for (const auto& key : settings.childKeys ())
		{
			const auto& serialized = settings.value (key).toByteArray ();
			auto acc = FlickrAccount::Deserialize (serialized, this, proxy);
			if (acc)
				AddAccount (acc);
		}
		settings.endGroup ();
	}

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

	QString FlickrService::GetServiceName () const
	{
		return "Flickr";
	}

	QIcon FlickrService::GetServiceIcon () const
	{
		static QIcon icon (":/blasq/spegnersi/resources/images/flickricon.png");
		return icon;
	}

	QList<IAccount*> FlickrService::GetRegisteredAccounts () const
	{
		QList<IAccount*> result;
		std::copy (Accounts_.begin (), Accounts_.end (), std::back_inserter (result));
		return result;
	}

	QList<QWidget*> FlickrService::GetAccountRegistrationWidgets () const
	{
		return {};
	}

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

	void FlickrService::RemoveAccount (IAccount *acc)
	{
		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Blasq_Spegnersi");
		settings.beginGroup ("Accounts");
		settings.remove (acc->GetID ());
		settings.endGroup ();

		const auto pos = std::find (Accounts_.begin (), Accounts_.end (), acc);
		if (pos == Accounts_.end ())
		{
			qWarning () << Q_FUNC_INFO
					<< "account"
					<< acc->GetID ()
					<< "not found";
			return;
		}

		emit accountRemoved (*pos);
		(*pos)->deleteLater ();

		Accounts_.erase (pos);
	}

	void FlickrService::AddAccount (FlickrAccount *acc)
	{
		Accounts_ << acc;
		emit accountAdded (acc);
		connect (acc,
				SIGNAL (accountChanged (FlickrAccount*)),
				this,
				SLOT (saveAccount (FlickrAccount*)));
	}

	void FlickrService::saveAccount (FlickrAccount *acc)
	{
		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_Blasq_Spegnersi");
		settings.beginGroup ("Accounts");
		settings.setValue (acc->GetID (), acc->Serialize ());
		settings.endGroup ();
	}
}
}
}
