/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2013  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 "autoidler.h"
#include <QIcon>
#include <QTranslator>
#include <xmlsettingsdialog/xmlsettingsdialog.h>
#include <util/util.h>
#include <interfaces/azoth/iproxyobject.h>
#include <interfaces/azoth/iaccount.h>
#include "xmlsettingsmanager.h"
#include "3dparty/idle.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Autoidler
{
	void Plugin::Init (ICoreProxy_ptr proxy)
	{
		IdleSeconds_ = 0;

		Translator_.reset (Util::InstallTranslator ("azoth_autoidler"));

		Proxy_ = proxy;

		XmlSettingsDialog_.reset (new Util::XmlSettingsDialog);
		XmlSettingsDialog_->RegisterObject (&XmlSettingsManager::Instance (),
				"azothautoidlersettings.xml");

		Idle_.reset (new Idle);
		connect (Idle_.get (),
				SIGNAL (secondsIdle (int)),
				this,
				SLOT (handleIdle (int)));
	}

	void Plugin::SecondInit ()
	{
		Idle_->start ();
	}

	QByteArray Plugin::GetUniqueID () const
	{
		return "org.LeechCraft.Azoth.Autoidler";
	}

	void Plugin::Release ()
	{
	}

	QString Plugin::GetName () const
	{
		return "Azoth Autoidler";
	}

	QString Plugin::GetInfo () const
	{
		return tr ("Automatically updates statuses depending on idle time.");
	}

	QIcon Plugin::GetIcon () const
	{
		static QIcon icon (":/plugins/azoth/plugins/autoidler/resources/images/autoidler.svg");
		return icon;
	}

	QSet<QByteArray> Plugin::GetPluginClasses () const
	{
		QSet<QByteArray> result;
		result << "org.LeechCraft.Plugins.Azoth.Plugins.IGeneralPlugin";
		return result;
	}

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

	int Plugin::GetInactiveSeconds ()
	{
		return IdleSeconds_;
	}

	void Plugin::initPlugin (QObject *proxy)
	{
		AzothProxy_ = qobject_cast<IProxyObject*> (proxy);
	}

	void Plugin::handleIdle (int seconds)
	{
		IdleSeconds_ = seconds;
		if (seconds && seconds % 60)
			return;

		if (!XmlSettingsManager::Instance ().property ("EnableAutoidler").toBool ())
			return;

		const int mins = seconds / 60;
		if (!mins &&
				!OldStatuses_.isEmpty ())
		{
			for (QObject *accObj : AzothProxy_->GetAllAccounts ())
			{
				if (!OldStatuses_.contains (accObj))
					continue;

				IAccount *acc = qobject_cast<IAccount*> (accObj);
				acc->ChangeState (OldStatuses_ [accObj]);
			}

			OldStatuses_.clear ();
		}

		if (!mins)
			return;

		EntryStatus status;
		if (mins == XmlSettingsManager::Instance ().property ("AwayTimeout").toInt ())
			status = EntryStatus (SAway,
					XmlSettingsManager::Instance ().property ("AwayText").toString ());
		else if (mins == XmlSettingsManager::Instance ().property ("NATimeout").toInt ())
			status = EntryStatus (SXA,
					XmlSettingsManager::Instance ().property ("NAText").toString ());
		else
			return;

		for (QObject *accObj : AzothProxy_->GetAllAccounts ())
		{
			auto acc = qobject_cast<IAccount*> (accObj);

			const auto& accState = acc->GetState ();
			if (accState.State_ == State::SOffline)
				continue;

			if (!OldStatuses_.contains (accObj))
				OldStatuses_ [accObj] = accState;
			acc->ChangeState (status);
		}
	}
}
}
}

LC_EXPORT_PLUGIN (leechcraft_azoth_autoidler, LeechCraft::Azoth::Autoidler::Plugin);
