#include "message-dialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QApplication>
#include <QTimer>
#include <QShowEvent>
#include <QGraphicsDropShadowEffect>

MessageDialog::MessageDialog(Type type, const QString& title, const QString& message, QWidget* parent)
    : LayeredDialog(title, 400, parent, false) // disable size reduction for message popups
{
    // Error dialogs stay on top
    if (type == Error) {
        setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
    }
    setupUI(type, title, message);
    // Remove header bar for error dialogs per new requirement (only icon, title, message, button)
    if (type == Error) {
        setHeaderVisible(false);
    }
    QTimer::singleShot(0, this, [this]() { raise(); activateWindow(); });
}

void MessageDialog::showEvent(QShowEvent* event)
{
    LayeredDialog::showEvent(event);
    raise();
    activateWindow();
}


void MessageDialog::setupUI(Type type, const QString& title, const QString& message)
{
    // Use LayeredDialog's body container
    QWidget* body = bodyContainer();
    auto* cardLayout = bodyLayout();
    // Override margins & spacing (error dialog has specific padding & spacing requirements)
    if (type == Error) {
        // Error dialog: all sides 24px, spacing 12px between icon/title/message
        cardLayout->setContentsMargins(0,16,0,16);
        cardLayout->setSpacing(12);
    } else {
        // Non-error retain previous style
        cardLayout->setContentsMargins(32,32,32,32);
        cardLayout->setSpacing(20);
    }

    // Icon container
    auto* iconLabel = new QLabel(body);
    iconLabel->setObjectName("DialogIcon");
    iconLabel->setAlignment(Qt::AlignCenter);
    iconLabel->setFixedSize(64, 64);

    if (type == Error) {
        // Use external SVG resource for error icon
        QPixmap pix(":/plugin/icons/dialog-error.svg");
        if (!pix.isNull()) {
            iconLabel->setPixmap(pix.scaled(64,64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        } else {
            // Fallback minimal style if resource missing (QSS provides gradient if property set)
            iconLabel->setProperty("dialogType", "error");
            iconLabel->setText("✕");
        }
    } else if (type == Success) {
        iconLabel->setProperty("dialogType", "success");
        iconLabel->setText("✓");
    } else {
        iconLabel->setProperty("dialogType", "info");
        iconLabel->setText("i");
    }
    cardLayout->insertWidget(0, iconLabel, 0, Qt::AlignCenter);

    // Title
    auto* titleLabel = new QLabel(title, body);
    titleLabel->setObjectName("DialogTitle");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setWordWrap(true);
    QFont titleFont;
    titleFont.setPixelSize(16);
    titleFont.setWeight(QFont::Medium);
    titleLabel->setFont(titleFont);
    // Color via QSS (#DialogTitle)
    cardLayout->addWidget(titleLabel);

    // Message (if not empty)
    if (!message.isEmpty()) {
    auto* msgLabel = new QLabel(message, body);
        msgLabel->setAlignment(Qt::AlignCenter);
        msgLabel->setWordWrap(true);
        QFont msgFont;
        msgFont.setPixelSize(14);
        msgFont.setWeight(QFont::Normal);
        msgLabel->setFont(msgFont);
        msgLabel->setObjectName("DialogMessage");
        // Color via QSS (#DialogMessage)
    cardLayout->addWidget(msgLabel);
    }

    // Confirm button
    auto* confirmBtn = new QPushButton(tr("Confirm"), body);
    confirmBtn->setObjectName("DialogConfirmButton");
    confirmBtn->setFixedHeight(36);
    confirmBtn->setMinimumWidth(100);
    confirmBtn->setCursor(Qt::PointingHandCursor);
    // Styling now via QSS (#DialogConfirmButton)
    
    auto* btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(confirmBtn);
    btnLayout->addStretch();
    if (type == Error) {
        // Push button to bottom by inserting a stretch before it so it stays visually separated
        cardLayout->addStretch();
    }
    cardLayout->addLayout(btnLayout);

    // Card styling
    // Use shared card styling (#Card already styled via panel-common.qss). Add shadow if missing.
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(body);
    shadow->setOffset(4,4);
    shadow->setBlurRadius(12);
    shadow->setColor(QColor(0,0,0,46));
    body->setGraphicsEffect(shadow);
    
    connect(confirmBtn, &QPushButton::clicked, this, &QDialog::accept);

    // LayeredDialog sets width; ensure size adapts
    adjustSize();
}

// Static helpers
void MessageDialog::showError(const QString& title, const QString& message, QWidget* parent)
{
    // Backward compatible blocking helper
    MessageDialog dlg(Error, title, message, parent);
    dlg.setWindowModality(Qt::ApplicationModal); // escalate modality for root error dialog
    dlg.exec();
}


void MessageDialog::showInfo(const QString& title, const QString& message, QWidget* parent)
{
    MessageDialog dlg(Info, title, message, parent);
    dlg.exec();
}
