/**********************************************************************
 * 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 "ircerrorhandler.h"
#include <QTextCodec>
#include <util/xpc/util.h>
#include <util/xpc/notificationactionhandler.h>
#include "core.h"
#include "ircserverhandler.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	IrcErrorHandler::IrcErrorHandler (QObject *parent)
	: QObject (parent)
	{
		InitErrors ();
	}

	void IrcErrorHandler::HandleError (const IrcMessageOptions& options)
	{
		if (!IsError (options.Command_.toInt ()))
			return;

		QString paramsMessage;

		if (options.Parameters_.count () > 1)
			for (const auto& str : options.Parameters_.mid (1))
				paramsMessage += QString::fromUtf8 (str.c_str ()) + " ";

		Entity e = Util::MakeNotification ("Azoth",
				paramsMessage.isEmpty () ?
						options.Message_ :
						(paramsMessage + ": " + options.Message_),
				PWarning_);
		Core::Instance ().SendEntity (e);
	}

	bool IrcErrorHandler::IsError (int id)
	{
		return ErrorKeys_.contains (id);
	}

	namespace
	{
		template<int B, int E>
		struct Range
		{
			static_assert (B <= E, "Invalid range");

			constexpr static int Begin = B;
			constexpr static int End = E;
		};

		template<int... Pts>
		struct Points
		{
		};

		template<typename T, typename... Args>
		struct Filler;

		template<typename T, int... Pts, typename... Args>
		struct Filler<T, Points<Pts...>, Args...>
		{
			void operator() (T& list) const
			{
				list += T { Pts... };

				Filler<T, Args...> {} (list);
			}
		};

		template<typename T, typename Arg, typename... Args>
		struct Filler<T, Arg, Args...>
		{
			void operator() (T& list) const
			{
				for (int i = Arg::Begin; i <= Arg::End; ++i)
					list << i;

				Filler<T, Args...> {} (list);
			}
		};

		template<typename T>
		struct Filler<T>
		{
			void operator() (T&) const
			{
			}
		};
	}

	void IrcErrorHandler::InitErrors ()
	{
		Filler<
				decltype (ErrorKeys_),
				Range<401, 409>,
				Range<411, 415>,
				Points<421, 422, 424>,
				Range<431, 433>,
				Range<436, 437>,
				Range<441, 446>,
				Points<451>,
				Range<461, 467>,
				Range<471, 478>,
				Range<481, 485>,
				Points<491, 501, 502>
			> {} (ErrorKeys_);
	}
}
}
}
