#include "userprofilewidget.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QPainter>
#include <QPainterPath>
#include <QStyle>

UserProfileWidget::UserProfileWidget(QWidget *parent)
    : QWidget(parent)
    , avatarLabel(nullptr)
    , usernameLabel(nullptr)
    , statusLabel(nullptr)
    , settingsButton(nullptr)
    , logoutButton(nullptr)
{
    setupUI();

    // 设置默认值
    setUsername("用户名");
    setStatus("在线");

    // 设置默认头像
    QPixmap defaultAvatar = QPixmap(64, 64);
    defaultAvatar.fill(Qt::gray);

    setAvatar(defaultAvatar);

    setMaximumHeight(165);
}

UserProfileWidget::~UserProfileWidget() {}

void UserProfileWidget::setupUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 10);
    mainLayout->setSpacing(5);

    // 创建头像和用户名的水平布局
    QHBoxLayout *profileLayout = new QHBoxLayout();

    // 创建头像标签
    avatarLabel = new QLabel(this);
    avatarLabel->setAlignment(Qt::AlignCenter);
    avatarLabel->setMinimumSize(48, 48);
    avatarLabel->setMaximumSize(48, 48);
    avatarLabel->setScaledContents(true);
    avatarLabel->setCursor(Qt::PointingHandCursor);

    // 创建用户信息的垂直布局
    QVBoxLayout *userInfoLayout = new QVBoxLayout();

    // 创建用户名标签
    usernameLabel = new QLabel(this);
    QFont usernameFont = usernameLabel->font();
    usernameFont.setBold(true);
    usernameFont.setPointSize(usernameFont.pointSize() + 1);
    usernameLabel->setFont(usernameFont);

    // 创建状态标签
    statusLabel = new QLabel(this);

    // 将用户名和状态添加到用户信息布局
    userInfoLayout->addWidget(usernameLabel);
    userInfoLayout->addWidget(statusLabel);
    userInfoLayout->setContentsMargins(0, 0, 0, 0);
    userInfoLayout->setSpacing(2);

    // 将头像和用户信息添加到水平布局
    profileLayout->addWidget(avatarLabel);
    profileLayout->addLayout(userInfoLayout, 1);
    profileLayout->setSpacing(10);

    // 创建按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(5);

    // 创建设置按钮
    settingsButton = new QPushButton(this);
    settingsButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileDialogDetailedView));
    settingsButton->setToolTip("设置");
    settingsButton->setFlat(true);

    // 创建登出按钮
    logoutButton = new QPushButton(this);
    logoutButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton));
    logoutButton->setToolTip("退出登录");
    logoutButton->setFlat(true);

    // 添加按钮到布局
    buttonLayout->addWidget(settingsButton);
    buttonLayout->addWidget(logoutButton);

    // 添加所有组件到主布局
    mainLayout->addLayout(profileLayout);
    mainLayout->addLayout(buttonLayout);
    mainLayout->addStretch();

    // // 设置样式
    // setStyleSheet("QLabel#avatarLabel { border-radius: 24px; border: 1px solid #cccccc; }"
    //               "QLabel#usernameLabel { color: #333333; }"
    //               "QLabel#statusLabel { color: #666666; }");

    // 连接信号和槽
    connect(avatarLabel, &QLabel::linkActivated, this, &UserProfileWidget::profileClicked);
    connect(settingsButton, &QPushButton::clicked, this, &UserProfileWidget::settingsClicked);
    connect(logoutButton, &QPushButton::clicked, this, &UserProfileWidget::logoutClicked);
}

void UserProfileWidget::setUsername(const QString &username)
{
    usernameLabel->setText(username);
}

void UserProfileWidget::setAvatar(const QPixmap &avatar)
{
    // 创建圆形头像
    QPixmap roundedAvatar(avatar.size());
    roundedAvatar.fill(Qt::transparent);

    QPainter painter(&roundedAvatar);
    painter.setRenderHint(QPainter::Antialiasing);

    QPainterPath path;
    path.addEllipse(0, 0, avatar.width(), avatar.height());
    painter.setClipPath(path);
    painter.drawPixmap(0, 0, avatar);

    avatarLabel->setPixmap(roundedAvatar);
}

void UserProfileWidget::setStatus(const QString &status)
{
    if (statusLabel) {
        statusLabel->setText(status);
    }
}
