/**********************************************************************
 * 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 "useravatarmetadata.h"
#include <QBuffer>
#include <QCryptographicHash>
#include <QDomElement>
#include <QXmppElement.h>

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	const QString NsAvatarMetadataNode = "urn:xmpp:avatar:metadata";

	UserAvatarMetadata::UserAvatarMetadata (const QImage& image)
	: Width_ (image.width ())
	, Height_ (image.height ())
	, Type_ ("image/png")
	{
		QByteArray ba;
		{
			QBuffer buffer (&ba);
			buffer.open (QIODevice::WriteOnly);
			image.save (&buffer, "PNG", 0);
		}

		ID_ = QCryptographicHash::hash (ba, QCryptographicHash::Sha1).toHex ();

		Size_ = ba.size ();
	}

	QString UserAvatarMetadata::GetNodeString ()
	{
		return NsAvatarMetadataNode;
	}

	QXmppElement UserAvatarMetadata::ToXML () const
	{
		QXmppElement item;
		item.setTagName ("item");
		item.setAttribute ("id", ID_);

		QXmppElement md;
		md.setTagName ("metadata");
		md.setAttribute ("xmlns", NsAvatarMetadataNode);

		if (!IsNull ())
		{
			QXmppElement info;
			info.setTagName ("info");
			info.setAttribute ("bytes", QString::number (Size_));
			info.setAttribute ("width", QString::number (Width_));
			info.setAttribute ("height", QString::number (Height_));
			info.setAttribute ("type", Type_);
			info.setAttribute ("id", ID_);

			if (URL_.isValid ())
				info.setAttribute ("url", URL_.toEncoded ());

			md.appendChild (info);
		}

		item.appendChild (md);

		return item;
	}

	void UserAvatarMetadata::Parse (const QDomElement& elem)
	{
		const QDomElement& md = elem.firstChildElement ("metadata");
		const QDomElement& info = md.firstChildElement ("info");

		if (info.isNull ())
		{
			Size_ = 0;
			Width_ = 0;
			Height_ = 0;
			Type_ = QString ();
			ID_ = QByteArray ();
			URL_ = QUrl ();
		}
		else
		{
			Size_ = info.attribute ("bytes").toInt ();
			Width_ = info.attribute ("width").toInt ();
			Height_ = info.attribute ("height").toInt ();
			Type_ = info.attribute ("type");
			ID_ = info.attribute ("id").toLatin1 ();
			URL_ = QUrl::fromEncoded (info.attribute ("url").toLatin1 ());
		}
	}

	QString UserAvatarMetadata::Node () const
	{
		return NsAvatarMetadataNode;
	}

	PEPEventBase* UserAvatarMetadata::Clone () const
	{
		return new UserAvatarMetadata (*this);
	}

	bool UserAvatarMetadata::IsNull () const
	{
		return !(Size_ > 0 && Width_ > 0 && Height_ > 0);
	}

	QUrl UserAvatarMetadata::GetURL () const
	{
		return URL_;
	}

	QByteArray UserAvatarMetadata::GetID () const
	{
		return ID_;
	}
}
}
}
