// Squawk messenger. 
// Copyright (C) 2019  Yury Gubich <blue@macaw.me>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef APPLICATION_H
#define APPLICATION_H

#include <map>
#include <list>
#include <set>

#include <QObject>
#include <QDBusInterface>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>

#include "dialogqueue.h"
#include "core/squawk.h"

#include "ui/squawk.h"
#include "ui/models/roster.h"
#include "ui/widgets/conversation.h"

#include "shared/message.h"
#include "shared/enums.h"

/**
 * @todo write docs
 */
class Application : public QObject
{
    Q_OBJECT
public:
    Application(Core::Squawk* core);
    ~Application();

    bool isConverstationOpened(const Models::Roster::ElId& id) const;

signals:
    void sendMessage(const QString& account, const Shared::Message& data);
    void replaceMessage(const QString& account, const QString& originalId, const Shared::Message& data);
    void resendMessage(const QString& account, const QString& jid, const QString& id);

    void changeState(Shared::Availability state);

    void setRoomJoined(const QString& account, const QString& jid, bool joined);
    void setRoomAutoJoin(const QString& account, const QString& jid, bool joined);
    void subscribeContact(const QString& account, const QString& jid, const QString& reason);
    void unsubscribeContact(const QString& account, const QString& jid, const QString& reason);
    void setEncryption(const QString& account, const QString& jid, Shared::EncryptionProtocol value);

    void quitting();
    void readyToQuit();

public slots:
    void readSettings();
    void quit();

protected slots:
    void notify(const QString& account, const Shared::Message& msg);
    void unreadMessagesCountChanged(int count);
    void setState(Shared::Availability availability);

    void changeAccount(const QString& account, const QMap<QString, QVariant>& data);
    void removeAccount(const QString& account);
    void openConversation(const Models::Roster::ElId& id, const QString& resource = "");

    void addGroup(const QString& account, const QString& name);

    void requestPassword(const QString& account, bool authenticationError);

    void writeSettings();

private slots:
    void onConversationClosed();
    void changeSubscription(const Models::Roster::ElId& id, bool subscribe);
    void onSquawkOpenedConversation();
    void onConversationMessage(const Shared::Message& msg);
    void onConversationReplaceMessage(const QString& originalId, const Shared::Message& msg);
    void onConversationResend(const QString& id);
    void stateChanged(Shared::Availability state);
    void onSquawkClosing();
    void onSquawkDestroyed();
    void onNotificationClosed(quint32 id, quint32 reason);
    void onNotificationInvoked(quint32 id, const QString& action);
    void onChangeTray(bool enabled, bool hide);
    void trayClicked(QSystemTrayIcon::ActivationReason reason);
    void toggleSquawk();
    void onItemExpanded(const QModelIndex& index);
    void onItemCollapsed(const QModelIndex& index);
    void onAddedElement(const std::list<QString>& path);
    void onConversationSetEncryption(Shared::EncryptionProtocol value);

private:
    void createMainWindow();
    void subscribeConversation(Conversation* conv);
    void checkForTheLastWindow();
    void focusConversation(const Models::Roster::ElId& id, const QString& resource = "", const QString& messageId = "");
    void createTrayIcon();

private:
    typedef std::map<Models::Roster::ElId, Conversation*> Conversations;
    typedef std::map<uint32_t, std::pair<Models::Roster::ElId, QString>> Notifications;

    Shared::Availability availability;
    Core::Squawk* core;
    Squawk* squawk;
    QDBusInterface notifications;
    Models::Roster roster;
    Conversations conversations;
    DialogQueue dialogueQueue;
    bool nowQuitting;
    bool destroyingSquawk;
    Notifications storage;
    QSystemTrayIcon* trayIcon;
    QAction actionQuit;
    QAction actionToggle;
    std::set<std::list<QString>> expandedPaths;
};

#endif // APPLICATION_H