#include "error-panel.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include "../DialogButton/dialog-button.h"
#include <QFont>
#include <QPixmap>

ErrorPanel::ErrorPanel(const QString &title, const QString &message, QWidget *parent)
    : QWidget(parent)
{
    auto *layout = new QVBoxLayout(this);
    // 内部上下 16，加上外层 PanelBody 24 => 总视觉 40；元素间距 12
    layout->setContentsMargins(0,16,0,16);
    layout->setSpacing(12);

    // Icon on top, centered, 84x84
    auto *iconLabel = new QLabel(this);
    iconLabel->setObjectName("DialogIcon");
    iconLabel->setAlignment(Qt::AlignCenter);
    iconLabel->setFixedSize(84, 84);
    {
        QPixmap pix(":/plugin/icons/dialog-error.svg");
        if (!pix.isNull()) {
            iconLabel->setPixmap(pix.scaled(84,84, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        } else {
            // Fallback: textual glyph if resource missing
            iconLabel->setText("✕");
        }
    }
    layout->addWidget(iconLabel, 0, Qt::AlignHCenter);

    m_titleLabel = new QLabel(title, this);
    QFont tf = m_titleLabel->font(); tf.setPixelSize(18); tf.setWeight(QFont::Weight::Medium); m_titleLabel->setFont(tf);
    m_titleLabel->setObjectName("DialogTitle");
    m_titleLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(m_titleLabel, 0, Qt::AlignHCenter);

    if (!message.isEmpty()) {
        m_messageLabel = new QLabel(message, this);
        QFont mf = m_messageLabel->font(); mf.setPixelSize(14); mf.setWeight(QFont::Normal); m_messageLabel->setFont(mf);
        m_messageLabel->setObjectName("DialogMessage");
        m_messageLabel->setWordWrap(true);
        m_messageLabel->setAlignment(Qt::AlignCenter);
        // Allow vertical expansion as content wraps; no fixed height.
        m_messageLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
        layout->addWidget(m_messageLabel, 0, Qt::AlignHCenter);
    }

    m_confirmButton = new DialogButton(tr("Confirm"), DialogButton::Confirm);
    // 保持 DialogButton 自带的 objectName=PrimaryButton 以应用其圆角样式；不要覆盖为未定义的选择器
    // 若需与旧 #DialogConfirmButton 名称兼容，可追加样式映射
    m_confirmButton->setFixedHeight(36);
    m_confirmButton->setMinimumWidth(120);
    auto *btnRow = new QHBoxLayout();
    btnRow->addStretch();
    btnRow->addWidget(m_confirmButton);
    btnRow->addStretch();
    // Ensure 32px gap from the last text block to the button row
    layout->addSpacing(32);
    layout->addLayout(btnRow);

    connect(m_confirmButton, &QToolButton::clicked, this, [this]{ emit confirmed(); });
}
