/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "toxcontact.h"
#include <QImage>
#include <util/threads/coro.h>
#include <interfaces/azoth/azothutil.h>
#include "toxaccount.h"
#include "chatmessage.h"
#include "util.h"

namespace LC::Azoth::Sarin
{
	ToxContact::ToxContact (Pubkey pkey, ToxAccount *account)
	: QObject { account }
	, Pubkey_ { pkey }
	, EntryId_ { account->GetAccountID () + '/' + ToxId2HR (pkey) }
	, Acc_ { account }
	{
	}

	Pubkey ToxContact::GetPubKey () const
	{
		return Pubkey_;
	}

	QObject* ToxContact::GetQObject ()
	{
		return this;
	}

	IAccount* ToxContact::GetParentAccount () const
	{
		return Acc_;
	}

	ICLEntry::Features ToxContact::GetEntryFeatures () const
	{
		return FPermanentEntry;
	}

	ICLEntry::EntryType ToxContact::GetEntryType () const
	{
		return EntryType::Chat;
	}

	QString ToxContact::GetEntryName () const
	{
		return PublicName_.isEmpty () ? ToxId2HR (Pubkey_) : PublicName_;
	}

	void ToxContact::SetEntryName (const QString& name)
	{
		if (name == PublicName_)
			return;

		PublicName_ = name;
		emit Emitter_.nameChanged (GetEntryName ());
	}

	QString ToxContact::GetEntryID () const
	{
		return EntryId_;
	}

	QString ToxContact::GetHumanReadableID () const
	{
		return ToxId2HR (Pubkey_);
	}

	QStringList ToxContact::Groups () const
	{
		return {};
	}

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

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

	void ToxContact::SendMessage (const OutgoingMessage& message)
	{
		const auto msg = new ChatMessage { message.Body_, IMessage::Direction::Out, this };
		Acc_->SendMessage (Pubkey_, msg);
		HandleMessage (msg);
	}

	QList<IMessage*> ToxContact::GetAllMessages () const
	{
		return { AllMessages_.begin (), AllMessages_.end () };
	}

	void ToxContact::PurgeMessages (const QDateTime& before)
	{
		AzothUtil::StandardPurgeMessages (AllMessages_, before);
	}

	void ToxContact::SetChatPartState (ChatPartState state, const QString&)
	{
		Acc_->SetTypingState (Pubkey_, state == CPSComposing);
	}

	EntryStatus ToxContact::GetStatus (const QString&) const
	{
		return Status_;
	}

	void ToxContact::ShowInfo ()
	{
	}

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

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

	void ToxContact::MarkMsgsRead ()
	{
	}

	void ToxContact::ChatTabClosed ()
	{
	}

	void ToxContact::SetStatus (const EntryStatus& status)
	{
		if (status == Status_)
			return;

		Status_ = status;
		emit Emitter_.statusChanged (Status_, Variants ().front ());
	}

	void ToxContact::SetTyping (bool isTyping)
	{
		emit Emitter_.chatPartStateChanged (isTyping ? ChatPartState::CPSComposing : ChatPartState::CPSNone, {});
	}

	void ToxContact::HandleMessage (ChatMessage *msg)
	{
		AllMessages_ << msg;
		emit Emitter_.gotMessage (msg);
	}
}
