#include "user-row-widget.h"

#include <QIcon>
#include <QStyle>
#include <QSpacerItem>
#include <QFont>
#include <QFontMetrics>
#include <QResizeEvent>

UserRowWidget::UserRowWidget(const UserRecord& user, QWidget* parent, bool showActions)
    : QWidget(parent), m_user(user), m_showActions(showActions) {
    setObjectName("UserRowWidget");
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    buildUi();
    wire();
}

QString UserRowWidget::displayId(const QString& raw) const {
    int idx = raw.indexOf(QLatin1Char(':'));
    if (idx > 0) return raw.left(idx);
    return raw;
}

void UserRowWidget::buildUi() {
    auto* layout = new QHBoxLayout(this);
    layout->setContentsMargins(12, 8, 12, 8);
    layout->setSpacing(8);

    // Three info columns: UID (fixed width), login name (expanding), full name (expanding)
    m_idLabel = new QLabel(this);
    m_idLabel->setText(QString::number(m_user.uid));
    m_idLabel->setFixedWidth(80);
    m_idLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_usernameLabel = new QLabel(this);
    m_usernameLabel->setText(m_user.username);
    m_usernameLabel->setMinimumWidth(130);
    m_usernameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    m_usernameLabel->setToolTip(m_user.username);
    m_usernameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_fullnameLabel = new QLabel(this);
    m_fullnameLabel->setText(m_user.realName.isEmpty() ? m_user.username : m_user.realName);
    m_fullnameLabel->setMinimumWidth(130);
    m_fullnameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    m_fullnameLabel->setToolTip(m_user.realName.isEmpty() ? m_user.username : m_user.realName);
    m_fullnameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    // Actions: change password button (text only) and delete icon-only tool button
    m_btnPwd = new QPushButton(tr("Change Password"), this);
    // 与顶部 changePasswordButton 共用样式选择器
    m_btnPwd->setObjectName("changePasswordButton");
    // Avoid focus rect on mouse click
    m_btnPwd->setFocusPolicy(Qt::NoFocus);
    m_btnPwd->setAutoDefault(false);
    m_btnPwd->setDefault(false);
    m_btnPwd->setVisible(m_showActions);
    
    m_btnDelete = new QToolButton(this);
    m_btnDelete->setObjectName("deleteUserButton"); 
    m_btnDelete->setIconSize(QSize(16, 16));
    m_btnDelete->setIcon(QIcon::fromTheme("list-remove-symbolic", QIcon(QLatin1String(":/plugin/icons/delete.svg"))));
    m_btnDelete->setAutoRaise(false);
    m_btnDelete->setToolTip(tr("Delete User"));
    m_btnDelete->setFocusPolicy(Qt::NoFocus);
    m_btnDelete->setVisible(m_showActions);

    // Add to layout
    layout->addWidget(m_idLabel, /*stretch*/0);
    layout->addWidget(m_usernameLabel, 1);
    layout->addWidget(m_fullnameLabel, 1);
    layout->addStretch();
    layout->addWidget(m_btnPwd, 0);
    layout->addWidget(m_btnDelete, 0);

    // Initial elision
    updateElision();
}

void UserRowWidget::wire() {
    connect(m_btnPwd, &QPushButton::clicked, this, [this]{
        if (m_btnPwd) m_btnPwd->clearFocus();
        emit changePasswordRequested(m_user.username);
    });
    connect(m_btnDelete, &QToolButton::clicked, this, [this]{
        if (m_btnDelete) m_btnDelete->clearFocus();
        emit deleteUserRequested(m_user.username);
    });
}

void UserRowWidget::updateData(const UserRecord& user) {
    m_user = user;
    if (m_idLabel) m_idLabel->setText(QString::number(m_user.uid));
    if (m_usernameLabel) {
        m_usernameLabel->setText(m_user.username);
        m_usernameLabel->setToolTip(m_user.username);
    }
    if (m_fullnameLabel) {
        const auto full = m_user.realName.isEmpty() ? m_user.username : m_user.realName;
        m_fullnameLabel->setText(full);
        m_fullnameLabel->setToolTip(full);
    }
    updateElision();
}

void UserRowWidget::updateElision() {
    if (m_usernameLabel) {
        const QString full = m_user.username;
        QFontMetrics fm(m_usernameLabel->font());
        const QString el = fm.elidedText(full, Qt::ElideRight, qMax(0, m_usernameLabel->width()));
        m_usernameLabel->setText(el);
        m_usernameLabel->setToolTip(full);
    }
    if (m_fullnameLabel) {
        const QString full = m_user.realName.isEmpty() ? m_user.username : m_user.realName;
        QFontMetrics fm(m_fullnameLabel->font());
        const QString el = fm.elidedText(full, Qt::ElideRight, qMax(0, m_fullnameLabel->width()));
        m_fullnameLabel->setText(el);
        m_fullnameLabel->setToolTip(full);
    }
}

void UserRowWidget::resizeEvent(QResizeEvent* event) {
    QWidget::resizeEvent(event);
    updateElision();
}
