/**********************************************************************
 * 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 "privacylistsmanager.h"
#include <QDomElement>
#include <QXmppClient.h>

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	const QString NsPrivacy = "jabber:iq:privacy";

	PrivacyListItem::PrivacyListItem (const QString& value,
			PrivacyListItem::Type type, PrivacyListItem::Action action)
	: Value_ (value)
	, Type_ (type)
	, Action_ (action)
	, Stanzas_ (STAll)
	{
	}

	void PrivacyListItem::Parse (const QDomElement& itemElem)
	{
		const QString& type = itemElem.attribute ("type");
		if (type == "jid")
			Type_ = TJid;
		else if (type == "subscription")
			Type_ = TSubscription;
		else if (type == "group")
			Type_ = TGroup;
		else
			Type_ = TNone;

		Value_ = itemElem.attribute ("value");
		Action_ = itemElem.attribute ("action") == "deny" ?
				ADeny :
				AAllow;

		Stanzas_ = STNone;
		if (!itemElem.firstChildElement ("message").isNull ())
			Stanzas_ |= STMessage;
		if (!itemElem.firstChildElement ("presence-in").isNull ())
			Stanzas_ |= STPresenceIn;
		if (!itemElem.firstChildElement ("presence-out").isNull ())
			Stanzas_ |= STPresenceOut;
		if (!itemElem.firstChildElement ("iq").isNull ())
			Stanzas_ |= STIq;

		if (Stanzas_ == STNone)
			Stanzas_ = STAll;
	}

	QXmppElement PrivacyListItem::ToXML() const
	{
		QXmppElement item;
		item.setTagName ("item");

		switch (Type_)
		{
		case TJid:
			item.setAttribute ("type", "jid");
			break;
		case TSubscription:
			item.setAttribute ("type", "subscription");
			break;
		case TGroup:
			item.setAttribute ("type", "group");
			break;
		case TNone:
			break;
		}

		item.setAttribute ("action", Action_ == ADeny ? "deny" : "allow");

		if (!Value_.isEmpty ())
			item.setAttribute ("value", Value_);

		if (Stanzas_ != STAll)
		{
			if (Stanzas_ & STMessage)
			{
				QXmppElement elem;
				elem.setTagName ("message");
				item.appendChild (elem);
			}
			if (Stanzas_ & STPresenceIn)
			{
				QXmppElement elem;
				elem.setTagName ("presence-in");
				item.appendChild (elem);
			}
			if (Stanzas_ &STPresenceOut)
			{
				QXmppElement elem;
				elem.setTagName ("presence-out");
				item.appendChild (elem);
			}
			if (Stanzas_ & STIq)
			{
				QXmppElement elem;
				elem.setTagName ("iq");
				item.appendChild (elem);
			}
		}

		return item;
	}

	PrivacyListItem::Type PrivacyListItem::GetType () const
	{
		return Type_;
	}

	void PrivacyListItem::SetType (PrivacyListItem::Type type)
	{
		Type_ = type;
	}

	PrivacyListItem::Action PrivacyListItem::GetAction () const
	{
		return Action_;
	}

	void PrivacyListItem::SetAction (PrivacyListItem::Action action)
	{
		Action_ = action;
	}

	QString PrivacyListItem::GetValue () const
	{
		return Value_;
	}

	void PrivacyListItem::SetValue (const QString& value)
	{
		Value_ = value;
	}

	PrivacyListItem::StanzaTypes PrivacyListItem::GetStanzaTypes () const
	{
		return Stanzas_;
	}

	void PrivacyListItem::SetStanzaTypes (PrivacyListItem::StanzaTypes st)
	{
		Stanzas_ = st;
	}

	bool operator== (const PrivacyListItem& left, const PrivacyListItem& right)
	{
		return left.GetAction () == right.GetAction () &&
				left.GetType() == right.GetType () &&
				left.GetStanzaTypes () == right.GetStanzaTypes () &&
				left.GetValue () == right.GetValue ();
	}

	PrivacyList::PrivacyList (const QString& name)
	: Name_ (name)
	{
	}

	void PrivacyList::Parse (const QDomElement& list)
	{
		Name_ = list.attribute ("name");

		QMap<int, PrivacyListItem> items;

		QDomElement itemElem = list.firstChildElement ("item");
		while (!itemElem.isNull ())
		{
			PrivacyListItem item;
			item.Parse (itemElem);
			items [itemElem.attribute ("order").toInt ()] = item;

			itemElem = itemElem.nextSiblingElement ("item");
		}

		SetItems (items.values ());
	}

	QXmppElement PrivacyList::ToXML () const
	{
		QXmppElement listElem;
		listElem.setTagName ("list");
		listElem.setAttribute ("name", Name_);

		int i = 1;
		Q_FOREACH (const PrivacyListItem& item, Items_)
		{
			QXmppElement itemElem = item.ToXML ();
			itemElem.setAttribute ("order", QString::number (i++));
			listElem.appendChild (itemElem);
		}

		return listElem;
	}

	QString PrivacyList::GetName () const
	{
		return Name_;
	}

	void PrivacyList::SetName (const QString& name)
	{
		Name_ = name;
	}

	QList<PrivacyListItem> PrivacyList::GetItems () const
	{
		return Items_;
	}

	void PrivacyList::SetItems (const QList<PrivacyListItem>& items)
	{
		Items_ = items;
	}

	void PrivacyListsManager::QueryLists ()
	{
		QXmppElement query;
		query.setTagName ("query");
		query.setAttribute ("xmlns", NsPrivacy);

		QXmppIq iq;
		iq.setExtensions (QXmppElementList () << query);

		ID2Type_ [iq.id ()] = QTQueryLists;

		client ()->sendPacket (iq);
	}

	void PrivacyListsManager::QueryList (const QString& name)
	{
		QXmppElement list;
		list.setTagName ("list");
		list.setAttribute ("name", name);

		QXmppElement query;
		query.setTagName ("query");
		query.setAttribute ("xmlns", NsPrivacy);
		query.appendChild (list);

		QXmppIq iq;
		iq.setExtensions (QXmppElementList () << query);

		ID2Type_ [iq.id ()] = QTGetList;

		client ()->sendPacket (iq);
	}

	void PrivacyListsManager::ActivateList (const QString& name, ListType type)
	{
		QXmppElement list;
		list.setTagName (type == LTActive ? "active" : "default");
		if (!name.isEmpty ())
			list.setAttribute ("name", name);

		QXmppElement query;
		query.setTagName ("query");
		query.setAttribute ("xmlns", NsPrivacy);
		query.appendChild (list);

		QXmppIq iq (QXmppIq::Set);
		iq.setExtensions (QXmppElementList () << query);

		client ()->sendPacket (iq);

		CurrentName_ = name;
		QueryList (name);
	}

	void PrivacyListsManager::SetList (const PrivacyList& list)
	{
		QXmppElement query;
		query.setTagName ("query");
		query.setAttribute ("xmlns", NsPrivacy);
		query.appendChild (list.ToXML ());

		QXmppIq iq (QXmppIq::Set);
		iq.setExtensions (QXmppElementList () << query);

		client ()->sendPacket (iq);

		if (list.GetName () == CurrentName_)
		{
			CurrentList_ = list;
			emit currentListFetched (list);
		}
	}

	const PrivacyList& PrivacyListsManager::GetCurrentList () const
	{
		return CurrentList_;
	}

	QStringList PrivacyListsManager::discoveryFeatures () const
	{
		return QStringList (NsPrivacy);
	}

	bool PrivacyListsManager::handleStanza (const QDomElement& elem)
	{
		if (elem.tagName () != "iq")
			return false;

		if (elem.attribute ("type") == "set" &&
				elem.firstChildElement ("query").namespaceURI () == NsPrivacy)
		{
			QXmppIq iq (QXmppIq::Result);
			iq.setId (elem.attribute ("id"));
			client ()->sendPacket (iq);
			return true;
		}

		if (!ID2Type_.contains (elem.attribute ("id")))
			return false;

		switch (ID2Type_ [elem.attribute ("id")])
		{
		case QTQueryLists:
			HandleListQueryResult (elem);
			break;
		case QTGetList:
			HandleList (elem);
			break;
		}

		return true;
	}

	void PrivacyListsManager::HandleListQueryResult (const QDomElement& elem)
	{
		const QDomElement& query = elem.firstChildElement ("query");
		const QString& active = query.firstChildElement ("active").attribute ("name");
		const QString& def = query.firstChildElement ("default").attribute ("name");

		QStringList lists;
		QDomElement listElem = query.firstChildElement ("list");
		while (!listElem.isNull ())
		{
			lists << listElem.attribute ("name");
			listElem = listElem.nextSiblingElement ("list");
		}

		CurrentName_ = active.isEmpty () ? def : active;;

		emit gotLists (lists, active, def);

		QueryList (CurrentName_);
	}

	void PrivacyListsManager::HandleList (const QDomElement& elem)
	{
		const QDomElement& query = elem.firstChildElement ("query");

		PrivacyList list;
		list.Parse (query.firstChildElement ("list"));

		if (list.GetName () == CurrentName_)
		{
			CurrentList_ = list;
			currentListFetched (list);
		}

		emit gotList (list);
	}
}
}
}
