#include "meetingselectionwidget.h"
#include "../appevent.h"
#include <QApplication>
#include <QScreen>

MeetingSelectionWidget::MeetingSelectionWidget(QWidget *parent)
    : QWidget(parent)
    , m_mainLayout(nullptr)
    , m_titleLabel(nullptr)
    , m_welcomeLabel(nullptr)
    , m_buttonFrame(nullptr)
    , m_buttonLayout(nullptr)
    , m_joinMeetingBtn(nullptr)
    , m_createMeetingBtn(nullptr)
    , m_logoutBtn(nullptr)
    , m_isAdmin(false)
{
    setupUI();
    applyStyles();
}

MeetingSelectionWidget::~MeetingSelectionWidget()
{
}

void MeetingSelectionWidget::setupUI()
{
    // 主布局
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setContentsMargins(50, 50, 50, 50);
    m_mainLayout->setSpacing(30);
    
    // 标题
    m_titleLabel = new QLabel("智能学习客户端", this);
    m_titleLabel->setAlignment(Qt::AlignCenter);
    m_titleLabel->setObjectName("titleLabel");
    
    // 欢迎信息
    m_welcomeLabel = new QLabel("欢迎使用！请选择您要进行的操作：", this);
    m_welcomeLabel->setAlignment(Qt::AlignCenter);
    m_welcomeLabel->setObjectName("welcomeLabel");
    
    // 按钮容器
    m_buttonFrame = new QFrame(this);
    m_buttonFrame->setObjectName("buttonFrame");
    m_buttonLayout = new QVBoxLayout(m_buttonFrame);
    m_buttonLayout->setContentsMargins(20, 20, 20, 20);
    m_buttonLayout->setSpacing(20);
    
    // 加入会议按钮
    m_joinMeetingBtn = new QPushButton("加入会议", this);
    m_joinMeetingBtn->setObjectName("joinMeetingBtn");
    m_joinMeetingBtn->setMinimumHeight(60);
    connect(m_joinMeetingBtn, &QPushButton::clicked, this, &MeetingSelectionWidget::onJoinMeetingClicked);
    
    // 创建会议按钮
    m_createMeetingBtn = new QPushButton("创建会议", this);
    m_createMeetingBtn->setObjectName("createMeetingBtn");
    m_createMeetingBtn->setMinimumHeight(60);
    connect(m_createMeetingBtn, &QPushButton::clicked, this, &MeetingSelectionWidget::onCreateMeetingClicked);
    
    // 退出登录按钮
    m_logoutBtn = new QPushButton("退出登录", this);
    m_logoutBtn->setObjectName("logoutBtn");
    m_logoutBtn->setMinimumHeight(40);
    connect(m_logoutBtn, &QPushButton::clicked, this, &MeetingSelectionWidget::onLogoutClicked);
    
    // 添加按钮到布局
    m_buttonLayout->addWidget(m_joinMeetingBtn);
    m_buttonLayout->addWidget(m_createMeetingBtn);
    m_buttonLayout->addStretch();
    m_buttonLayout->addWidget(m_logoutBtn);
    
    // 添加到主布局
    m_mainLayout->addStretch();
    m_mainLayout->addWidget(m_titleLabel);
    m_mainLayout->addWidget(m_welcomeLabel);
    m_mainLayout->addStretch();
    m_mainLayout->addWidget(m_buttonFrame, 0, Qt::AlignCenter);
    m_mainLayout->addStretch();
    
    // 设置按钮框架的固定宽度
    m_buttonFrame->setFixedWidth(300);
}

void MeetingSelectionWidget::setUserRoles(const QStringList &roles)
{
    m_userRoles = roles;
    m_isAdmin = roles.contains("role.admin");
    updateButtonsVisibility();
}

void MeetingSelectionWidget::setUserInfo(const QString &username, const QString &userId)
{
    m_username = username;
    m_userId = userId;
    
    // 更新欢迎信息
    if (!username.isEmpty()) {
        m_welcomeLabel->setText(QString("欢迎，%1！请选择您要进行的操作：").arg(username));
    }
}

void MeetingSelectionWidget::updateButtonsVisibility()
{
    // 所有用户都可以加入会议
    m_joinMeetingBtn->setVisible(true);
    
    // 只有管理员可以创建会议
    m_createMeetingBtn->setVisible(m_isAdmin);
}

void MeetingSelectionWidget::applyStyles()
{
    // 设置整体样式
    setStyleSheet("MeetingSelectionWidget {"
                  "    background-color: #f5f5f5;"
                  "}"

                  "#titleLabel {"
                  "    font-size: 28px;"
                  "    font-weight: bold;"
                  "    color: #2c3e50;"
                  "    margin-bottom: 10px;"
                  "}"

                  "#welcomeLabel {"
                  "    font-size: 16px;"
                  "    color: #7f8c8d;"
                  "    margin-bottom: 20px;"
                  "}"

                  "#buttonFrame {"
                  "    background-color: white;"
                  "    border: 1px solid #e0e0e0;"
                  "    border-radius: 10px;"
                  "}"

                  "#joinMeetingBtn {"
                  "    background-color: #3498db;"
                  "    color: white;"
                  "    border: none;"
                  "    border-radius: 8px;"
                  "    font-size: 16px;"
                  "    font-weight: bold;"
                  "    padding: 15px;"
                  "}"

                  "#joinMeetingBtn:hover {"
                  "    background-color: #2980b9;"
                  "}"

                  "#joinMeetingBtn:pressed {"
                  "    background-color: #21618c;"
                  "}"

                  "#createMeetingBtn {"
                  "    background-color: #27ae60;"
                  "    color: white;"
                  "    border: none;"
                  "    border-radius: 8px;"
                  "    font-size: 16px;"
                  "    font-weight: bold;"
                  "    padding: 15px;"
                  "}"

                  "#createMeetingBtn:hover {"
                  "    background-color: #229954;"
                  "}"

                  "#createMeetingBtn:pressed {"
                  "    background-color: #1e8449;"
                  "}"

                  "#logoutBtn {"
                  "    background-color: #e74c3c;"
                  "    color: white;"
                  "    border: none;"
                  "    border-radius: 6px;"
                  "    font-size: 14px;"
                  "    padding: 10px;"
                  "}"

                  "#logoutBtn:hover {"
                  "    background-color: #c0392b;"
                  "}"

                  "#logoutBtn:pressed {"
                  "    background-color: #a93226;"
                  "}");
}

void MeetingSelectionWidget::onJoinMeetingClicked()
{
    emit joinMeetingRequested();
}

void MeetingSelectionWidget::onCreateMeetingClicked()
{
    emit createMeetingRequested();
}

void MeetingSelectionWidget::onLogoutClicked()
{
    emit logoutRequested();
}
