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

#pragma once

#include <memory>
#include <QObject>
#include <interfaces/azoth/iaccount.h>
#include <interfaces/azoth/isupportmediacalls.h>
#include "toxaccountconfiguration.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Sarin
{
	class ToxProtocol;
	class ToxThread;
	class ToxContact;
	class ChatMessage;
	class MessagesManager;
	class AccountConfigDialog;
	class FileTransferManager;

	class ToxAccount : public QObject
					 , public IAccount
					 , public ISupportMediaCalls
	{
		Q_OBJECT
		Q_INTERFACES (LeechCraft::Azoth::IAccount LeechCraft::Azoth::ISupportMediaCalls)

		ToxProtocol * const Proto_;
		const QByteArray UID_;

		QString Name_;
		QString Nick_;

		ToxAccountConfiguration ToxConfig_;

		QByteArray ToxState_;

		QAction * const ActionGetToxId_;

		std::shared_ptr<ToxThread> Thread_;

		MessagesManager * const MsgsMgr_;
		FileTransferManager * const XferMgr_;

		QHash<QByteArray, ToxContact*> Contacts_;

		ToxAccount (const QByteArray&, const QString& name, ToxProtocol*);
	public:
		ToxAccount (const QString& name, ToxProtocol*);

		QByteArray Serialize ();
		static ToxAccount* Deserialize (const QByteArray&, ToxProtocol*);

		void SetNickname (const QString&);

		ToxContact* GetByAzothId (const QString&) const;
		ToxContact* GetByPubkey (const QByteArray&) const;

		QObject* GetQObject () override;
		QObject* GetParentProtocol () const override;
		AccountFeatures GetAccountFeatures () const override;

		QList<QObject*> GetCLEntries () override;

		QString GetAccountName () const override;
		QString GetOurNick () const override;
		void RenameAccount (const QString& name) override;
		QByteArray GetAccountID () const override;

		QList<QAction*> GetActions () const override;
		void OpenConfigurationDialog () override;
		EntryStatus GetState () const override;
		void ChangeState (const EntryStatus&) override;

		void Authorize (QObject*) override;
		void DenyAuth (QObject*) override;
		void RequestAuth (const QString&, const QString&, const QString&, const QStringList&) override;
		void RemoveEntry (QObject*) override;

		MediaCallFeatures GetMediaCallFeatures () const override;
		QObject* Call (const QString& id, const QString& variant) override;

		QObject* GetTransferManager () const override;

		void SendMessage (const QByteArray& pkey, ChatMessage *msg);
		void SetTypingState (const QByteArray& pkey, bool isTyping);
	private:
		void InitThread (const EntryStatus&);
		void InitEntry (const QByteArray&);

		void HandleConfigAccepted (AccountConfigDialog*);
	private slots:
		void handleThreadReady ();

		void handleIncomingCall (const QByteArray&, int32_t);

		void handleToxIdRequested ();
		void handleToxStateChanged (const QByteArray&);

		void handleGotFriend (qint32);
		void handleGotFriendRequest (const QByteArray&, const QByteArray&, const QString&);
		void handleRemovedFriend (const QByteArray&);

		void handleFriendNameChanged (const QByteArray&, const QString&);
		void handleFriendStatusChanged (const QByteArray&, const EntryStatus&);
		void handleFriendTypingChanged (const QByteArray&, bool);

		void handleInMessage (const QByteArray&, const QString&);
	signals:
		void accountRenamed (const QString&) override;
		void authorizationRequested (QObject*, const QString&) override;
		void gotCLItems (const QList<QObject*>&) override;
		void itemCancelledSubscription (QObject*, const QString&) override;
		void itemGrantedSubscription (QObject*, const QString&) override;
		void itemSubscribed (QObject*, const QString&) override;
		void itemUnsubscribed (const QString& entryID, const QString&) override;
		void itemUnsubscribed (QObject*, const QString&) override;
		void mucInvitationReceived (const QVariantMap&, const QString&, const QString&) override;
		void removedCLItems (const QList< QObject* >&) override;
		void statusChanged (const EntryStatus&) override;

		void accountChanged (ToxAccount*);

		void threadChanged (const std::shared_ptr<ToxThread>&);

		void called (QObject*) override;
	};
}
}
}
