/**********************************************************************
 * 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 <QMap>
#include "xpcconfig.h"
#include "interfaces/iinfo.h"
#include "interfaces/core/ihookproxy.h"

namespace LeechCraft
{
namespace Util
{
	/** @brief Standard implementation of IHookProxy.
	 *
	 * This class is the standard implementation of the IHookProxy
	 * interface and can be used in most cases.
	 *
	 * @sa IHookProxy
	 */
	class UTIL_XPC_API DefaultHookProxy : public IHookProxy
	{
		bool Cancelled_;
		QVariant ReturnValue_;

		QMap<QByteArray, QVariant> Name2NewVal_;
	public:
		/** @brief Creates a new hook proxy.
		 */
		DefaultHookProxy ();

		/** @brief Reimplemented from IHookProxy::CancelDefault().
		 *
		 * @sa IsCancelled()
		 */
		void CancelDefault ();

		/** @brief Returns whether the default implementation is canceled.
		 *
		 * This function returns whether CancelDefault() has been called
		 * at least once.
		 *
		 * @return Whether the default implementation is canceled.
		 *
		 * @sa CancelDefault()
		 */
		bool IsCancelled () const;

		/** @brief Reimplemented from IHookProxy::GetReturnValue().
		 */
		const QVariant& GetReturnValue () const;

		/** @brief Reimplemented from IHookProxy::SetReturnValue().
		 */
		void SetReturnValue (const QVariant&);

		/** @brief Fills the value of the given parameter set by SetValue().
		 *
		 * If SetValue() has been called with the given parameter
		 * \em name this function sets \em val to that value, otherwise
		 * it does nothing.
		 *
		 * @param[in] name The name of the parameter.
		 * @param[out] val The value to fill.
		 * @tparam T The type of the value, which should be known to Qt's
		 * metatypes system so that <code>QVariant::value<T>()</code> is valid.
		 */
		template<typename T>
		void FillValue (const QByteArray& name, T& val)
		{
			if (!Name2NewVal_.contains (name))
				return;

			const QVariant& newVal = Name2NewVal_ [name];
			if (!newVal.isValid ())
				return;

			val = newVal.value<T> ();
		}

		/** @brief Reimplemented from IHookProxy::GetValue().
		 */
		QVariant GetValue (const QByteArray&) const;

		/** @brief Reimplemented from IHookProxy::SetValue().
		 */
		void SetValue (const QByteArray&, const QVariant&);
	};

	typedef std::shared_ptr<DefaultHookProxy> DefaultHookProxy_ptr;
}
}
