// SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL

#pragma once

#include <QQmlEngine>
#include <QSortFilterProxyModel>

#include "models/timelinemessagemodel.h"

/**
 * @class MessageFilterModel
 *
 * This model filters out any messages that should be hidden.
 *
 * Deleted messages are only hidden if the user hasn't set them to be shown.
 *
 * The model also contains the roles and functions to support aggregating multiple
 * consecutive state events into a single delegate. The state events must all happen
 * on the same day to be aggregated.
 */
class MessageFilterModel : public QSortFilterProxyModel
{
    Q_OBJECT
    QML_ELEMENT

    /**
     * @brief The model index of the read marker.
     */
    Q_PROPERTY(QPersistentModelIndex readMarkerIndex READ readMarkerIndex NOTIFY readMarkerIndexChanged)

    /**
     * @brief The number of selected messages.
     */
    Q_PROPERTY(int selectedMessageCount READ selectedMessageCount NOTIFY selectionChanged)

public:
    /**
     * @brief Defines the model roles.
     */
    enum Roles {
        AggregateDisplayRole = TimelineMessageModel::LastRole + 1, /**< Single line aggregation of all the state events. */
        StateEventsRole, /**< List of state events in the aggregated state. */
        AuthorListRole, /**< List of the first 5 unique authors of the aggregated state event. */
        ExcessAuthorsRole, /**< The number of unique authors beyond the first 5. */
        LastRole, // Keep this last
    };

    QPersistentModelIndex readMarkerIndex() const;

    explicit MessageFilterModel(QObject *parent = nullptr, QAbstractItemModel *sourceModel = nullptr);

    /**
     * @brief Custom filter function to remove hidden messages.
     */
    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;

    /**
     * @brief Get the given role value at the given index.
     *
     * @sa QSortFilterProxyModel::data
     */
    [[nodiscard]] QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override;

    /**
     * @brief Returns a mapping from Role enum values to role names.
     *
     * @sa Roles, QAbstractProxyModel::roleNames()
     */
    [[nodiscard]] QHash<int, QByteArray> roleNames() const override;

    /**
     * @brief Get the QModelIndex the given event ID in the model.
     */
    Q_INVOKABLE QModelIndex indexForEventId(const QString &eventId) const;

    /**
     * @brief Finds the event of the given event ID in the model, returning nullptr if no matches were found.
     */
    Q_INVOKABLE const Quotient::RoomEvent *findEvent(const QString &eventId) const;

    /**
     * @brief The number of selected messages.
     */
    int selectedMessageCount() const;

    /**
     * @brief Whether the given message is selected.
     */
    Q_INVOKABLE bool isMessageSelected(const QString &eventId) const;

    /**
     * @brief Toggle the selection state of the given message.
     */
    Q_INVOKABLE void toggleMessageSelection(const QString &eventId);

    /**
     * @brief Hides the media for a given event.
     */
    Q_INVOKABLE void hideMedia(const QString &eventId);

    /**
     * @brief Shows the media for a given event.
     */
    Q_INVOKABLE void showMedia(const QString &eventId);

    /**
     * @brief If the media is hidden for a given event.
     */
    Q_INVOKABLE bool isMediaHidden(const QString &eventId);

    static void setShowAllEvents(bool enabled);
    static void setShowDeletedMessages(bool enabled);

Q_SIGNALS:
    /**
     * @brief Emitted when the reader marker index is changed.
     */
    void readMarkerIndexChanged();

    /**
     * @brief Emitted when a message is selected or deselected.
     */
    void selectionChanged();

private:
    static bool m_showAllEvents;
    static bool m_showDeletedMessages;

    std::pair<MessageModel *, bool> messageModel() const;

    bool eventIsVisible(int sourceRow, const QModelIndex &sourceParent) const;

    bool showAuthor(QModelIndex index) const;

    /**
     * @brief Aggregation of the text of consecutive state events starting at row.
     *
     * If state events happen on different days they will be split into two aggregate
     * events.
     */
    [[nodiscard]] QString aggregateEventToString(int row) const;

    /**
     * @brief Return a list of consecutive state events starting at row.
     *
     * If state events happen on different days they will be split into two aggregate
     * events.
     */
    [[nodiscard]] QVariantList stateEventsList(int row) const;

    /**
     * @brief List of the first 5 unique authors for the aggregate state events starting at row.
     */
    [[nodiscard]] QVariantList authorList(int row) const;

    /**
     * @brief The number of unique authors beyond the first 5 for the aggregate state events starting at row.
     */
    [[nodiscard]] QString excessAuthors(int row) const;
};
