/**********************************************************************
 * 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 <QString>
#include <QByteArray>
#include <QTextCodec>

namespace LeechCraft
{
namespace Azoth
{
namespace Vader
{
namespace Proto
{
	class UIDL
	{
		QByteArray ID_;
	public:
		UIDL ();
		UIDL (const UIDL&);
		explicit UIDL (const QByteArray&);

		UIDL& operator= (const QByteArray&);

		operator QByteArray () const;
	};

	QByteArray ToMRIM1251 (const QString&);
	QByteArray ToMRIM16 (const QString&);
	QByteArray ToMRIM (const QString&);
	QByteArray ToMRIM (const QByteArray&);
	QByteArray ToMRIM (const UIDL&);
	QByteArray ToMRIM (quint32);
	QByteArray ToMRIM (int);

	template<typename Enum>
	typename std::enable_if<std::is_enum<Enum>::value, QByteArray>::type ToMRIM (Enum e)
	{
		return ToMRIM (static_cast<quint32> (e));
	}

	QByteArray ToMRIM ();

	template<typename T, typename... Args>
	QByteArray ToMRIM (T t, Args... args)
	{
		return ToMRIM (t) + ToMRIM (args...);
	}

	struct EncoderProxy
	{
		QString Str_;

		EncoderProxy& operator= (const QByteArray& ba)
		{
			Str_ = QTextCodec::codecForName (GetCodecName ())->toUnicode (ba);
			return *this;
		}

		operator QString () const { return Str_; }
	protected:
		virtual QByteArray GetCodecName () = 0;
	};

	struct Str1251 : EncoderProxy
	{
	protected:
		QByteArray GetCodecName () { return "Windows-1251"; }
	};

	struct Str16 : EncoderProxy
	{
	protected:
		QByteArray GetCodecName () { return "UTF-16LE"; }
	};

	QString FromMRIM1251 (const QByteArray&);
	QString FromMRIM16 (const QByteArray&);
	void FromMRIM (QByteArray&, EncoderProxy&);
	inline void FromMRIM (QByteArray& ba, Str1251& str) { FromMRIM (ba, static_cast<EncoderProxy&> (str)); }
	inline void FromMRIM (QByteArray& ba, Str16& str){ FromMRIM (ba, static_cast<EncoderProxy&> (str)); }
	void FromMRIM (QByteArray&, UIDL&);
	void FromMRIM (QByteArray&, QByteArray&);
	void FromMRIM (QByteArray&, quint32&);
	void FromMRIM (QByteArray&);

	template<typename Enum>
	void FromMRIM (QByteArray& ba, Enum& out, typename std::enable_if<std::is_enum<Enum>::value, void>::type* = nullptr)
	{
		quint32 proxy = 0;
		FromMRIM (ba, proxy);
		out = static_cast<Enum> (proxy);
	}

	template<typename T, typename... Args>
	void FromMRIM (QByteArray& ba, T& u, Args&... args)
	{
		FromMRIM (ba, u);
		FromMRIM (ba, args...);
	}
}
}
}
}
