// SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#pragma once

#include <QAbstractListModel>
#include <QQmlEngine>
#include <functional>

#include "neochatroom.h"
#include "readmarkermodel.h"

/**
 * @class MessageModel
 *
 * This class defines a base model for visualising the room events.
 *
 * On its own MessageModel will result in an empty timeline as there is no mechanism
 * to retrieve events. This is by design as it allows the model to be inherited from
 * and the user can specify their own source of events, e.g. a room timeline or a
 * search result.
 *
 * The inherited model MUST do the following:
 *  - Define methods for retrieving events
 *  - Call newEventAdded() for each new event in the model so that all the required
 *    event objects are created.
 *  - Override getEventForIndex() so that the data() function can get an event for a
 *    given index
 *  - Override rowCount()
 *
 * Optionally the new model can:
 *  - override timelineServerIndex() if dealing with pending events otherwise the default
 *    function returns 0 which is correct for other use cases.
 *  - m_lastReadEventIndex is available to track a read marker location (so that the
 *    data function can output the appropriate values). The new class must implement
 *    the functionality to add, move, remove, etc though.
 *
 * @sa NeoChatRoom
 */
class MessageModel : public QAbstractListModel
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("")

    /**
     * @brief The current room that the model is getting its messages from.
     */
    Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)

    /**
     * @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)

    /**
     * @brief Whether the user can delete the selected messages.
     */
    Q_PROPERTY(bool canDeleteSelectedMessages READ canDeleteSelectedMessages NOTIFY selectionChanged)

public:
    /**
     * @brief Defines the model roles.
     */
    enum EventRoles {
        DelegateTypeRole = Qt::UserRole + 1, /**< The delegate type of the message. */
        EventIdRole, /**< The matrix event ID of the event. */
        DateTimeRole, /**< The timestamp for when the event was sent (as a NeoChatDateTime). */
        AuthorRole, /**< The author of the event. */
        HighlightRole, /**< Whether the event should be highlighted. */
        SpecialMarksRole, /**< Whether the event is hidden or not. */
        ProgressInfoRole, /**< Progress info when downloading files. */
        GenericDisplayRole, /**< A generic string based upon the message type. */
        IsMediaRole, /**< Whether the event is a media event. */

        ContentModelRole, /**< The MessageContentModel for the event. */

        IsThreadedRole, /**< Whether the message is in a thread. */
        ThreadRootRole, /**< The Matrix ID of the thread root message, if any . */
        IsPollRole, /**< Whether the message is a poll. */

        ShowSectionRole, /**< Whether the section header should be shown. */

        ReadMarkersRole, /**< The first 5 other users at the event for read marker tracking. */
        ShowReadMarkersRole, /**< Whether there are any other user read markers to be shown. */

        VerifiedRole, /**< Whether an encrypted message is sent in a verified session. */
        AuthorDisplayNameRole, /**< The displayname for the event's sender; for name change events, the old displayname. */
        IsRedactedRole, /**< Whether an event has been deleted. */
        IsPendingRole, /**< Whether an event is waiting to be accepted by the server. */
        IsEditableRole, /**< Whether the event can be edited by the user. */
        ShowAuthorRole, /**< Whether the author of a message should be shown. */
        EventTypeRole, /**< The matrix event type of this message. */
        RoomRole, /**< The room this event is from. */

        IsSelectedRole, /**< Whether the message is selected. */
        LastRole, // Keep this last
    };
    Q_ENUM(EventRoles)

    explicit MessageModel(QObject *parent = nullptr);

    [[nodiscard]] NeoChatRoom *room() const;
    void setRoom(NeoChatRoom *room);

    QPersistentModelIndex readMarkerIndex() const;

    /**
     * @brief Get the given role value at the given index.
     *
     * @sa QAbstractItemModel::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 EventRoles, QAbstractItemModel::roleNames()
     */
    [[nodiscard]] QHash<int, QByteArray> roleNames() const override;

    /**
     * @brief Get the QModelIndex of the given event ID in the model, returning an invalid QModelIndex if no matches were found.
     */
    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 local user can delete the selected messages.
     */
    bool canDeleteSelectedMessages() 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 Get the content of the selected messages formatted as a single string.
     */
    Q_INVOKABLE QString getFormattedSelectedMessages() const;

    /**
     * @brief Delete the selected messages with an optional reason.
     */
    Q_INVOKABLE void deleteSelectedMessages(const QString &reason = QString());

    /**
     * @brief Clear the selection of messages.
     */
    Q_INVOKABLE void clearSelectedMessages();

    static void setHiddenFilter(std::function<bool(const Quotient::RoomEvent *)> hiddenFilter);

    /**
     * @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);

Q_SIGNALS:
    /**
     * @brief Emitted when the room is changed.
     */
    void roomAboutToChange(NeoChatRoom *oldRoom, NeoChatRoom *newRoom);

    /**
     * @brief Emitted when the room is changed.
     */
    void roomChanged(NeoChatRoom *oldRoom, NeoChatRoom *newRoom);

    /**
     * @brief Emitted when the reader marker is added.
     */
    void readMarkerAdded();

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

    /**
     * @brief Emitted when the model is about to reset.
     */
    void modelAboutToReset();

    /**
     * @brief Emitted when the model has been reset.
     */
    void modelResetComplete();

    /**
     * @brief A signal to tell the MessageModel that a new event has been added.
     *
     * Any model inheriting from MessageModel needs to emit this signal for every
     * new event it adds.
     */
    void newEventAdded(const Quotient::RoomEvent *event);

    /**
     * @brief A signal that should be emitted when the local user posts a new event in the room.
     */
    void newLocalUserEventAdded();

    /**
     * @brief Emitted when a message is selected or deselected.
     */
    void selectionChanged();
protected:
    QPointer<NeoChatRoom> m_room;
    QPersistentModelIndex m_lastReadEventIndex;

    virtual int timelineServerIndex() const;
    virtual std::optional<std::reference_wrapper<const Quotient::RoomEvent>> getEventForIndex(QModelIndex index) const;

    bool m_resetting = false;
    bool m_movingEvent = false;

    void fullEventRefresh(int row);
    int refreshEventRoles(const QString &eventId, const QList<int> &roles = {});
    void refreshEventRoles(int row, const QList<int> &roles = {});
    void refreshLastUserEvents(int baseTimelineRow);

    void moveReadMarker(const QString &toEventId);

    void clearModel();
    void clearEventObjects();

    bool event(QEvent *event) override;

private:
    QMap<QString, QSharedPointer<ReadMarkerModel>> m_readMarkerModels;

    QHash<QString, QSet<QString>> m_selectedMessageIds;

    void createEventObjects(const Quotient::RoomEvent *event);
    NeoChatRoom *roomForEvent(const QString &eventId) const;

    static std::function<bool(const Quotient::RoomEvent *)> m_hiddenFilter;
};
