/*
  SPDX-FileCopyrightText: 2025-2026 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "textautogeneratetext_export.h"
#include "widgets/view/lrucache.h"
#include <QItemDelegate>
#include <QScopedPointer>
#include <QTextDocument>
#include <TextAutoGenerateText/TextAutoGenerateAttachment>
#include <memory>
class QListView;
namespace TextAutoGenerateText
{
class TextAutoGenerateListViewTextSelection;
/*!
 * \class TextAutoGenerateText::TextAutoGenerateListViewBaseDelegate
 * \brief The TextAutoGenerateListViewBaseDelegate class
 * \author Laurent Montel <montel@kde.org>
 * \inmodule TextAutoGenerateText
 * \inheaderfile TextAutoGenerateText/TextAutoGenerateListViewBaseDelegate
 */
class TEXTAUTOGENERATETEXT_EXPORT TextAutoGenerateListViewBaseDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    explicit TextAutoGenerateListViewBaseDelegate(QListView *view);
    ~TextAutoGenerateListViewBaseDelegate() override;

    void clearSizeHintCache();

    void removeMessageCache(const QByteArray &uuid);

    void clearCache();

    [[nodiscard]] virtual QTextDocument *documentForIndex(const QModelIndex &index, int width) const = 0;
    [[nodiscard]] QString selectedText() const;
    [[nodiscard]] bool hasSelection() const;
    [[nodiscard]] virtual bool mouseEvent(QEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index);
    [[nodiscard]] virtual bool maybeStartDrag(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index);
    [[nodiscard]] virtual QString urlAt(const QModelIndex &index, QPoint pos) const;

    void selectAll(const QStyleOptionViewItem &option, const QModelIndex &index);

    void clearSelection();

    void clearDocumentCache();
Q_SIGNALS:
    void updateView(const QModelIndex &index);

protected:
    [[nodiscard]] QSize documentSizeHint(const QModelIndex &index, int maxWidth, const QStyleOptionViewItem &option, qreal *pBaseLine) const;
    [[nodiscard]] QSize textSizeHint(QTextDocument *doc, qreal *pBaseLine) const;
    [[nodiscard]] std::unique_ptr<QTextDocument> createTextDocument(const QString &text, int width) const;

    // Cache SizeHint value
    // We need to clear it when we resize widget.
    mutable LRUCache<QByteArray, QSize> mSizeHintCache;

    mutable LRUCache<QByteArray, std::unique_ptr<QTextDocument>> mDocumentCache;
    QAbstractItemView *const mListView;
    TextAutoGenerateListViewTextSelection *const mTextSelection;
};
}
