// 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

#include "messagemodel.h"

#include <QEvent>

#include <Quotient/events/encryptedevent.h>
#include <Quotient/events/redactionevent.h>
#include <Quotient/events/roommessageevent.h>
#include <Quotient/events/stickerevent.h>
#include <Quotient/thread.h>

#include <KFormat>

#include "contentprovider.h"
#include "enums/blocktype.h"
#include "enums/delegatetype.h"
#include "eventhandler.h"
#include "events/pollevent.h"
#include "models/eventmessagecontentmodel.h"
#include "neochatdatetime.h"
#include "neochatroommember.h"

#include <ranges>

using namespace Quotient;

std::function<bool(const Quotient::RoomEvent *)> MessageModel::m_hiddenFilter = [](const Quotient::RoomEvent *) -> bool {
    return false;
};

MessageModel::MessageModel(QObject *parent)
    : QAbstractListModel(parent)
{
    connect(this, &MessageModel::newEventAdded, this, &MessageModel::createEventObjects, Qt::QueuedConnection);

    connect(this, &MessageModel::modelAboutToReset, this, [this]() {
        m_resetting = true;
    });
    connect(this, &MessageModel::modelReset, this, [this]() {
        m_resetting = false;
    });
}

NeoChatRoom *MessageModel::room() const
{
    return m_room;
}

void MessageModel::setRoom(NeoChatRoom *room)
{
    if (room == m_room) {
        return;
    }

    clearSelectedMessages();

    const auto oldRoom = m_room;
    Q_EMIT roomAboutToChange(oldRoom, room);
    clearModel();

    if (!m_resetting) {
        m_resetting = true;
        Q_EMIT modelAboutToReset();
        beginResetModel();
    }
    m_room = room;
    if (m_room != nullptr) {
        m_room->setVisible(true);
    }
    if (m_resetting) {
        endResetModel();
        Q_EMIT modelResetComplete();
        m_resetting = false;
    }
    Q_EMIT roomChanged(oldRoom, m_room);
}

int MessageModel::timelineServerIndex() const
{
    return 0;
}

QPersistentModelIndex MessageModel::readMarkerIndex() const
{
    return m_lastReadEventIndex;
}

std::optional<std::reference_wrapper<const Quotient::RoomEvent>> MessageModel::getEventForIndex(QModelIndex index) const
{
    Q_UNUSED(index)
    return std::nullopt;
}

