/**********************************************************************
 * 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 "usercommandmanager.h"
#include <boost/bind.hpp>
#include "ircserverhandler.h"
#include "ircparser.h"
#include "ircaccount.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	UserCommandManager::UserCommandManager (IrcServerHandler *ish, 
			IrcParser *parser)
	: QObject (ish)
	, ISH_ (ish)
	, Parser_ (parser)
	{
		Init ();
	}

	QString UserCommandManager::VerifyMessage (const QString& msg,
			const QString& channelName)
	{
		const int pos = msg.indexOf (' ');
		QString cmd;
		if (msg.startsWith ('/'))
			cmd = msg.mid (1, pos).trimmed ().toLower ();
		else
			cmd = msg.left (pos).trimmed ().toLower ();

		if (!Command2Action_.contains (cmd))
			return QString ();

		QString message;
		QStringList messageList;
		if (pos != -1)
		{
			message = msg.mid (pos).trimmed ();
			messageList = message.split (' ');
		}

		if (cmd == "me")
		{
			messageList.insert (0, channelName);
			messageList.insert (1, "ACTION");
		}
		else if (cmd == "part" && message.isEmpty ())
			messageList << channelName; //TODO message for part
		else if (cmd == "join" && !message.isEmpty ())
		{
			QStringList channelList = messageList.first ().split (',');

			for (int i = 0; i < channelList.count (); ++i)
			{
				const QString& channel = channelList.at (i);
				if (!channel.startsWith ('#') &&
						!channel.startsWith ('+') &&
						!channel.startsWith ('&') &&
						!channel.startsWith ('!'))
					channelList [i].prepend ('#');
			}

			QString passwords;
			if (messageList.count () == 2)
				passwords = messageList.last ();
			messageList.clear ();
			messageList << channelList.join (",")
					<< passwords;
		}
		else if (cmd == "away")
		{
			ISH_->SetAway (messageList.join (" "));
			return cmd;
		}
// 		else if (cmd == "kick" && !message.isEmpty ())
// 		{
// 			if (ISH_->IsParticipantExists (messageList.first ()))
// 				messageList.insert (0, channelName);
// 		}
		else if (cmd == "say")
			messageList.insert (0, channelName);

		Command2Action_ [cmd] (messageList);
		return cmd;
	}

	void UserCommandManager::Init ()
	{
		Command2Action_ ["join"] = boost::bind (&IrcParser::JoinCommand,
				Parser_, _1);
		Command2Action_ ["part"] = boost::bind (&IrcParser::PartCommand,
				Parser_, _1);
		Command2Action_ ["quit"] = boost::bind (&IrcParser::QuitCommand,
				Parser_, _1);
		Command2Action_ ["privmsg"] = boost::bind (&IrcServerHandler::SendMessage,
				ISH_, _1);
		Command2Action_ ["msg"] = boost::bind (&IrcServerHandler::SendMessage,
				ISH_, _1);
		Command2Action_ ["nick"] = boost::bind (&IrcParser::NickCommand,
				Parser_, _1);
		Command2Action_ ["ping"] = boost::bind (&IrcParser::PingCommand,
				Parser_, _1);
		Command2Action_ ["pong"] = boost::bind (&IrcParser::PongCommand,
				Parser_, _1);
		Command2Action_ ["topic"] = boost::bind (&IrcParser::TopicCommand,
				Parser_, _1);
		Command2Action_ ["kick"] = boost::bind (&IrcParser::KickCommand,
				Parser_, _1);
		Command2Action_ ["invite"] = boost::bind (&IrcParser::InviteCommand,
				Parser_, _1);
		Command2Action_ ["ctcp"] = boost::bind (&IrcParser::CTCPRequest,
				Parser_, _1);
		Command2Action_ ["names"] = boost::bind (&IrcParser::NamesCommand,
				Parser_, _1);
		Command2Action_ ["away"] = boost::bind (&IrcParser::AwayCommand,
				Parser_, _1);
		Command2Action_ ["userhost"] = boost::bind (&IrcParser::UserhostCommand,
				Parser_, _1);
		Command2Action_ ["ison"] = boost::bind (&IrcParser::IsonCommand,
				Parser_, _1);
		Command2Action_ ["whois"] = boost::bind (&IrcParser::WhoisCommand,
				Parser_, _1);
		Command2Action_ ["whowas"] = boost::bind (&IrcParser::WhowasCommand,
				Parser_, _1);
		Command2Action_ ["who"] = boost::bind (&IrcParser::WhoCommand,
				Parser_, _1);
		Command2Action_ ["summon"] = boost::bind (&IrcParser::SummonCommand,
				Parser_, _1);
		Command2Action_ ["version"] = boost::bind (&IrcParser::VersionCommand,
				Parser_, _1);
		Command2Action_ ["links"] = boost::bind (&IrcParser::LinksCommand,
				Parser_, _1);
		Command2Action_ ["info"] = boost::bind (&IrcParser::InfoCommand,
				Parser_, _1);
		Command2Action_ ["motd"] = boost::bind (&IrcParser::MOTDCommand,
				Parser_, _1);
		Command2Action_ ["time"] = boost::bind (&IrcParser::TimeCommand,
				Parser_, _1);
		Command2Action_ ["oper"] = boost::bind (&IrcParser::OperCommand,
				Parser_, _1);
		Command2Action_ ["rehash"] = boost::bind (&IrcParser::RehashCommand,
				Parser_, _1);
		Command2Action_ ["lusers"] = boost::bind (&IrcParser::LusersCommand,
				Parser_, _1);
		Command2Action_ ["users"] = boost::bind (&IrcParser::UsersCommand,
				Parser_, _1);
		Command2Action_ ["wallops"] = boost::bind (&IrcParser::WallopsCommand,
				Parser_, _1);
		Command2Action_ ["quote"] = boost::bind (&IrcParser::RawCommand,
				Parser_, _1);
		Command2Action_ ["me"] = boost::bind (&IrcParser::CTCPRequest,
				Parser_, _1);
		Command2Action_ ["squit"] = boost::bind (&IrcParser::SQuitCommand,
				Parser_, _1);
		Command2Action_ ["stats"] = boost::bind (&IrcParser::StatsCommand,
				Parser_, _1);
		Command2Action_ ["connect"] = boost::bind (&IrcParser::ConnectCommand,
				Parser_, _1);
		Command2Action_ ["trace"] = boost::bind (&IrcParser::TraceCommand,
				Parser_, _1);
		Command2Action_ ["admin"] = boost::bind (&IrcParser::AdminCommand,
				Parser_, _1);
		Command2Action_ ["kill"] = boost::bind (&IrcParser::KillCommand,
				Parser_, _1);
		Command2Action_ ["die"] = boost::bind (&IrcParser::DieCommand,
				Parser_, _1);
		Command2Action_ ["restart"] = boost::bind (&IrcParser::RestartCommand,
				Parser_, _1);
		Command2Action_ ["mode"] = boost::bind (&IrcParser::ChanModeCommand,
				Parser_, _1);
		Command2Action_ ["say"] = boost::bind (&IrcServerHandler::SayCommand,
				ISH_, _1);
		Command2Action_ ["list"] = boost::bind (&IrcServerHandler::showChannels,
				ISH_, _1);
	}
}
}
}
