#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QFileDialog>
#include <QLabel>
#include <QTimer>
#include "chatview.h"

class ImageChatTestWidget : public QWidget
{
    Q_OBJECT

public:
    ImageChatTestWidget(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        setupUI();
        connectSignals();
        
        // 添加一些测试消息
        addTestMessages();
    }

private slots:
    void sendTextMessage()
    {
        QString text = m_textEdit->text();
        if (!text.isEmpty()) {
            m_chatView->addMessage(text, ":/icons/user.png", "我", false);
            m_textEdit->clear();
            
            // 模拟回复
            QTimer::singleShot(1000, [this, text]() {
                m_chatView->addMessage("收到：" + text, ":/icons/bot.png", "助手", true);
            });
        }
    }
    
    void sendImageMessage()
    {
        QString imagePath = QFileDialog::getOpenFileName(this, 
            "选择图片", 
            "", 
            "图片文件 (*.png *.jpg *.jpeg *.bmp *.gif)");
            
        if (!imagePath.isEmpty()) {
            QString text = m_textEdit->text();
            m_chatView->addImageMessage(imagePath, ":/icons/user.png", "我", false, text);
            m_textEdit->clear();
            
            // 模拟回复
            QTimer::singleShot(1000, [this]() {
                m_chatView->addMessage("收到了你的图片！", ":/icons/bot.png", "助手", true);
            });
        }
    }

private:
    void setupUI()
    {
        setWindowTitle("图片聊天测试");
        resize(600, 800);
        
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        
        // 聊天视图
        m_chatView = new ChatView(this);
        mainLayout->addWidget(m_chatView);
        
        // 输入区域
        QHBoxLayout *inputLayout = new QHBoxLayout();
        
        m_textEdit = new QLineEdit(this);
        m_textEdit->setPlaceholderText("输入消息...");
        inputLayout->addWidget(m_textEdit);
        
        m_sendTextBtn = new QPushButton("发送文字", this);
        inputLayout->addWidget(m_sendTextBtn);
        
        m_sendImageBtn = new QPushButton("发送图片", this);
        inputLayout->addWidget(m_sendImageBtn);
        
        mainLayout->addLayout(inputLayout);
    }
    
    void connectSignals()
    {
        connect(m_sendTextBtn, &QPushButton::clicked, this, &ImageChatTestWidget::sendTextMessage);
        connect(m_sendImageBtn, &QPushButton::clicked, this, &ImageChatTestWidget::sendImageMessage);
        connect(m_textEdit, &QLineEdit::returnPressed, this, &ImageChatTestWidget::sendTextMessage);
    }
    
    void addTestMessages()
    {
        m_chatView->addSystemMessage("欢迎使用图片聊天功能测试！");
        m_chatView->addMessage("你好！这是一条普通的文本消息。", ":/icons/bot.png", "助手", true);
        m_chatView->addMessage("你好！我可以发送图片了吗？", ":/icons/user.png", "我", false);
        m_chatView->addMessage("当然可以！点击'发送图片'按钮选择图片文件。", ":/icons/bot.png", "助手", true);
    }

private:
    ChatView *m_chatView;
    QLineEdit *m_textEdit;
    QPushButton *m_sendTextBtn;
    QPushButton *m_sendImageBtn;
};

// #include "test_image_chat.moc"

// int main(int argc, char *argv[])
// {
//     QApplication app(argc, argv);
    
//     ImageChatTestWidget widget;
//     widget.show();
    
//     return app.exec();
// }