/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2011 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 "entrybase.h"
#include <QAction>
#include <interfaces/azoth/iproxyobject.h>
#include <interfaces/azoth/azothutil.h>
#include "clientconnection.h"
#include "ircprotocol.h"
#include "ircaccount.h"
#include "ircmessage.h"
#include "vcarddialog.h"
#include "ircparticipantentry.h"

namespace LC
{
namespace Azoth
{
namespace Acetamide
{
	EntryBase::EntryBase (IrcAccount *account)
	: QObject (account)
	, Account_ (account)
	, VCardDialog_ (0)
	{
	}

	EntryBase::~EntryBase ()
	{
		qDeleteAll (AllMessages_);
		qDeleteAll (Actions_);
		delete VCardDialog_;
	}

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

	QList<IMessage*> EntryBase::GetAllMessages () const
	{
		return AllMessages_;
	}

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

	void EntryBase::SetChatPartState (ChatPartState, const QString&)
	{
	}

	EntryStatus EntryBase::GetStatus (const QString&) const
	{
		return CurrentStatus_;
	}

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

	void EntryBase::ShowInfo ()
	{
		IrcParticipantEntry *entry = qobject_cast<IrcParticipantEntry*> (this);
		if (!entry)
		{
			qWarning () << Q_FUNC_INFO
					<< this
					<< "is not an IrcParticipantEntry object";
			return;
		}

		if (!VCardDialog_)
			VCardDialog_ = new VCardDialog ();

		Account_->GetClientConnection ()->FetchVCard (entry->GetServerID (),
				entry->GetEntryName());
		VCardDialog_->show ();
	}

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

	void EntryBase::MarkMsgsRead ()
	{
		Core::Instance ().GetPluginProxy ()->MarkMessagesAsRead (this);
	}

	void EntryBase::ChatTabClosed ()
	{
		emit chatTabClosed ();
	}

	void EntryBase::HandleMessage (IrcMessage *msg)
	{
		msg->SetOtherPart (this);
		const auto proto = qobject_cast<IrcProtocol*> (Account_->GetParentProtocol ());
		const auto proxy = qobject_cast<IProxyObject*> (proto->GetProxyObject ());
		proxy->GetFormatterProxy ().PreprocessMessage (msg);

		AllMessages_ << msg;
		emit gotMessage (msg);
	}

	void EntryBase::SetStatus (const EntryStatus& status)
	{
		CurrentStatus_ = status;
		emit statusChanged (CurrentStatus_, QString ());
	}

	void EntryBase::SetAvatar (const QByteArray&)
	{
	}

	void EntryBase::SetAvatar (const QImage&)
	{
	}

	void EntryBase::SetRawInfo (const QString&)
	{
	}

	void EntryBase::SetInfo (const WhoIsMessage& msg)
	{
		if (VCardDialog_)
			VCardDialog_->UpdateInfo (msg);
	}
};
};
};
