/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2011  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 "serverinfowidget.h"
#include <boost/bind.hpp>
#include "ircserverclentry.h"
#include <QtDebug>

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	ServerInfoWidget::ServerInfoWidget (IrcServerCLEntry *isEntry , QWidget *parent)
	: QWidget (parent)
	, ISCLEntry_ (isEntry)
	{
		Ui_.setupUi (this);
		Init ();
		SetISupport ();
	}

	void ServerInfoWidget::Init ()
	{
		Parameter2Command_ ["casemapping"] = boost::bind (&QLineEdit::setText,
				Ui_.CaseMapping_, _1);
		Parameter2Command_ ["chanlimit"] = boost::bind (&QLineEdit::setText,
				Ui_.ChanLimit_, _1);
		Parameter2Command_ ["chanmodes"] = boost::bind (&ServerInfoWidget::SetChanModes,
				this, _1);
		Parameter2Command_ ["channellen"] = boost::bind (&QLineEdit::setText,
				Ui_.ChannelLen_, _1);
		Parameter2Command_ ["chantypes"] = boost::bind (&QLineEdit::setText,
				Ui_.ChanTypes_, _1);
		Parameter2Command_ ["excepts"] = boost::bind (&ServerInfoWidget::SetExcepts,
				this, _1);
		Parameter2Command_ ["idchan"] = boost::bind (&QLineEdit::setText,
				Ui_.IdChan_, _1);
		Parameter2Command_ ["kicklen"] = boost::bind (&QLineEdit::setText,
				Ui_.KickLen_, _1);
		Parameter2Command_ ["maxlist"] = boost::bind (&QLineEdit::setText,
				Ui_.MaxList_, _1);
		Parameter2Command_ ["modes"] = boost::bind (&QLineEdit::setText,
				Ui_.Modes_, _1);
		Parameter2Command_ ["network"] = boost::bind (&QLineEdit::setText,
				Ui_.NetworkName_, _1);
		Parameter2Command_ ["nicklen"] = boost::bind (&QLineEdit::setText,
				Ui_.NickLength_, _1);
		Parameter2Command_ ["prefix"] = boost::bind (&ServerInfoWidget::SetPrefix,
				this, _1);
		Parameter2Command_ ["safelist"] = boost::bind (&ServerInfoWidget::SetSafeList,
				this, _1);
		Parameter2Command_ ["statusmsg"] = boost::bind (&QLineEdit::setText,
				Ui_.StatusMsg_, _1);
		Parameter2Command_ ["std"] = boost::bind (&QLineEdit::setText,
				Ui_.Std_, _1);
		Parameter2Command_ ["targmax"] = boost::bind (&ServerInfoWidget::SetTargMax,
				this, _1);
		Parameter2Command_ ["topiclen"] = boost::bind (&QLineEdit::setText,
				Ui_.TopicLen_, _1);
		Parameter2Command_ ["invex"] = boost::bind (&ServerInfoWidget::SetInvEx,
				this, _1);
	}

	void ServerInfoWidget::SetISupport ()
	{
		const QMap<QString, QString>& info = ISCLEntry_->GetISupport ();

		for (QMap<QString, QString>::const_iterator it_begin = info.begin (),
				it_end = info.end (); it_begin != it_end; ++it_begin)
			if (Parameter2Command_.contains (it_begin.key ().toLower ()))
				Parameter2Command_ [it_begin.key ().toLower ()] (it_begin.value ());
	}

	void ServerInfoWidget::SetChanModes (const QString& modes)
	{
		const QStringList& list = modes.split (',');

		Ui_.ChanModesA_->setText (list.at (0));
		Ui_.ChanModesB_->setText (list.at (1));
		Ui_.ChanModesC_->setText (list.at (2));
		Ui_.ChanModesD_->setText (list.at (3));
	}

	void ServerInfoWidget::SetExcepts (const QString& str)
	{
		Ui_.Excepts_->setChecked (GetBoolFromString (str));
	}

	void ServerInfoWidget::SetPrefix (const QString& str)
	{
		const int mode_end = str.indexOf (')');
		const QString& modeStr = str.mid (1, mode_end - 1);
		const QString& prefixStr = str.mid (mode_end + 1);
		const int rowCount = qMin (modeStr.length (), prefixStr.length ());

		if (modeStr.length () != prefixStr.length ())
			qWarning () << "number of modes is not equal to number of prefixes";

		Ui_.tableWidget->clear ();
		Ui_.tableWidget->setRowCount (rowCount);

		for (int i = 0; i < rowCount; ++i)
		{
			QTableWidgetItem *newMode = new QTableWidgetItem (modeStr [i]);
			QTableWidgetItem *newPrefix = new QTableWidgetItem (prefixStr [i]);
			Ui_.tableWidget->setItem (i, 0, newMode);
			Ui_.tableWidget->setItem (i, 1, newPrefix);
		}
	}

	void ServerInfoWidget::SetSafeList (const QString& str)
	{
		Ui_.SafeList_->setChecked (GetBoolFromString (str));
	}

	void ServerInfoWidget::SetTargMax (const QString& str)
	{
		const QStringList& list = str.split (',');

		Ui_.TargetMax_->clear ();
		Ui_.TargetMax_->setRowCount (list.count ());

		int row = 0;
		Q_FOREACH (const QString& param, list)
		{
			const int index = param.indexOf (':');
			QTableWidgetItem *target = new QTableWidgetItem (param.mid (0, index));
			QTableWidgetItem *count = new QTableWidgetItem (param.mid (index + 1));
			Ui_.TargetMax_->setItem (row, 0, target);
			Ui_.TargetMax_->setItem (row, 1, count);
			++row;
		}
	}

	void ServerInfoWidget::SetInvEx (const QString& str)
	{
		Ui_.InvEx_->setChecked (GetBoolFromString (str));
	}

	bool ServerInfoWidget::GetBoolFromString (const QString& str)
	{
		return str == "true";
	}

	void ServerInfoWidget::accept ()
	{
	}
}
}
}
