#include "themesettingswidget.h"

#include <QApplication>
#include <QButtonGroup>
#include <QCheckBox>
#include <QColorDialog>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPainter>
#include <QPushButton>
#include <QRadioButton>
#include <QSlider>
#include <QStyle>
#include <QVBoxLayout>

#include "thememanager.h"
#include "themesettingswidget.h"
#include "widgets/colorlistwidget.h"

ThemeSettingsWidget::ThemeSettingsWidget(QWidget *parent)
    : TWidget(parent)
{
    setupUI();
}

void ThemeSettingsWidget::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(25);

    // 主题切换按钮
    m_themeToggle = new QPushButton(this);
    m_themeToggle->setCheckable(true);
    m_themeToggle->setFixedSize(80, 40);
    connect(m_themeToggle, &QPushButton::toggled, this, &ThemeSettingsWidget::toggleThemeMode);
    mainLayout->addWidget(m_themeToggle, 0, Qt::AlignHCenter);

    // 颜色选择按钮
    m_colorPicker = new QPushButton(tr("选择主题色"), this);
    m_colorPicker->setFixedHeight(40);
    connect(m_colorPicker, &QPushButton::clicked, this, &ThemeSettingsWidget::updatePrimaryColor);
    mainLayout->addWidget(m_colorPicker);

    // 尺寸选项
    mainLayout->addWidget(createSizeGroup());

    // 预览区域
    mainLayout->addWidget(createPreviewGroup());

    this->mainLayout()->addLayout(mainLayout);
    // 初始化状态
    updateColorButton();
    refreshPreview();
}

QGroupBox *ThemeSettingsWidget::createSizeGroup()
{
    QGroupBox *group = new QGroupBox(tr("控件尺寸"), this);
    QHBoxLayout *layout = new QHBoxLayout(group);

    m_sizeGroup = new QButtonGroup(this);
    const QList<QPair<QString, int>> sizes = {{tr("紧凑"), ThemeManager::Small},
                                              {tr("标准"), ThemeManager::Medium},
                                              {tr("宽松"), ThemeManager::Large}};

    for (const auto &[text, mode] : sizes) {
        QRadioButton *rb = new QRadioButton(text, group);
        rb->setProperty("sizeMode", mode);
        m_sizeGroup->addButton(rb, mode);
        layout->addWidget(rb);
    }

    connect(m_sizeGroup,
            QOverload<int>::of(&QButtonGroup::idClicked),
            this,
            &ThemeSettingsWidget::changeSizeMode);

    m_sizeGroup->button(ThemeManager::Medium)->setChecked(true);
    return group;
}

QWidget *ThemeSettingsWidget::createPreviewGroup()
{
    QWidget *preview = new QWidget(this);
    QHBoxLayout *hbox = new QHBoxLayout(preview);

    QVBoxLayout *layout = new QVBoxLayout();
    layout->setContentsMargins(15, 15, 15, 15);
    layout->setSpacing(12);

    // 示例控件
    m_sampleButton = new QPushButton(tr("示例按钮"), preview);
    m_sampleInput = new QLineEdit(preview);
    m_sampleCheck = new QCheckBox(tr("示例复选框"), preview);
    m_sampleSlider = new QSlider(Qt::Horizontal, preview);

    m_sampleInput->setPlaceholderText(tr("输入内容..."));
    m_sampleSlider->setRange(0, 100);
    m_sampleSlider->setValue(50);

    layout->addWidget(m_sampleButton);
    layout->addWidget(m_sampleInput);
    layout->addWidget(m_sampleCheck);
    layout->addWidget(m_sampleSlider);

    colorList = new ColorListWidget;
    colorList->setColors(ThemeManager::instance().colors());

    hbox->addLayout(layout);
    hbox->addWidget(colorList);

    return preview;
}

void ThemeSettingsWidget::toggleThemeMode()
{
    ThemeManager::instance().toggleThemeMode();
    updateColorButton();
    refreshPreview();
    emit themeChanged();
}

void ThemeSettingsWidget::updatePrimaryColor()
{
    QColor color = QColorDialog::getColor(ThemeManager::instance().primaryColor(),
                                          this,
                                          tr("选择主题颜色"),
                                          QColorDialog::DontUseNativeDialog
                                              | QColorDialog::ShowAlphaChannel);

    if (color.isValid()) {
        ThemeManager::instance().setPrimaryColor(color);
        updateColorButton();
        refreshPreview();
        emit themeChanged();
    }
}

void ThemeSettingsWidget::changeSizeMode(int mode)
{
    ThemeManager::instance().setSizeMode(static_cast<ThemeManager::SizeMode>(mode));
    refreshPreview();
    emit themeChanged();
}

void ThemeSettingsWidget::updateColorButton()
{
    const QColor primary = ThemeManager::instance().primaryColor();
    const QColor text = ThemeManager::instance().color("colorText");

    QString style = QString(R"(
        QPushButton {
            background: %1;
            border: 2px solid %2;
            border-radius: 6px;
            color: %3;
            font-weight: 500;
        }
        QPushButton:hover {
            background: %4;
            border-color: %5;
        }
    )")
                        .arg(primary.name(),
                             primary.darker(115).name(),
                             text.name(),
                             primary.lighter(110).name(),
                             primary.darker(125).name());

    m_colorPicker->setStyleSheet(style);
}

void ThemeSettingsWidget::refreshPreview()
{
    // 更新切换按钮样式
    QString themeStyle = ThemeManager::instance().themeMode() == ThemeManager::Light
                             ? "background: qradialgradient(cx:0.5, cy:0.5, radius: 0.5, fx:0.7, "
                               "fy:0.3, stop:0 #FFD700, stop:1 #FFA500);"
                             : "background: qradialgradient(cx:0.5, cy:0.5, radius: 0.5, fx:0.3, "
                               "fy:0.7, stop:0 #5A5A5A, stop:1 #2D2D2D);";

    m_themeToggle->setStyleSheet(QString(R"(
        QPushButton {
            border-radius: 20px;
            border: 2px solid palette(shadow);
            %1
        }
        QPushButton:hover {
            border-color: palette(highlight);
        }
    )")
                                     .arg(themeStyle));

    m_themeToggle->setText(ThemeManager::instance().themeMode() == ThemeManager::Dark ? "Dark"
                                                                                      : "Light");
    colorList->setColors(ThemeManager::instance().colors());

    // 强制刷新样式
    qApp->setStyleSheet(qApp->styleSheet());
}
