/*
    SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#pragma once

#include <QAbstractListModel>
#include <QDateTime>
#include <QWindow>

#include <memory>

#include "notification.h"
#include "notifications.h"
#include "server.h"

#include "notificationmanager_export.h"

namespace NotificationManager
{
class NOTIFICATIONMANAGER_EXPORT AbstractNotificationsModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(QWindow *window READ window WRITE setWindow NOTIFY windowChanged)

public:
    ~AbstractNotificationsModel() override;

    QDateTime lastRead() const;
    void setLastRead(const QDateTime &lastRead);

    QWindow *window() const;
    void setWindow(QWindow *window);

    QVariant data(const QModelIndex &index, int role) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role) override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QHash<int, QByteArray> roleNames() const override;

    virtual void expire(uint notificationId) = 0;
    virtual void close(uint notificationId) = 0;

    void configure(const QString &desktopEntry, const QString &notifyRcName, const QString &eventId);

    virtual void invokeDefaultAction(uint notificationId, Notifications::InvokeBehavior behavior) = 0;
    virtual void invokeAction(uint notificationId, const QString &actionName, Notifications::InvokeBehavior behavior) = 0;
    virtual void reply(uint notificationId, const QString &text, Notifications::InvokeBehavior behavior) = 0;

    void startTimeout(uint notificationId);
    void stopTimeout(uint notificationId);

    void clear(Notifications::ClearFlags flags);

Q_SIGNALS:
    void lastReadChanged();
    void windowChanged(QWindow *window);

protected:
    AbstractNotificationsModel();
    void onNotificationAdded(const Notification &notification);
    void onNotificationReplaced(uint replacedId, const Notification &notification);
    void onNotificationRemoved(uint notificationId, Server::CloseReason reason);

    void setupNotificationTimeout(const Notification &notification);
    const QList<Notification> &notifications();
    int rowOfNotification(uint id) const;

private:
    friend class NotificationTest;

    class Private;
    std::unique_ptr<Private> d;

    Q_DISABLE_COPY(AbstractNotificationsModel)
};

} // namespace NotificationManager
