#include "moderndialogbase.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>


#include <QWKWidgets/widgetwindowagent.h>
#include <widgetframe/windowbar.h>
#include <widgetframe/windowbutton.h>

ModernDialogBase::ModernDialogBase(QWidget *parent,
                                   bool disableNativeTransitions)
    : QDialog(parent) {
    Q_UNUSED(disableNativeTransitions);
    
    setWindowFlag(Qt::WindowContextHelpButtonHint, false);

    
    setAttribute(Qt::WA_StyledBackground, true);

    auto *mainLayout = new QVBoxLayout(this);
    
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    
    m_titleBarWidget = new QWidget(this);
    m_titleBarWidget->setObjectName("dialog-titlebar");

    auto *titleBarLayout = new QHBoxLayout(m_titleBarWidget);
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
    
    titleBarLayout->setContentsMargins(52, 0, 16, 0);
#else
    
    titleBarLayout->setContentsMargins(16, 0, 0, 0);
#endif
    titleBarLayout->setSpacing(0);

    m_titleLabel = new QLabel(m_titleBarWidget);
    m_titleLabel->setObjectName("dialog-title");

    
    auto *agent = new QWK::WidgetWindowAgent(this);
    agent->setup(this);
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
    agent->setWindowAttribute("no-system-buttons", false);
    agent->setWindowAttribute("macos-close-button-only", true);
#endif

#if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
    
    auto *closeBtn = new QWK::WindowButton(m_titleBarWidget);
    closeBtn->setObjectName("dialog-close-btn");
    closeBtn->setProperty("system-button", true); 
    connect(closeBtn, &QWK::WindowButton::clicked, this, &QDialog::reject);
#endif

    titleBarLayout->addWidget(m_titleLabel);
    titleBarLayout->addStretch();
#if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
    titleBarLayout->addWidget(closeBtn);
#endif

    
    agent->setTitleBar(m_titleBarWidget);
#if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
    agent->setSystemButton(QWK::WindowAgentBase::Close, closeBtn);
#endif

    
    m_contentLayout = new QVBoxLayout();
    m_contentLayout->setContentsMargins(20, 10, 20, 20); 

    mainLayout->addWidget(m_titleBarWidget);
    mainLayout->addLayout(m_contentLayout);
}

void ModernDialogBase::setTitle(const QString &title) {
    m_titleLabel->setText(title);
}
