#include "chatwindow.h"

#include "appevent.h"
#include "widgets/chatView/chat1/chatview.h"

ChatWindow::ChatWindow(WebSocketClient *webSocketClient, QWidget *parent)
    : QWidget(parent)
    , m_webSocketClient(webSocketClient)
{
    setWindowTitle("气泡消息示例");
    resize(800, 600);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建消息视图
    m_messageView = new ChatView(this);
    mainLayout->addWidget(m_messageView);

    // 创建输入区域
    QHBoxLayout *inputLayout = new QHBoxLayout();
    m_inputEdit = new QLineEdit(this);
    m_inputEdit->setPlaceholderText("输入消息...");
    inputLayout->addWidget(m_inputEdit);

    QPushButton *sendButton = new QPushButton("发送", this);
    inputLayout->addWidget(sendButton);

    QPushButton *imageButton = new QPushButton("图片", this);
    // inputLayout->addWidget(imageButton);
    imageButton->setHidden(true);

    QPushButton *fileButton = new QPushButton("文件", this);
    //  inputLayout->addWidget(fileButton);
    fileButton->setHidden(true);

    QPushButton *recallButton = new QPushButton("撤回", this);
    //  inputLayout->addWidget(recallButton);
    recallButton->setHidden(true);

    mainLayout->addLayout(inputLayout);

    // 连接信号和槽
    connect(sendButton, &QPushButton::clicked, this, &ChatWindow::onSendClicked);
    connect(m_inputEdit, &QLineEdit::returnPressed, this, &ChatWindow::onSendClicked);
    connect(imageButton, &QPushButton::clicked, this, &ChatWindow::onImageClicked);
    connect(fileButton, &QPushButton::clicked, this, &ChatWindow::onFileClicked);
    connect(recallButton, &QPushButton::clicked, this, &ChatWindow::onRecallClicked);

    // // 添加一些示例消息
    // m_messageView->addMessage(QDateTime::currentDateTime().toString(), "", true);
    // m_messageView->addMessage("你好，这是一条测试文本消息！", "", true);
    // m_messageView->addMessage(
    //     "你好，这是一条测试文本消息！你好，这是一条测试文本消息！你好，这是一条测试文本消息！你好，"
    //     "这是一条测试文本消息！你好，这是一条测试文本消息！你好，这是一条测试文本消息！你好，这是一"
    //     "条测试文本消息！你好，这是一条测试文本消息！你好，这是一条测试文本消息！",
    //     "",
    //     true);

    // initWebsocket();
    // m_messageView->addSuccessMessage("操作成功完成", BubbleMessage::Sent);
    // m_messageView->addFailureMessage("操作失败", BubbleMessage::Received);

    connect(m_webSocketClient,
            &WebSocketClient::messageReceived,
            this,
            [this](const ChatMessage &message) {
                qDebug() << message.text;
                if (message.isLeft()) {
                    m_messageView->addMessage(message.text, "", message.senderName, true);
                } else if (message.isRight()) {
                    m_messageView->addMessage(message.text, "", message.senderName, false);
                } else {
                    m_messageView->addSystemMessage(message.text);
                }
            }); // 回复

    // connect(m_webSocketClient,
    //         &WebSocketClient::connectionStateChanged,
    //         this,
    //         &ChatRoomWidget::onConnectionStateChanged); //连接状态
    connect(m_webSocketClient,
            &WebSocketClient::errorOccurred,
            this,
            [](const QString &errorMessage) { qDebug() << errorMessage; }); //错误反馈

    connect(m_webSocketClient, &WebSocketClient::connectionStateChanged, this, [](bool connected) {
        emit AppEvent::instance()->connectionStateChanged(connected);
    });
}
void ChatWindow::initWebsocket(const QString &roomId)
{
    // 首先断开链接
    m_webSocketClient->disconnect();

    if (roomId.isEmpty()) {
        m_webSocketClient->connectToRoom("416a1990-85ae-48af-aef6-5168230b999f");
    } else {
        m_webSocketClient->connectToRoom(roomId);
    }
}


void ChatWindow::onSendClicked()
{
    QString text = m_inputEdit->text().trimmed();
    if (text.isEmpty())
        return;

    m_webSocketClient->sendMessage(text, "room");

    m_messageView->addMessage(text, "", AppEvent::instance()->userName(), false);

    m_inputEdit->clear();
}

void ChatWindow::onImageClicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    "选择图片",
                                                    QString(),
                                                    "图片文件 (*.png *.jpg *.jpeg *.bmp)");
    if (fileName.isEmpty())
        return;

    QPixmap image(fileName);
    if (image.isNull())
        return;
}

void ChatWindow::onFileClicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, "选择文件");
    if (fileName.isEmpty())
        return;

    // QFileInfo fileInfo(fileName);

    // // 随机发送或接收文件
    // BubbleMessage::MessageDirection direction = (qrand() % 2 == 0) ? BubbleMessage::Sent
    //                                                                : BubbleMessage::Received;
    // m_messageView->addFileMessage(fileInfo.fileName(), fileName, fileInfo.size(), direction);
}

void ChatWindow::onRecallClicked()
{
    // 随机发送或接收撤回消息
    // BubbleMessage::MessageDirection direction = (qrand() % 2 == 0) ? BubbleMessage::Sent
    //                                                                : BubbleMessage::Received;
    // QString name = (direction == BubbleMessage::Sent) ? "你" : "对方";
    // m_messageView->addRecallMessage(name, direction);
}

void ChatWindow::closeEvent(QCloseEvent *event)
{
    // 阻止默认关闭行为
    event->ignore();
    
    // 发出关闭请求信号，让MainPanel处理
    emit windowCloseRequested();
}
