/**********************************************************************
 * 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)
 **********************************************************************/

#ifndef PLUGINS_AZOTH_PLUGINS_XOOX_ROOMHANDLER_H
#define PLUGINS_AZOTH_PLUGINS_XOOX_ROOMHANDLER_H
#include <QObject>
#include <QHash>
#include <interfaces/azoth/imucentry.h>
#include "clientconnection.h"
#include "roomparticipantentry.h"

class QXmppVCardIq;
class QXmppMucManager;
class QXmppMucRoom;

namespace LC
{
namespace Azoth
{
namespace Xoox
{
	class RoomCLEntry;
	class GlooxAccount;
	class RoomParticipantEntry;
	class FormBuilder;

	class RoomHandler : public QObject
	{
		Q_OBJECT

		GlooxAccount *Account_;
		QXmppMucManager *MUCManager_;
		const QString RoomJID_;
		QXmppMucRoom *Room_;
		RoomCLEntry *CLEntry_;

		QHash<QString, RoomParticipantEntry_ptr> Nick2Entry_;
		QString Subject_;
		// contains new nicks
		QSet<QString> PendingNickChanges_;
		bool HadRequestedPassword_;

		std::optional<bool> IsGateway_;

		bool VoluntaryLeave_ = false;
	public:
		RoomHandler (const QString& roomJID, const QString& ourNick,
				bool asAutojoin, GlooxAccount*);

		QString GetRoomJID () const;
		RoomCLEntry* GetCLEntry ();

		void SetPresence (QXmppPresence);

		QList<ICLEntry*> GetParticipants () const;
		QString GetSubject () const;
		void SetSubject (const QString&);
		void Join ();
		void Leave (const QString& msg);
		RoomParticipantEntry* GetSelf ();
		QString GetOurNick () const;
		void SetOurNick (const QString&);

		void SetAffiliation (const RoomParticipantEntry&, QXmppMucItem::Affiliation, const QString&);
		void SetRole (const RoomParticipantEntry&, QXmppMucItem::Role, const QString&);

		QXmppMucRoom* GetRoom () const;

		void HandleErrorPresence (const QXmppPresence&, const QString&);
		void HandleMessage (const QXmppMessage&, const QString&);

		RoomParticipantEntry_ptr FindParticipantEntry (const QString& nick) const;
		RoomParticipantEntry_ptr GetParticipantEntry (const QString& nick, bool announce = true);

		bool IsGateway () const;
	private slots:
		void handleParticipantAdded (const QString&);
		void handleParticipantChanged (const QString&);

		void requestVoice ();
	private:
		RoomParticipantEntry_ptr FindActor (const QString& actorId) const;

		void HandlePermsChange (RoomParticipantEntry&, const QXmppMucItem&) const;

		void HandlePrivateMessage (const QXmppMessage& msg, const QString& nick);
		void HandlePublicMessage (const QXmppMessage& msg, const QString& nick);

		void HandleSelfRemoved (const QXmppPresence& removalPresence);
		void HandleParticipantRemoved (const QString& nick, const QXmppPresence& removalPresence);

		void HandleMessageExtensions (const QXmppMessage&);
		void HandlePendingForm (std::unique_ptr<QXmppDataForm>, const QString&);

		void HandleRenameStart (const RoomParticipantEntry_ptr& entry,
				const QString& nick, const QString& newNick);

		void MakeNickChangeMessage (const QString&, const QString&);
		void HandleNickConflict ();
		void HandlePasswordRequired ();
		QString GetPassKey () const;

		void RemoveEntry (RoomParticipantEntry*);

		void SendLeave (const QString& reason);
		void RemoveParticipants ();
		void RemoveThis ();
	};
}
}
}

#endif