QVariant MessageModel::data(const QModelIndex &idx, int role) const
{
    if (!checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
        return {};
    }
    const auto row = idx.row();

    if (!m_room || row < 0 || row >= rowCount()) {
        return {};
    };

    if (m_lastReadEventIndex.row() == row) {
        switch (role) {
        case DelegateTypeRole:
            return DelegateType::ReadMarker;
        case DateTimeRole:
            return data(index(m_lastReadEventIndex.row() + 1, 0), DateTimeRole);
        case SpecialMarksRole:
            // Check if all the earlier events in the timeline are hidden. If so hide this.
            for (auto r = row - 1; r >= 0; --r) {
                const auto specialMark = index(r).data(SpecialMarksRole);
                if (!(specialMark == EventStatus::Hidden || specialMark == EventStatus::Replaced)) {
                    return EventStatus::Normal;
                }
            }
            return EventStatus::Hidden;
        }
        return {};
    }

    bool isPending = row < timelineServerIndex();

    const auto event = getEventForIndex(idx);
    if (!event.has_value()) {
        return {};
    }

    // Figure out the correct room for this event.
    // This is unusual for a timeline model, but not for our search model which can span multiple room versions.
    NeoChatRoom *eventRoom = m_room;
    if (event->get().roomId() != m_room->id()) {
        // Grab the associated room *if* we can find it.
        if (const auto room = dynamic_cast<NeoChatRoom *>(m_room->connection()->room(event->get().roomId()))) {
            eventRoom = room;
        }
    }

    if (role == Qt::DisplayRole) {
        return EventHandler::richBody(eventRoom, &event.value().get());
    }

    if (role == ContentModelRole) {
        if (event->get().is<EncryptedEvent>() || event->get().is<PollStartEvent>() || event->get().is<StickerEvent>()) {
            return QVariant::fromValue<EventMessageContentModel *>(ContentProvider::self().contentModelForEvent(eventRoom, event->get().id()));
        }

        auto roomMessageEvent = eventCast<const RoomMessageEvent>(&event.value().get());
        if (roomMessageEvent && roomMessageEvent->isThreaded()) {
            return QVariant::fromValue<EventMessageContentModel *>(
                ContentProvider::self().contentModelForEvent(eventRoom, roomMessageEvent->threadRootEventId()));
        }
        return QVariant::fromValue<EventMessageContentModel *>(ContentProvider::self().contentModelForEvent(eventRoom, &event->get()));
    }

    if (role == GenericDisplayRole) {
        return EventHandler::genericBody(eventRoom, &event.value().get());
    }

    if (role == DelegateTypeRole) {
        return DelegateType::typeForEvent(event.value().get());
    }

    if (role == AuthorRole) {
        QString mId;
        if (isPending) {
            mId = eventRoom->localMember().id();
        } else {
            mId = event.value().get().senderId();
        }

        return QVariant::fromValue<NeochatRoomMember *>(eventRoom->qmlSafeMember(mId));
    }

    if (role == HighlightRole) {
        return EventHandler::isHighlighted(eventRoom, &event.value().get());
    }

    if (role == SpecialMarksRole) {
        if (isPending) {
            // A pending event with an m.new_content key will be merged into the
            // original event so don't show.
            if (event.value().get().contentJson().contains("m.new_content"_L1)) {
                return EventStatus::Hidden;
            }
            // A threaded event will be merged into the thread root event so
            // also don't show.
            const auto roomMessageEvent = eventCast<const RoomMessageEvent>(&event.value().get());
            if (roomMessageEvent && roomMessageEvent->relatesTo() && roomMessageEvent->relatesTo()->type == EventRelation::ThreadType) {
                return EventStatus::Hidden;
            }
            const auto pendingIt = eventRoom->findPendingEvent(event->get().transactionId());
            if (pendingIt == eventRoom->pendingEvents().end()) {
                return EventStatus::Hidden;
            }
            return pendingIt->deliveryStatus();
        }

        if (EventHandler::isHidden(eventRoom, &event.value().get(), m_hiddenFilter)) {
            return EventStatus::Hidden;
        }

        const auto roomMessageEvent = eventCast<const RoomMessageEvent>(&event.value().get());
        if (roomMessageEvent && roomMessageEvent->relatesTo() && roomMessageEvent->relatesTo()->type == EventRelation::ThreadType) {
            return EventStatus::Hidden;
        }
        return EventStatus::Normal;
    }

    if (role == EventIdRole) {
        return event.value().get().displayId();
    }

    if (role == ProgressInfoRole) {
        if (auto e = eventCast<const RoomMessageEvent>(&event.value().get())) {
            if (e->has<EventContent::FileContent>() || e->has<EventContent::ImageContent>() || e->has<EventContent::VideoContent>()
                || e->has<EventContent::AudioContent>()) {
                return QVariant::fromValue(eventRoom->cachedFileTransferInfo(&event.value().get()));
            }
        }
        if (eventCast<const StickerEvent>(&event.value().get())) {
            return QVariant::fromValue(eventRoom->cachedFileTransferInfo(&event.value().get()));
        }
        return {};
    }

    if (role == DateTimeRole) {
        return QVariant::fromValue(EventHandler::dateTime(eventRoom, &event.value().get(), isPending));
    }

    if (role == IsThreadedRole) {
        if (auto roomMessageEvent = eventCast<const RoomMessageEvent>(&event.value().get())) {
            return roomMessageEvent->isThreaded() || eventRoom->threads().contains(event->get().id());
        }
        return {};
    }

    if (role == ThreadRootRole) {
        auto roomMessageEvent = eventCast<const RoomMessageEvent>(&event.value().get());
        if (roomMessageEvent && roomMessageEvent->isThreaded()) {
            return roomMessageEvent->threadRootEventId();
        } else if (eventRoom->threads().contains(event->get().id())) {
            return event->get().id();
        }
        return {};
    }

    if (role == IsPollRole) {
        return event->get().is<PollStartEvent>();
    }

    if (role == ShowSectionRole) {
        for (auto r = row + 1; r < rowCount(); ++r) {
            auto i = index(r);
            // Note !itemData(i).empty() is a check for instances where rows have been removed, e.g. when the read marker is moved.
            // While the row is removed the subsequent row indexes are not changed so we need to skip over the removed index.
            // See - https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows
            if (data(i, SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) {
                const auto day = data(idx, DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date().dayOfYear();
                const auto previousEventDay = data(i, DateTimeRole).value<NeoChatDateTime>().dateTime().toLocalTime().date().dayOfYear();
                return day != previousEventDay;
            }
        }

        return false;
    }

    if (role == ReadMarkersRole) {
        if (m_readMarkerModels.contains(event.value().get().id())) {
            return QVariant::fromValue<ReadMarkerModel *>(m_readMarkerModels[event.value().get().id()].get());
        } else {
            return QVariantList();
        }
    }

    if (role == ShowReadMarkersRole) {
        return m_readMarkerModels.contains(event.value().get().id());
    }

    if (role == VerifiedRole) {
#ifdef RUST_CRYPTO
        return m_room->connection()->isVerifiedEvent(event.value().get().id(), room());
#else
        if (event.value().get().originalEvent()) {
            auto encrypted = dynamic_cast<const EncryptedEvent *>(event.value().get().originalEvent());
            Q_ASSERT(encrypted);
            return eventRoom->connection()->isVerifiedSession(encrypted->sessionId().toLatin1());
        }
        return false;
#endif
    }

    if (role == AuthorDisplayNameRole) {
        return EventHandler::authorDisplayName(eventRoom, &event.value().get(), isPending);
    }

    if (role == IsRedactedRole) {
        return event.value().get().isRedacted();
    }

    if (role == IsPendingRole) {
        return row < static_cast<int>(eventRoom->pendingEvents().size());
    }

    if (role == IsMediaRole) {
        return EventHandler::isMediaMessage(&event.value().get());
    }

    if (role == IsEditableRole) {
        return Blocks::typeForEvent(event.value().get()) == Blocks::Text && event.value().get().senderId() == eventRoom->localMember().id();
    }

    if (role == ShowAuthorRole) {
        return true;
    }

    if (role == EventTypeRole) {
        return event.value().get().matrixType();
    }

    if (role == RoomRole) {
        return QVariant::fromValue(eventRoom);
    }

    if (role == IsSelectedRole) {
        const auto eventIt = m_selectedMessageIds.constFind(eventRoom->id());
        return eventIt != m_selectedMessageIds.constEnd() && eventIt.value().contains(event.value().get().id());
    }

    return {};
}

QHash<int, QByteArray> MessageModel::roleNames() const
{
    QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
    roles[DelegateTypeRole] = "delegateType";
    roles[EventIdRole] = "eventId";
    roles[DateTimeRole] = "dateTime";
    roles[AuthorRole] = "author";
    roles[HighlightRole] = "isHighlighted";
    roles[SpecialMarksRole] = "marks";
    roles[ProgressInfoRole] = "progressInfo";
    roles[IsThreadedRole] = "isThreaded";
    roles[ThreadRootRole] = "threadRoot";
    roles[IsPollRole] = "isPoll";
    roles[ShowSectionRole] = "showSection";
    roles[ReadMarkersRole] = "readMarkers";
    roles[ShowReadMarkersRole] = "showReadMarkers";
    roles[VerifiedRole] = "verified";
    roles[AuthorDisplayNameRole] = "authorDisplayName";
    roles[IsRedactedRole] = "isRedacted";
    roles[GenericDisplayRole] = "genericDisplay";
    roles[IsPendingRole] = "isPending";
    roles[ContentModelRole] = "contentModel";
    roles[IsMediaRole] = "isMedia";
    roles[IsEditableRole] = "isEditable";
    roles[ShowAuthorRole] = "showAuthor";
    roles[EventTypeRole] = "eventType";
    roles[RoomRole] = "room";
    roles[IsSelectedRole] = "isSelected";
    return roles;
}

QModelIndex MessageModel::indexForEventId(const QString &eventId) const
{
    const auto matches = match(index(0, 0), EventIdRole, eventId);
    if (matches.isEmpty()) {
        qWarning() << "Trying to find non-existent event:" << eventId;
        return {};
    }
    return matches.constFirst();
}

const RoomEvent *MessageModel::findEvent(const QString &eventId) const
{
    const auto index = indexForEventId(eventId);
    if (const auto event = getEventForIndex(index)) {
        return &event.value().get();
    }
    return nullptr;
}

int MessageModel::selectedMessageCount() const
{
    return std::accumulate(m_selectedMessageIds.constBegin(), m_selectedMessageIds.constEnd(), 0, [](int count, const auto &selectedMessageIds) {
        return count + selectedMessageIds.size();
    });
}

bool MessageModel::canDeleteSelectedMessages() const
{
    if (!m_room) {
        return false;
    }

    const QString localUserId = m_room->connection()->userId();

    return std::ranges::all_of(m_selectedMessageIds.asKeyValueRange(), [this, &localUserId](const auto &pair) {
        const auto [roomId, selectedMessageIds] = pair;

        const NeoChatRoom *eventRoom = m_room;
        if (roomId != eventRoom->id()) {
            if (const auto room = dynamic_cast<const NeoChatRoom *>(m_room->connection()->room(roomId))) {
                eventRoom = room;
            }
        }

        return eventRoom->canSendState("redact"_L1) || std::ranges::all_of(selectedMessageIds, [eventRoom, &localUserId](const QString &eventId) {
                   const RoomEvent *event = eventRoom->findEvent(eventId);
                   return event && event->senderId() == localUserId;
               });
    });
}

bool MessageModel::isMessageSelected(const QString &eventId) const
{
    const auto idx = indexForEventId(eventId);
    if (!idx.isValid()) {
        qWarning() << "isMessageSelected: event not in model:" << eventId;
        return false;
    }

    const auto event = getEventForIndex(idx);
    const NeoChatRoom *eventRoom = m_room;
    if (const auto roomId = event.value().get().roomId(); roomId != eventRoom->id()) {
        if (const auto room = dynamic_cast<const NeoChatRoom *>(m_room->connection()->room(roomId))) {
            eventRoom = room;
        }
    }

    const auto eventIt = m_selectedMessageIds.constFind(eventRoom->id());
    return eventIt != m_selectedMessageIds.constEnd() && eventIt.value().contains(event.value().get().id());
}

void MessageModel::toggleMessageSelection(const QString &eventId)
{
    const auto idx = indexForEventId(eventId);
    if (!idx.isValid()) {
        qWarning() << "toggleMessageSelection: event not in model:" << eventId;
        return;
    }

    const auto event = getEventForIndex(idx);
    const NeoChatRoom *eventRoom = m_room;
    if (const auto roomId = event.value().get().roomId(); roomId != eventRoom->id()) {
        if (const auto room = dynamic_cast<const NeoChatRoom *>(m_room->connection()->room(roomId))) {
            eventRoom = room;
        }
    }

    auto &eventIds = m_selectedMessageIds[eventRoom->id()];
    if (!eventIds.remove(eventId)) {
        eventIds.insert(eventId);
    }

    if (eventIds.isEmpty()) {
        m_selectedMessageIds.remove(eventRoom->id());
    }

    Q_EMIT selectionChanged();
    Q_EMIT refreshEventRoles(idx.row(), {IsSelectedRole});
}

QString MessageModel::getFormattedSelectedMessages() const
{
    QString formattedContent;
    formattedContent.reserve(selectedMessageCount() * 256); // estimate an average of 256 characters per message

    for (const auto &[roomId, selectedMessageIds] : m_selectedMessageIds.asKeyValueRange()) {
        const NeoChatRoom *eventRoom = m_room;
        if (roomId != m_room->id()) {
            if (const auto *room = dynamic_cast<const NeoChatRoom *>(m_room->connection()->room(roomId))) {
                eventRoom = room;
            }
        }

        // clang-format off
        auto allEvents = selectedMessageIds
            | std::views::transform(std::bind_front(&NeoChatRoom::findEvent, eventRoom))
            | std::views::filter([](const RoomEvent *event) {
                return event != nullptr;
            });
        // clang-format on

        QVector<const RoomEvent *> rootEvents;
        QHash<QString, QVector<const RoomEvent *>> threadReplies;
        // rootEvents.contains is O(n) but we expect the number of selected messages to be small
        // and this is simpler than trying to maintain a separate set of root event ids.
        for (const auto &event : allEvents) {
            if (const auto roomMessageEvent = eventCast<const RoomMessageEvent>(event)) {
                if (const auto rel = roomMessageEvent->relatesTo(); rel && rel->type == EventRelation::ThreadType) {
                    const auto rootEvent = eventRoom->findEvent(rel->eventId);
                    if (!rootEvent) {
                        continue;
                    }

                    if (!rootEvents.contains(rootEvent)) {
                        rootEvents.push_back(rootEvent);
                    }
                    threadReplies[rootEvent->id()].push_back(event);
                } else if (!rootEvents.contains(event)) {
                    rootEvents.push_back(event);
                }
            }
        }

        std::ranges::sort(rootEvents, {}, &RoomEvent::originTimestamp);
        for (auto &replies : threadReplies.values()) {
            std::ranges::sort(replies, {}, &RoomEvent::originTimestamp);
        }

        if (m_selectedMessageIds.size() > 1) {
            formattedContent += u"["_s + eventRoom->displayName() + u"]\n"_s;
        }

        // clang-format off
        for (const RoomEvent *event : rootEvents) {
            formattedContent +=
                EventHandler::authorDisplayName(eventRoom, event)
                % u" — "_s
                % EventHandler::dateTime(eventRoom, event).shortDateTime();
            if (const auto roomMessageEvent = eventCast<const RoomMessageEvent>(event)) {
                if (const auto rel = roomMessageEvent->relatesTo(); rel && rel->type == EventRelation::ReplyType) {
                    if (const auto parentEvent = eventRoom->findEvent(rel->eventId)) {
                        formattedContent +=
                            u"\n> In reply to "_s
                            % EventHandler::authorDisplayName(eventRoom, parentEvent)
                            % u"\n"_s
                            % EventHandler::plainBody(eventRoom, parentEvent)
                            % u"\n"_s;
                    }
                }
            }

            formattedContent +=
                u'\n'
                % EventHandler::plainBody(eventRoom, event)
                % u"\n\n"_s;

            if (const auto it = threadReplies.constFind(event->id()); it != threadReplies.constEnd()) {
                for (const RoomEvent *threadReply : it.value()) {
                    formattedContent +=
                        u"\t"_s
                        % EventHandler::authorDisplayName(eventRoom, threadReply)
                        % u" — "_s
                        % EventHandler::dateTime(eventRoom, threadReply).shortDateTime()
                        % u"\n\t"_s
                        % EventHandler::plainBody(eventRoom, threadReply)
                        % u"\n\n"_s;
                }
            }
        }
        // clang-format on
    }

    return formattedContent.trimmed();
}

void MessageModel::deleteSelectedMessages(const QString &reason)
{
    for (const auto &[roomId, selectedMessageIds] : m_selectedMessageIds.asKeyValueRange()) {
        NeoChatRoom *eventRoom = m_room;
        if (roomId != eventRoom->id()) {
            if (auto room = dynamic_cast<NeoChatRoom *>(m_room->connection()->room(roomId))) {
                eventRoom = room;
            }
        }

        QStringList events;
        for (const auto &eventId : std::as_const(selectedMessageIds)) {
            const auto [event, isPending] = eventRoom->getEvent(eventId);
            if (event && !isPending && !event->isRedacted() && !is<RedactionEvent>(*event)) {
                events += eventId;
            }
        }
        eventRoom->deleteMessageIds(events, reason);
    }

    clearSelectedMessages();
}

void MessageModel::clearSelectedMessages()
{
    m_selectedMessageIds.clear();
    Q_EMIT selectionChanged();
    if (rowCount() > 0) {
        Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), {IsSelectedRole});
    }
}

