/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2013  Oleg Linkin
 *
 * 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 "channelslistdialog.h"
#include <QTimer>
#include <QStandardItemModel>
#include "channelslistfilterproxymodel.h"
#include "ircserverhandler.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	ChannelsListDialog::ChannelsListDialog (IrcServerHandler* ish, QWidget* parent)
	: QDialog (parent)
	, ISH_ (ish)
	, BufferTimer_ (new QTimer (this))
	, FilterProxyModel_ (new ChannelsListFilterProxyModel (this))
	, Model_ (new QStandardItemModel (this))
	{
		Ui_.setupUi (this);

		Model_->setHorizontalHeaderLabels ({ tr ("Name"), tr ("Users count"), tr ("Topic") });
		FilterProxyModel_->setSourceModel (Model_);
		Ui_.ChannelsList_->setModel (FilterProxyModel_);
		Ui_.ChannelsList_->setColumnWidth (ChannelName, 200);
		Ui_.ChannelsList_->setColumnWidth (ParticipantsCount, 50);
		Ui_.ChannelsList_->header ()->setStretchLastSection (true);

		connect (BufferTimer_,
				SIGNAL (timeout ()),
				this,
				SLOT (appendRows ()));
	}

	void ChannelsListDialog::handleGotChannelsBegin ()
	{
		Model_->removeRows (0, Model_->rowCount ());
		BufferTimer_->start (1000);
	}

	void ChannelsListDialog::handleGotChannels (const ChannelsDiscoverInfo& info)
	{
		QStandardItem *name = new QStandardItem (info.ChannelName_);
		name->setEditable (false);
		QStandardItem *count = new QStandardItem (QString::number (info.UsersCount_));
		count->setEditable (false);
		QStandardItem *topic = new QStandardItem (info.Topic_);
		topic->setEditable (false);
		Buffer_.append ({ name, count, topic });
	}

	void ChannelsListDialog::handleGotChannelsEnd ()
	{
		BufferTimer_->stop ();
	}

	void ChannelsListDialog::appendRows ()
	{
		auto list = Buffer_;
		Buffer_.clear ();

		for (const auto& row : list)
			Model_->appendRow (row);
	}

	void ChannelsListDialog::on_Filter__textChanged (const QString& text)
	{
		FilterProxyModel_->setFilterRegExp (text);
	}

	void ChannelsListDialog::on_ChannelsList__doubleClicked (const QModelIndex& index)
	{
		if (!index.isValid ())
			return;

		QModelIndex idx = index.sibling (index.row (), 0);
		ChannelOptions opts;
		opts.ChannelName_ = idx.data ().toString ();
		opts.ServerName_ = ISH_->GetServerOptions ().ServerName_;
		ISH_->JoinChannel (opts);
	}
}
}
}
