// SPDX-FileCopyrightText: 2017 Linus Jahn <lnj@kaidan.im>
// SPDX-FileCopyrightText: 2020 Jonah Brüchert <jbb@kaidan.im>
// SPDX-FileCopyrightText: 2020 Melvin Keskin <melvo@olomono.de>
// SPDX-FileCopyrightText: 2020 caca hueto <cacahueto@olomono.de>
// SPDX-FileCopyrightText: 2022 Bhavy Airi <airiragahv@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

// Qt
#include <QDateTime>
#include <QObject>
// Kaidan
#include "Message.h"

// KDE
class KNotification;
// QXmpp
class QXmppPresence;
// Kaidan
class AccountSettings;
class AvatarCache;
class Call;
class ChatController;
class MessageController;
class RosterController;
struct RosterItem;

class NotificationController : public QObject
{
    Q_OBJECT

public:
    struct MessageNotificationWrapper {
        QString chatJid;
        QDateTime previousTimestamp;
        QString latestMessageId;
        QList<QString> messages;
        bool isDeletionEnabled = true;
        KNotification *notification = nullptr;
    };

    struct PresenceSubscriptionRequestNotificationWrapper {
        QString chatJid;
        KNotification *notification = nullptr;
    };

    NotificationController(AccountSettings *accountSettings,
                           AvatarCache *avatarCache,
                           RosterController *rosterController,
                           MessageController *messageController,
                           QObject *parent = nullptr);

    /**
     * Closes all chat message notifications of the same age or older than a timestamp.
     *
     * @param chatJid JID of the message's chat
     */
    void closeMessageNotification(const QString &chatJid);

    void closePresenceSubscriptionRequestNotification(const QString &chatJid);

    void sendCallNotification(Call *call);

    void setChatController(ChatController *chatController);

private:
    /**
     * Shows a notification for a message if needed.
     *
     * @param message message for whom a notification may be shown
     */
    void handleMessage(const Message &message, MessageOrigin origin);

    void handlePresenceSubscriptionRequestReceived(const QXmppPresence &request);

    /**
     * Sends a system notification for a chat message.
     *
     * @param chatJid JID of the message's chat
     * @param messageId ID of the message
     * @param messageBody body of the message to display as the notification's text
     */
    void sendMessageNotification(const QString &chatJid, const QString &messageId, const QString &messageBody);

    void sendPresenceSubscriptionRequestNotification(const QString &chatJid);

    bool checkChatActive(const QString &chatJid) const;
    QString determineChatName(const QString &chatJid) const;
    QPixmap retrieveAvatar(const QString &chatJid);
    void showChat(const QString &chatJid);

    void reply(const QString &message, const QString &messageId, const QString &body);
    void markAsRead(const RosterItem &rosterItem, const QString &messageId);

    AccountSettings *const m_accountSettings;
    AvatarCache *const m_avatarCache;
    MessageController *const m_messageController;

    ChatController *m_chatController = nullptr;

    QList<MessageNotificationWrapper> m_openMessageNotifications;
    QList<PresenceSubscriptionRequestNotificationWrapper> m_openPresenceSubscriptionRequestNotifications;
};
