#ifndef CHATMESSAGEDELEGATE_H
#define CHATMESSAGEDELEGATE_H

#include <QHash>
#include <QPixmap>
#include <QStyledItemDelegate>
#include "qpainter.h"
#include "widgets/chatView/chat1/chatmessage.h"

#include <QTextDocument>

class TestWidget : public QWidget
{
public:
    TestWidget()
        : m_document(nullptr)
    {}
    QTextDocument *m_document;

    QTextDocument document;
    void setDocument(QTextDocument *doc)
    {
        document.setHtml(doc->toHtml());
        document.setDefaultFont(doc->defaultFont());
        document.setTextWidth(doc->textWidth());
        document.setDefaultTextOption(doc->defaultTextOption());

        m_document = &document;

        update();
    }
    void paintEvent(QPaintEvent *event) override
    {
        QWidget::paintEvent(event);

        if (!m_document)
            return;

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::TextAntialiasing);
        m_document->drawContents(&painter);
    };
};

class ChatMessageDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit ChatMessageDelegate(QObject *parent = nullptr);
    int hitTestText(const QPoint &pos, const QString &text) const;
    QSize calculateTextSize(const QFontMetrics &fm, const QString &text) const;

    void setSelectionRange(int start, int end)
    {
        m_selectionStart = start;
        m_selectionEnd = end;
    }
    void setCurrentMessageIndex(const QModelIndex &index) { m_currentMessageIndex = index; }

    int getPositionFromPoint(const QPoint &pos, const QString &text, const QFontMetrics &fm);

    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const override;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;

    void setViewportWidth(int width);
    int viewportWidth() const { return m_viewportWidth; }

private:
    void drawBubble(QPainter *painter, const QRectF &rect, bool isLeft) const;
    void drawAvatar(QPainter *painter, const QRectF &rect, const QString &avatarPath) const;
    void drawTextWithSelection(QPainter *painter,
                               const QRectF &rect,
                               const QString &text,
                               const QFontMetrics &fm,
                               bool isCurrentMessage) const;
    // 添加系统消息绘制方法
    void drawSystemMessage(QPainter *painter,
                           const QStyleOptionViewItem &option,
                           const ChatMessage &message) const;
    // 添加图片渲染方法
    void drawImage(QPainter *painter, const QRectF &rect, const QString &imagePath) const;
    QSize calculateImageSize(const QString &imagePath, int maxWidth, int maxHeight) const;
    QSize calculateContentSize(const ChatMessage &message, const QFontMetrics &fm) const;

    QHash<QString, QPixmap> m_avatarCache;
    mutable QHash<QString, QPixmap> m_imageCache;  // 图片缓存

    int m_viewportWidth;
    TestWidget test;
    mutable int m_selectionStart = -1;
    mutable int m_selectionEnd = -1;
    mutable QModelIndex m_currentMessageIndex;
};

#endif // CHATMESSAGEDELEGATE_H
