/**********************************************************************
 * 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 "vkchatentry.h"
#include <QStringList>
#include <QtDebug>
#include <interfaces/azoth/iproxyobject.h>
#include "vkaccount.h"
#include "vkentry.h"
#include "vkmessage.h"
#include "vkconnection.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Murm
{
	VkChatEntry::VkChatEntry (const ChatInfo& info, VkAccount *acc)
	: EntryBase (acc)
	, Info_ (info)
	{
		for (auto id : info.Users_)
			if (auto entry = acc->GetEntry (id))
				entry->RegisterIn (this);
	}

	void VkChatEntry::Send (VkMessage *msg)
	{
		Account_->Send (GetInfo ().ChatID_, VkConnection::MessageType::Chat, msg);
	}

	void VkChatEntry::HandleMessage (const MessageInfo& info)
	{
		const auto from = info.Params_ ["from"].toULongLong ();
		if (from == 0)
		{
			qWarning () << Q_FUNC_INFO
					<< "unknown from"
					<< info.Params_;
			return;
		}

		auto entry = Account_->GetEntry (from);
		if (!entry)
		{
			qWarning () << Q_FUNC_INFO
					<< "unknown entry for"
					<< from;
			return;
		}

		auto msg = new VkMessage (false, IMessage::DIn, IMessage::MTMUCMessage, this, entry);
		msg->SetBody (info.Text_);
		msg->SetDateTime (info.TS_);
		msg->SetID (info.ID_);

		HandleAttaches (msg, info);

		Store (msg);
	}

	const ChatInfo& VkChatEntry::GetInfo () const
	{
		return Info_;
	}

	void VkChatEntry::UpdateInfo (const ChatInfo& info)
	{
		for (auto id : info.Users_)
			if (!Info_.Users_.contains (id))
				HandleAdded (id);
		for (auto id : Info_.Users_)
			if (!info.Users_.contains (id))
				HandleRemoved (id);

		const bool titleChanged = info.Title_ != Info_.Title_;

		Info_ = info;
		if (titleChanged)
		{
			emit nameChanged (GetEntryName ());

			for (auto id : Info_.Users_)
				if (auto entry = Account_->GetEntry (id))
					entry->ReemitGroups ();
		}
	}

	void VkChatEntry::HandleAdded (qulonglong id)
	{
		if (auto entry = Account_->GetEntry (id))
			entry->RegisterIn (this);
	}

	void VkChatEntry::HandleRemoved (qulonglong id)
	{
		if (id == Account_->GetSelf ()->GetInfo ().ID_)
		{
			for (auto id : Info_.Users_)
				if (auto entry = Account_->GetEntry (id))
					entry->UnregisterIn (this);

			emit removeEntry (this);
		}
		else if (auto entry = Account_->GetEntry (id))
			entry->UnregisterIn (this);
	}

	ICLEntry::Features VkChatEntry::GetEntryFeatures () const
	{
		return FSessionEntry | FSupportsRenames;
	}

	ICLEntry::EntryType VkChatEntry::GetEntryType () const
	{
		return ETMUC;
	}

	QString VkChatEntry::GetEntryName () const
	{
		return Info_.Title_;
	}

	void VkChatEntry::SetEntryName (const QString& title)
	{
		Account_->GetConnection ()->SetChatTitle (Info_.ChatID_, title);
	}

	QString VkChatEntry::GetEntryID () const
	{
		return Account_->GetAccountID () + QString::number (Info_.ChatID_);
	}

	QString VkChatEntry::GetHumanReadableID () const
	{
		return QString::number (Info_.ChatID_);
	}

	QStringList VkChatEntry::Groups () const
	{
		return { tr ("Chats") };
	}

	void VkChatEntry::SetGroups (const QStringList&)
	{
	}

	QStringList VkChatEntry::Variants () const
	{
		return {};
	}

	void VkChatEntry::SetChatPartState (ChatPartState, const QString&)
	{
		// TODO
	}

	EntryStatus VkChatEntry::GetStatus (const QString&) const
	{
		return { SOnline, {} };
	}

	QImage VkChatEntry::GetAvatar () const
	{
		return {};
	}

	QString VkChatEntry::GetRawInfo () const
	{
		return {};
	}

	void VkChatEntry::ShowInfo ()
	{
	}

	QList<QAction*> VkChatEntry::GetActions () const
	{
		return {};
	}

	QMap<QString, QVariant> VkChatEntry::GetClientInfo (const QString&) const
	{
		return {};
	}

	void VkChatEntry::MarkMsgsRead ()
	{
		Account_->GetParentProtocol ()->GetAzothProxy ()->MarkMessagesAsRead (this);
	}

	void VkChatEntry::ChatTabClosed ()
	{
	}

	IMUCEntry::MUCFeatures VkChatEntry::GetMUCFeatures () const
	{
		return MUCFCanInvite;
	}

	QString VkChatEntry::GetMUCSubject () const
	{
		return {};
	}

	bool VkChatEntry::CanChangeSubject () const
	{
		return false;
	}

	void VkChatEntry::SetMUCSubject (const QString&)
	{
	}

	QList<QObject*> VkChatEntry::GetParticipants ()
	{
		QList<QObject*> result;
		for (const auto id : Info_.Users_)
			if (auto entry = Account_->GetEntry (id))
				result << entry;
		return result;
	}

	bool VkChatEntry::IsAutojoined () const
	{
		return false;
	}

	void VkChatEntry::Join ()
	{
	}

	void VkChatEntry::Leave (const QString&)
	{
		const auto selfId = Account_->GetSelf ()->GetInfo ().ID_;
		Account_->GetConnection ()->RemoveChatUser (Info_.ChatID_, selfId);
	}

	QString VkChatEntry::GetNick () const
	{
		return Account_->GetOurNick ();
	}

	void VkChatEntry::SetNick (const QString&)
	{
	}

	QString VkChatEntry::GetGroupName () const
	{
		return tr ("Participants of %1").arg (Info_.Title_);
	}

	QString VkChatEntry::GetRealID (QObject*) const
	{
		return {};
	}

	QVariantMap VkChatEntry::GetIdentifyingData () const
	{
		return {};
	}

	void VkChatEntry::InviteToMUC (const QString& userId, const QString&)
	{
		bool ok = false;
		const auto numericId = userId.toULongLong (&ok);

		if (!ok)
		{
			qWarning () << Q_FUNC_INFO
					<< "incorrect user id"
					<< userId;
			return;
		}

		Account_->GetConnection ()->AddChatUser (Info_.ChatID_, numericId);
	}
}
}
}