void MessageModel::fullEventRefresh(int row)
{
    auto roles = roleNames().keys();
    // The author of an event never changes so should only be updated when a member
    // changed signal is emitted.
    // This also avoids any race conditions where a member is updating and this refresh
    // tries to access a member event that has already been deleted.
    roles.removeAll(AuthorRole);
    refreshEventRoles(row, roles);
}

void MessageModel::refreshEventRoles(int row, const QList<int> &roles)
{
    const auto idx = index(row);
    Q_EMIT dataChanged(idx, idx, roles);
}

int MessageModel::refreshEventRoles(const QString &id, const QList<int> &roles)
{
    // On 64-bit platforms, difference_type for std containers is long long
    // but Qt uses int throughout its interfaces; hence casting to int below.
    int row = -1;
    // First try pendingEvents because it is almost always very short.
    const auto pendingIt = m_room->findPendingEvent(id);
    if (pendingIt != m_room->pendingEvents().end()) {
        row = int(pendingIt - m_room->pendingEvents().begin());
    } else {
        const auto timelineIt = m_room->findInTimeline(id);
        if (timelineIt == m_room->historyEdge()) {
            return -1;
        }
        row = int(timelineIt - m_room->messageEvents().rbegin()) + timelineServerIndex();
        if (data(index(row, 0), DelegateTypeRole).toInt() == DelegateType::ReadMarker || data(index(row, 0), DelegateTypeRole).toInt() == DelegateType::Other) {
            row++;
        }
    }
    refreshEventRoles(row, roles);
    return row;
}

