/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2012  Oleg Linkin
 *
 * 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 "ljfriendentry.h"
#include <QDataStream>
#include <QtDebug>

namespace LeechCraft
{
namespace Blogique
{
namespace Metida
{
	LJFriendEntry::LJFriendEntry (QObject *parent)
	: QObject (parent)
	, GroupMask_ (0)
	, FriendOf_ (false)
	, MyFriend_ (false)
	{
	}

	void LJFriendEntry::SetAvatarUrl (const QUrl& url)
	{
		AvatarUrl_ = url;
	}

	QUrl LJFriendEntry::GetAvatarurl () const
	{
		return AvatarUrl_;
	}

	void LJFriendEntry::SetFullName (const QString& fullName)
	{
		FullName_ = fullName;
	}

	QString LJFriendEntry::GetFullName () const
	{
		return FullName_;
	}

	void LJFriendEntry::SetUserName (const QString& userName)
	{
		UserName_ = userName;
	}

	QString LJFriendEntry::GetUserName () const
	{
		return UserName_;
	}

	void LJFriendEntry::SetGroupMask (int groupmask)
	{
		GroupMask_ = groupmask;
	}

	uint LJFriendEntry::GetGroupMask () const
	{
		return GroupMask_;
	}

	void LJFriendEntry::SetBGColor (const QString& name)
	{
		BGColor_.setNamedColor (name);
	}

	QColor LJFriendEntry::GetBGColor () const
	{
		return BGColor_;
	}

	void LJFriendEntry::SetFGColor (const QString& name)
	{
		FGColor_.setNamedColor (name);
	}

	QColor LJFriendEntry::GetFGColor () const
	{
		return FGColor_;
	}

	void LJFriendEntry::SetBirthday (const QString& date)
	{
		Birthday_ = date;
	}

	QString LJFriendEntry::GetBirthday () const
	{
		return Birthday_;
	}

	void LJFriendEntry::SetFriendOf (bool friendOf)
	{
		FriendOf_ = friendOf;
	}

	bool LJFriendEntry::GetFriendOf () const
	{
		return FriendOf_;
	}

	void LJFriendEntry::SetMyFriend (bool myFriend)
	{
		MyFriend_ = myFriend;
	}

	bool LJFriendEntry::GetMyFriend () const
	{
		return MyFriend_;
	}

	QByteArray LJFriendEntry::Serialize () const
	{
		quint16 ver = 3;
		QByteArray result;
		{
			QDataStream ostr (&result, QIODevice::WriteOnly);
			ostr << ver
					<< UserName_
					<< FullName_
					<< AvatarUrl_
					<< BGColor_.name ()
					<< FGColor_.name ()
					<< GroupMask_
					<< Birthday_
					<< FriendOf_
					<< MyFriend_;
		}

		return result;
	}

	LJFriendEntry_ptr LJFriendEntry::Deserialize (const QByteArray& data)
	{
		quint16 ver;
		QDataStream in (data);
		in >> ver;

		if (ver < 1 ||
				ver > 3)
		{
			qWarning () << Q_FUNC_INFO
					<< "unknown version"
					<< ver;
			return 0;
		}

		LJFriendEntry_ptr result = std::make_shared<LJFriendEntry> ();
		QString bgColorName, fgColorName;
		in >> result->UserName_
				>> result->FullName_
				>> result->AvatarUrl_
				>> bgColorName
				>> fgColorName
				>> result->GroupMask_;
		result->BGColor_.setNamedColor (bgColorName);
		result->FGColor_.setNamedColor (fgColorName);

		if (ver >= 2)
			in >> result->Birthday_
					>> result->FriendOf_;

		if (ver == 3)
			in >> result->MyFriend_;

		return result;
	}

	bool LJFriendEntry::operator== (const LJFriendEntry& entry) const
	{
		return UserName_ == entry.UserName_;
	}
}
}
}
