#ifndef CHATVIEW_H
#define CHATVIEW_H

#include <QListView>
#include "chatmessagedelegate.h"
#include "chatmessagemodel.h"

class ChatView : public QListView
{
    Q_OBJECT
public:
    explicit ChatView(QWidget *parent = nullptr);
    void addMessage(const QString &text, const QString &avatar, const QString &senderName, bool isLeft);
    void addImageMessage(const QString &imagePath, const QString &avatar, const QString &senderName, bool isLeft, const QString &text = QString());
    void addSystemMessage(const QString &text);
    void clear();

signals:
    void viewportWidthChanged(int width);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void contextMenuEvent(QContextMenuEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private:
    ChatMessageModel *m_model;
    ChatMessageDelegate *m_delegate;

    bool m_selecting;
    QModelIndex m_currentMessageIndex;
    int m_selectionStartPos = -1;
    int m_selectionEndPos = -1;
    QMenu *m_contextMenu = nullptr;

    QString getSelectedText() const;
    void copySelectedText();
    void scrollToBottom();
};
#endif // CHATVIEW_H