void MessageModel::refreshLastUserEvents(int baseTimelineRow)
{
    if (!m_room || m_room->timelineSize() <= baseTimelineRow) {
        return;
    }

    const auto &timelineBottom = m_room->messageEvents().rbegin();
    const auto &lastSender = (*(timelineBottom + baseTimelineRow))->senderId();
    const auto limit = timelineBottom + std::min(baseTimelineRow + 10, m_room->timelineSize());
    for (auto it = timelineBottom + std::max(baseTimelineRow - 10, 0); it != limit; ++it) {
        if ((*it)->senderId() == lastSender) {
            fullEventRefresh(it - timelineBottom);
        }
    }
}

void MessageModel::createEventObjects(const Quotient::RoomEvent *event)
{
    if (event == nullptr) {
        return;
    }

    auto eventId = event->id();
    if (eventId.isEmpty()) {
        eventId = event->transactionId();
    }

    // ReadMarkerModel handles updates to add and remove markers, we only need to
    // handle adding and removing whole models here.
    if (m_readMarkerModels.contains(eventId)) {
        // If a model already exists but now has no reactions remove it
        if (m_readMarkerModels[eventId]->rowCount() <= 0) {
            m_readMarkerModels.remove(eventId);
            if (!m_resetting) {
                refreshEventRoles(eventId, {ReadMarkersRole, ShowReadMarkersRole});
            }
        }
    } else {
        auto memberIds = m_room->userIdsAtEvent(eventId);
        memberIds.remove(m_room->localMember().id());
        if (memberIds.size() > 0) {
            // If a model doesn't exist and there are reactions add it.
            auto newModel = QSharedPointer<ReadMarkerModel>(new ReadMarkerModel(eventId, m_room));
            if (newModel->rowCount() > 0) {
                m_readMarkerModels[eventId] = newModel;
                if (!m_resetting) {
                    refreshEventRoles(eventId, {ReadMarkersRole, ShowReadMarkersRole});
                }
            }
        }
    }
}

