#include "independentchatwindow.h"
#include "chatwindow.h"

#include "widgetframe/windowbar.h"
#include "widgetframe/windowbutton.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QApplication>

IndependentChatWindow::IndependentChatWindow(WebSocketClient *webSocketClient, QWidget *parent)
    : TWidget(parent)
    , m_chatWindow(nullptr)
    , m_webSocketClient(webSocketClient)
{
    setWindowTitle("聊天窗口");
    resize(800, 600);
    
    setupUI();
    setupTitleBar();
}

IndependentChatWindow::~IndependentChatWindow()
{
}

void IndependentChatWindow::setupUI()
{
    // 创建聊天窗口组件
    m_chatWindow = new ChatWindow(m_webSocketClient, this);
    
    // 添加到主布局
    mainLayout()->addWidget(m_chatWindow);
}

void IndependentChatWindow::setupTitleBar()
{
    // 创建自定义标题栏
    auto titleBar = new QWK::WindowBar(this);
    titleBar->setObjectName("win-title-bar");
    
    // 创建标题标签
    auto titleLabel = new QLabel("聊天窗口", this);
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setObjectName("win-title-label");
    
    // 创建窗口控制按钮
    auto minButton = new QWK::WindowButton(this);
    minButton->setObjectName("min-button");
    minButton->setProperty("system-button", true);
    
    auto maxButton = new QWK::WindowButton(this);
    maxButton->setCheckable(true);
    maxButton->setObjectName("max-button");
    maxButton->setProperty("system-button", true);
    
    auto closeButton = new QWK::WindowButton(this);
    closeButton->setObjectName("close-button");
    closeButton->setProperty("system-button", true);
    
    // 设置标题栏布局
    titleBar->setTitleLabel(titleLabel);
    titleBar->setMinButton(minButton);
    titleBar->setMaxButton(maxButton);
    titleBar->setCloseButton(closeButton);
    
    // 连接信号
    connect(titleBar, &QWK::WindowBar::minimizeRequested, this, &QWidget::showMinimized);
    connect(titleBar, &QWK::WindowBar::maximizeRequested, this, [this](bool max) {
        if (max) {
            showMaximized();
        } else {
            showNormal();
        }
    });
    connect(titleBar, &QWK::WindowBar::closeRequested, this, &QWidget::close);
    
    // 设置为窗口的标题栏
    setMenuWidget(titleBar);
}

void IndependentChatWindow::initWebsocket(const QString &roomId)
{
    if (m_chatWindow) {
        m_chatWindow->initWebsocket(roomId);
    }
}
