/**********************************************************************
 * 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 "matchconfigdialog.h"
#include <interfaces/ianemitter.h>
#include <interfaces/core/icoreproxy.h>
#include <interfaces/core/ipluginsmanager.h>
#include "core.h"
#include "typedmatchers.h"
#include "fieldmatch.h"

namespace LeechCraft
{
namespace AdvancedNotifications
{
	MatchConfigDialog::MatchConfigDialog (const QStringList& types, QWidget *parent)
	: QDialog (parent)
	, Types_ (QSet<QString>::fromList (types))
	{
		Ui_.setupUi (this);

		const QObjectList& emitters = Core::Instance ().GetProxy ()->
				GetPluginsManager ()->GetAllCastableRoots<IANEmitter*> ();
		Q_FOREACH (QObject *pObj, emitters)
		{
			IInfo *ii = qobject_cast<IInfo*> (pObj);
			if (!ii)
			{
				qWarning () << Q_FUNC_INFO
						<< pObj
						<< "doesn't implement IInfo";
				continue;
			}

			Ui_.SourcePlugin_->addItem (ii->GetIcon (),
					ii->GetName (), ii->GetUniqueID ());
		}

		if (!emitters.isEmpty ())
			on_SourcePlugin__activated (0);
	}

	FieldMatch MatchConfigDialog::GetFieldMatch () const
	{
		const int fieldIdx = Ui_.FieldName_->currentIndex ();
		const int sourceIdx = Ui_.SourcePlugin_->currentIndex ();
		if (fieldIdx == -1 || sourceIdx == -1)
			return FieldMatch ();

		CurrentMatcher_->SyncToWidget ();

		const ANFieldData& data = Ui_.FieldName_->
				itemData (fieldIdx).value<ANFieldData> ();

		FieldMatch result (data.Type_, CurrentMatcher_);
		result.SetPluginID (Ui_.SourcePlugin_->
					itemData (sourceIdx).toByteArray ());
		result.SetFieldName (data.ID_);

		return result;
	}

	void MatchConfigDialog::on_SourcePlugin__activated (int idx)
	{
		Ui_.FieldName_->clear ();

		const QByteArray& id = Ui_.SourcePlugin_->
				itemData (idx).toByteArray ();
		QObject *pObj = Core::Instance ().GetProxy ()->
				GetPluginsManager ()->GetPluginByID (id);
		IANEmitter *iae = qobject_cast<IANEmitter*> (pObj);
		if (!iae)
		{
			qWarning () << Q_FUNC_INFO
					<< "plugin for ID"
					<< id
					<< "doesn't implement IANEmitter:"
					<< pObj;
			return;
		}

		const QList<ANFieldData> fields = iae->GetANFields ();
		Q_FOREACH (const ANFieldData& data, fields)
		{
			if (!Types_.isEmpty () &&
					QSet<QString>::fromList (data.EventTypes_).intersect (Types_).isEmpty ())
				continue;

			Ui_.FieldName_->addItem (data.Name_,
					QVariant::fromValue<ANFieldData> (data));
		}

		if (!fields.isEmpty ())
			on_FieldName__activated (0);
	}

	void MatchConfigDialog::on_FieldName__activated (int idx)
	{
		const ANFieldData& data = Ui_.FieldName_->
				itemData (idx).value<ANFieldData> ();
		Ui_.DescriptionLabel_->setText (data.Description_);

		QLayout *lay = Ui_.ConfigWidget_->layout ();
		QLayoutItem *oldItem = 0;
		while ((oldItem = lay->takeAt (0)) != 0)
		{
			if (oldItem->widget ())
				delete oldItem->widget ();
			delete oldItem;
		}

		CurrentMatcher_ = TypedMatcherBase::Create (data.Type_);
		if (CurrentMatcher_)
			lay->addWidget (CurrentMatcher_->GetConfigWidget ());
		else
			lay->addWidget (new QLabel (tr ("Invalid matcher type %1.")
						.arg (QVariant::typeToName (data.Type_))));
	}
}
}