NeoChatRoom *MessageModel::roomForEvent(const QString &eventId) const
{
    const auto idx = indexForEventId(eventId);
    if (!idx.isValid()) {
        qWarning() << "Event not in model:" << eventId;
        return nullptr;
    }

    const auto event = getEventForIndex(idx);
    NeoChatRoom *eventRoom = m_room;
    if (const auto roomId = event.value().get().roomId(); roomId != eventRoom->id()) {
        if (const auto room = dynamic_cast<NeoChatRoom *>(m_room->connection()->room(roomId))) {
            eventRoom = room;
        }
    }

    return eventRoom;
}

void MessageModel::moveReadMarker(const QString &toEventId)
{
    const auto timelineIt = m_room->findInTimeline(toEventId);
    if (timelineIt == m_room->historyEdge()) {
        return;
    }
    int newRow = int(timelineIt - m_room->messageEvents().rbegin()) + timelineServerIndex();

    if (!m_lastReadEventIndex.isValid()) {
        // Not valid index means we don't display any marker yet, in this case
        // we create the new index and insert the row in case the read marker
        // need to be displayed.
        if (newRow > timelineServerIndex()) {
            // The user didn't read all the messages yet.
            beginInsertRows({}, newRow, newRow);
            m_lastReadEventIndex = QPersistentModelIndex(index(newRow, 0));
            endInsertRows();
            Q_EMIT readMarkerIndexChanged();
            Q_EMIT readMarkerAdded();
            return;
        }
        // The user read all the messages and we didn't display any read marker yet
        // => do nothing
        return;
    }
    if (newRow <= timelineServerIndex()) {
        // The user read all the messages => remove read marker
        beginRemoveRows({}, m_lastReadEventIndex.row(), m_lastReadEventIndex.row());
        m_lastReadEventIndex = QModelIndex();
        endRemoveRows();
        Q_EMIT readMarkerIndexChanged();
        return;
    }

    // The user didn't read all the messages yet but moved the reader marker.
    beginMoveRows({}, m_lastReadEventIndex.row(), m_lastReadEventIndex.row(), {}, newRow);
    m_lastReadEventIndex = QPersistentModelIndex(index(newRow, 0));
    endMoveRows();
    Q_EMIT readMarkerIndexChanged();
}

