/**********************************************************************
 * 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 "acceptriexdialog.h"
#include <QStandardItemModel>
#include <interfaces/core/icoreproxy.h>
#include <interfaces/core/itagsmanager.h>
#include "interfaces/azoth/iclentry.h"
#include "core.h"

namespace LeechCraft
{
namespace Azoth
{
	AcceptRIEXDialog::AcceptRIEXDialog (const QList<RIEXItem>& items,
			QObject *entryObj, QString message, QWidget *parent)
	: QDialog (parent)
	, Model_ (new QStandardItemModel (this))
	{
		Ui_.setupUi (this);

		Model_->setHorizontalHeaderLabels ({ tr ("Action"), tr ("ID"), tr ("Name"), tr ("Groups") });

		for (const RIEXItem& item : items)
		{
			QList<QStandardItem*> row;

			QStandardItem *action = new QStandardItem;
			action->setCheckState (Qt::Checked);
			action->setCheckable (true);
			switch (item.Action_)
			{
			case RIEXItem::AAdd:
				action->setText (tr ("add"));
				break;
			case RIEXItem::ADelete:
				action->setText (tr ("delete"));
				break;
			case RIEXItem::AModify:
				action->setText (tr ("modify"));
				break;
			default:
				action->setText (tr ("(unknown)"));
				break;
			}

			action->setData (QVariant::fromValue<RIEXItem> (item));

			row << action;
			row << new QStandardItem (item.ID_);
			row << new QStandardItem (item.Nick_);
			row << new QStandardItem (item.Groups_.join ("; "));

			Model_->appendRow (row);
		}

		Ui_.ItemsTree_->setModel (Model_);

		ICLEntry *entry = qobject_cast<ICLEntry*> (entryObj);
		const QString& id = entry->GetEntryName ().isEmpty () ?
				entry->GetHumanReadableID () :
				entry->GetEntryName () + " (" + entry->GetHumanReadableID () + ")";

		const QString& text = message.isEmpty () ?
				tr ("%1 has suggested to modify your contact list:")
					.arg (id) :
				tr ("%1 has suggested to modify your contact list:\n%2")
					.arg (id)
					.arg (message);
		Ui_.MessageLabel_->setText (text);
	}

	QList<RIEXItem> AcceptRIEXDialog::GetSelectedItems () const
	{
		QList<RIEXItem> result;

		const auto itp = Core::Instance ().GetProxy ()->GetTagsManager ();
		for (int i = 0, size = Model_->rowCount (); i < size; ++i)
		{
			QStandardItem *item = Model_->item (i);
			if (item->checkState () != Qt::Checked)
				continue;

			auto riex = item->data ().value<RIEXItem> ();
			riex.Groups_ = itp->Split (Model_->item (i, Column::Groups)->text ());
			result << riex;
		}

		return result;
	}
}
}
