#include "ui/settingsview.h"
#include "services/accountservice.h"
#include "core/models/account.h"

SettingsView::SettingsView(QWidget *parent) : QWidget(parent) {
    setupUI();
}

void SettingsView::setupUI() {
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    m_tabWidget = new QTabWidget();
    m_tabWidget->addTab(createAccountsTab(), "Accounts");
    m_tabWidget->addTab(createGeneralTab(), "General");
    m_tabWidget->addTab(createAppearanceTab(), "Appearance");

    mainLayout->addWidget(m_tabWidget);
}

QWidget* SettingsView::createAccountsTab() {
    QWidget *w = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout(w);
    layout->setContentsMargins(20, 20, 20, 20);
    layout->setSpacing(15);

    QLabel *sectionTitle = new QLabel("Email Accounts");
    QFont titleFont = sectionTitle->font();
    titleFont.setPointSize(16);
    titleFont.setBold(true);
    sectionTitle->setFont(titleFont);
    layout->addWidget(sectionTitle);

    m_accountList = new QListWidget();
    m_accountList->setAlternatingRowColors(true);
    m_accountList->setFrameShape(QFrame::NoFrame);
    m_accountList->setStyleSheet(
        "QListWidget { background: #ffffff; border: 1px solid #e0e0e0; border-radius: 4px; }"
        "QListWidget::item { padding: 12px; border-bottom: 1px solid #f0f0f0; }"
        "QListWidget::item:selected { background: #e3f2fd; }"
    );
    layout->addWidget(m_accountList, 1);

    QPushButton *addBtn = new QPushButton("+ Add Email Account");
    addBtn->setStyleSheet(
        "QPushButton { background: #1976D2; color: white; border: none; border-radius: 4px; padding: 10px 20px; font-weight: bold; }"
        "QPushButton:hover { background: #1565C0; }"
    );
    connect(addBtn, &QPushButton::clicked, this, &SettingsView::accountAddRequested);
    layout->addWidget(addBtn);

    return w;
}

QWidget* SettingsView::createGeneralTab() {
    QWidget *w = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout(w);
    layout->setContentsMargins(20, 20, 20, 20);
    layout->setSpacing(15);

    QLabel *sectionTitle = new QLabel("General Settings");
    QFont titleFont = sectionTitle->font();
    titleFont.setPointSize(16);
    titleFont.setBold(true);
    sectionTitle->setFont(titleFont);
    layout->addWidget(sectionTitle);

    QCheckBox *startOnLogin = new QCheckBox("Start application on login");
    startOnLogin->setChecked(true);
    connect(startOnLogin, &QCheckBox::toggled, [this](bool checked) {
        emit settingChanged("start_on_login", checked);
    });
    layout->addWidget(startOnLogin);

    QCheckBox *enableNotifications = new QCheckBox("Enable notifications");
    enableNotifications->setChecked(true);
    connect(enableNotifications, &QCheckBox::toggled, [this](bool checked) {
        emit settingChanged("notifications_enabled", checked);
    });
    layout->addWidget(enableNotifications);

    QCheckBox *minimizeToTray = new QCheckBox("Minimize to tray on close");
    minimizeToTray->setChecked(true);
    connect(minimizeToTray, &QCheckBox::toggled, [this](bool checked) {
        emit settingChanged("minimize_to_tray", checked);
    });
    layout->addWidget(minimizeToTray);

    layout->addSpacing(10);

    QHBoxLayout *syncLayout = new QHBoxLayout();
    QLabel *syncLabel = new QLabel("Sync Interval:");
    syncLabel->setStyleSheet("font-weight: bold; color: #555;");
    m_syncIntervalCombo = new QComboBox();
    m_syncIntervalCombo->addItem("15 minutes", 15);
    m_syncIntervalCombo->addItem("30 minutes", 30);
    m_syncIntervalCombo->addItem("60 minutes", 60);
    m_syncIntervalCombo->addItem("120 minutes", 120);
    m_syncIntervalCombo->setCurrentIndex(1);
    connect(m_syncIntervalCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int idx) {
        emit settingChanged("sync_interval", m_syncIntervalCombo->itemData(idx));
    });
    syncLayout->addWidget(syncLabel);
    syncLayout->addWidget(m_syncIntervalCombo);
    syncLayout->addStretch();
    layout->addLayout(syncLayout);

    layout->addStretch();
    return w;
}

QWidget* SettingsView::createAppearanceTab() {
    QWidget *w = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout(w);
    layout->setContentsMargins(20, 20, 20, 20);
    layout->setSpacing(15);

    QLabel *sectionTitle = new QLabel("Appearance");
    QFont titleFont = sectionTitle->font();
    titleFont.setPointSize(16);
    titleFont.setBold(true);
    sectionTitle->setFont(titleFont);
    layout->addWidget(sectionTitle);

    QLabel *themeLabel = new QLabel("Theme:");
    themeLabel->setStyleSheet("font-weight: bold; color: #555;");
    layout->addWidget(themeLabel);

    m_themeGroup = new QButtonGroup(this);
    QRadioButton *lightRadio = new QRadioButton("Light");
    QRadioButton *darkRadio = new QRadioButton("Dark");
    QRadioButton *systemRadio = new QRadioButton("System");
    lightRadio->setChecked(true);

    m_themeGroup->addButton(lightRadio, 0);
    m_themeGroup->addButton(darkRadio, 1);
    m_themeGroup->addButton(systemRadio, 2);

    connect(m_themeGroup, QOverload<int>::of(&QButtonGroup::idClicked), [this](int id) {
        QString theme;
        switch (id) {
            case 0: theme = "light"; break;
            case 1: theme = "dark"; break;
            case 2: theme = "system"; break;
        }
        emit themeChanged(theme);
    });

    layout->addWidget(lightRadio);
    layout->addWidget(darkRadio);
    layout->addWidget(systemRadio);

    layout->addSpacing(15);

    QCheckBox *deepinTheme = new QCheckBox("Use Deepin theme");
    deepinTheme->setChecked(true);
    connect(deepinTheme, &QCheckBox::toggled, [this](bool checked) {
        emit settingChanged("deepin_theme", checked);
    });
    layout->addWidget(deepinTheme);

    layout->addStretch();
    return w;
}