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

#include "xep0313prefiq.h"
#include "xep0313manager.h"
#include <QDomElement>
#include <QtDebug>

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	Xep0313PrefIq::Xep0313PrefIq (QXmppIq::Type type)
	: QXmppIq { type }
	{
	}

	QStringList Xep0313PrefIq::GetAllowed () const
	{
		return Allowed_;
	}

	void Xep0313PrefIq::SetAllowed (const QStringList& allowed)
	{
		Allowed_ = allowed;
	}

	QStringList Xep0313PrefIq::GetForbidden () const
	{
		return Forbidden_;
	}

	void Xep0313PrefIq::SetForbidden (const QStringList& forbidden)
	{
		Forbidden_ = forbidden;
	}

	Xep0313PrefIq::DefaultPolicy Xep0313PrefIq::GetDefaultPolicy () const
	{
		return Policy_;
	}

	void Xep0313PrefIq::SetDefaultPolicy (Xep0313PrefIq::DefaultPolicy policy)
	{
		Policy_ = policy;
	}

	namespace
	{
		Xep0313PrefIq::DefaultPolicy PolicyStr2Policy (const QString& str)
		{
			if (str == "always")
				return Xep0313PrefIq::DefaultPolicy::Always;
			else if (str == "never")
				return Xep0313PrefIq::DefaultPolicy::Never;
			else if (str == "roster")
				return Xep0313PrefIq::DefaultPolicy::Roster;
			else
			{
				qWarning () << Q_FUNC_INFO
						<< "unknown policy"
						<< str;
				return Xep0313PrefIq::DefaultPolicy::Roster;
			}
		}
	}

	void Xep0313PrefIq::parseElementFromChild (const QDomElement& element)
	{
		QXmppIq::parseElementFromChild (element);

		const auto& prefs = element.firstChildElement ("prefs");
		if (prefs.isNull ())
			return;

		Policy_ = PolicyStr2Policy (prefs.attribute ("default").toLower ());

		auto fill = [&prefs] (QStringList& list, const QString& tagName) -> void
		{
			list.clear ();

			auto jid = prefs.firstChildElement (tagName).firstChildElement ("jid");
			while (!jid.isNull ())
			{
				list << jid.text ();
				jid = jid.nextSiblingElement ("jid");
			}
		};

		fill (Allowed_, "always");
		fill (Forbidden_, "never");
	}

	namespace
	{
		QString Policy2Str (const Xep0313PrefIq::DefaultPolicy policy)
		{
			switch (policy)
			{
			case Xep0313PrefIq::DefaultPolicy::Always:
				return "always";
			case Xep0313PrefIq::DefaultPolicy::Never:
				return "never";
			case Xep0313PrefIq::DefaultPolicy::Roster:
				return "roster";
			}

			qWarning () << Q_FUNC_INFO
					<< "unknown policy"
					<< static_cast<int> (policy);
			return "roster";
		}
	}

	void Xep0313PrefIq::toXmlElementFromChild (QXmlStreamWriter *writer) const
	{
		QXmppIq::toXmlElementFromChild (writer);

		writer->writeStartElement ("prefs");
		writer->writeAttribute ("xmlns", Xep0313Manager::GetNsUri ());

		if (type () != QXmppIq::Get)
		{
			writer->writeAttribute ("default", Policy2Str (Policy_));

			auto dump = [writer] (const QStringList& list, const QString& tagName) -> void
			{
				writer->writeStartElement (tagName);

				for (const auto& jid : list)
					writer->writeTextElement ("jid", jid);

				writer->writeEndElement ();
			};

			dump (Allowed_, "always");
			dump (Forbidden_, "never");
		}

		writer->writeEndElement ();
	}
}
}
}