void MessageModel::clearModel()
{
    if (m_room) {
        const auto oldRoom = m_room;

        // HACK: Reset the model to a null room first to make sure QML dismantles
        // last room's objects before the room is actually changed
        if (!m_resetting) {
            m_resetting = true;
            Q_EMIT modelAboutToReset();
            beginResetModel();
        }
        m_room->disconnect(this);
        m_room = nullptr;
        if (m_resetting) {
            endResetModel();
            Q_EMIT modelResetComplete();
            m_resetting = false;
        }

        // Because we don't want any of the object deleted before the model is cleared.
        oldRoom->setVisible(false);
    }

    // Don't clear the member objects until the model has been fully reset and all
    // refs cleared.
    clearEventObjects();
}

void MessageModel::clearEventObjects()
{
    m_readMarkerModels.clear();
}

bool MessageModel::event(QEvent *event)
{
    if (event->type() == QEvent::ApplicationPaletteChange) {
        if (rowCount() > 0) {
            Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), {AuthorRole, ReadMarkersRole});
        }
    }
    return QObject::event(event);
}

void MessageModel::setHiddenFilter(std::function<bool(const Quotient::RoomEvent *)> hiddenFilter)
{
    MessageModel::m_hiddenFilter = hiddenFilter;
}

void MessageModel::hideMedia(const QString &eventId)
{
    ContentProvider::self().contentModelForEvent(roomForEvent(eventId), eventId)->hideMedia();
}

void MessageModel::showMedia(const QString &eventId)
{
    ContentProvider::self().contentModelForEvent(roomForEvent(eventId), eventId)->showMedia();
}

bool MessageModel::isMediaHidden(const QString &eventId)
{
    return ContentProvider::self().contentModelForEvent(roomForEvent(eventId), eventId)->isMediaHidden();
}

#include "moc_messagemodel.cpp"
