/**********************************************************************
 * 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.
 **********************************************************************/

#pragma once

#include <tuple>
#include <type_traits>

namespace LeechCraft
{
namespace Aggregator
{
namespace WebAccess
{
	template<size_t...>
	struct Seq {};

	template<size_t N, size_t... S>
	struct Gen : Gen<N - 1, N - 1, S...> {};

	template<size_t... S>
	struct Gen<0, S...>
	{
		typedef Seq<S...> type;
	};

	template<typename F, typename Tuple, size_t... S>
	constexpr bool AppliableHelper (int, Seq<S...>, decltype ((*static_cast<F*> (nullptr)) (std::get<S> (Tuple {})...)) * = nullptr)
	{
		return true;
	}

	template<typename F, typename Tuple, size_t... S>
	constexpr bool AppliableHelper (char, Seq<S...>)
	{
		return false;
	}

	template<typename F, typename Tuple>
	struct Appliable
	{
		constexpr static bool value = AppliableHelper<F, Tuple> (1, typename Gen<std::tuple_size<Tuple>::value>::type {});
	};

	template<typename... Args>
	struct CutoffOne;

	template<typename Arg, typename Arg2, typename... Rest>
	struct CutoffOne<Arg, Arg2, Rest...>
	{
		typedef decltype (std::tuple_cat (std::tuple<typename std::decay<Arg>::type> { Arg {} }, typename CutoffOne<Arg2, Rest...>::type {})) type;
	};

	template<typename Last>
	struct CutoffOne<Last>
	{
		typedef std::tuple<> type;
	};

	template<>
	struct CutoffOne<>
	{
		typedef std::tuple<> type;
	};

	template<template<typename... Args> class MetaF, typename Tuple, size_t... S>
	MetaF<decltype (std::get<S> (Tuple {}))...> PerformWithTupleTypesImpl (Seq<S...>)
	{
		return {};
	}

	template<template<typename... Args> class MetaF, typename Tuple>
	struct PerformWithTupleTypes
	{
		typedef typename decltype (PerformWithTupleTypesImpl<MetaF, Tuple> (typename Gen<std::tuple_size<Tuple>::value>::type {}))::type type;
	};

	template<typename F, typename ArgsTuple>
	struct FArgCount
	{
		constexpr static size_t value ()
		{
			return Appliable<F, ArgsTuple>::value ?
					std::tuple_size<ArgsTuple>::value :
						(std::tuple_size<ArgsTuple>::value ? FArgCount<F, typename PerformWithTupleTypes<CutoffOne, ArgsTuple>::type>::value () : 0);
		}
	};

	template<typename F>
	struct WFImpl
	{
		F F_;

		template<typename Tuple, size_t... S>
		void CallHelper (const Tuple& tuple, Seq<S...>)
		{
			F_ (std::get<S> (tuple)...);
		}

		template<typename... Args>
		void operator() (Args... args)
		{
			constexpr auto argCount = FArgCount<F, std::tuple<Args...>>::value ();
			CallHelper (std::make_tuple (args...), typename Gen<argCount>::type {});
		}
	};

	template<typename F>
	WFImpl<F> WF (const F& f)
	{
		return { f };
	}
}
}
}
