/*
 * Fooyin
 * Copyright © 2023, Luke Taylor <luket@pm.me>
 *
 * Fooyin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fooyin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Fooyin.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "guigeneralpage.h"

#include "internalguisettings.h"
#include "quicksetup/quicksetupdialog.h"

#include <gui/editablelayout.h>
#include <gui/guiconstants.h>
#include <gui/guisettings.h>
#include <gui/layoutprovider.h>
#include <gui/theme/fytheme.h>
#include <gui/theme/themeregistry.h>
#include <playlist/playlistwidget.h>
#include <playlist/presetregistry.h>
#include <utils/settings/settingsmanager.h>

#include <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QStyleFactory>
#include <QVBoxLayout>

using namespace Qt::StringLiterals;

namespace Fooyin {
using namespace Settings::Gui;
using namespace Settings::Gui::Internal;

class GuiGeneralPageWidget : public SettingsPageWidget
{
    Q_OBJECT

public:
    explicit GuiGeneralPageWidget(LayoutProvider* layoutProvider, EditableLayout* editableLayout,
                                  ThemeRegistry* themeRegistry, PresetRegistry* presetRegistry,
                                  SettingsManager* settings);

    void load() override;
    void apply() override;
    void reset() override;

private:
    void showQuickSetup();
    void importLayout();
    void exportLayout();

    LayoutProvider* m_layoutProvider;
    EditableLayout* m_editableLayout;
    ThemeRegistry* m_themeRegistry;
    PresetRegistry* m_presetRegistry;
    SettingsManager* m_settings;

    QComboBox* m_styles;

    QRadioButton* m_detectIconTheme;
    QRadioButton* m_lightTheme;
    QRadioButton* m_darkTheme;
    QRadioButton* m_systemTheme;

    QCheckBox* m_showMenuBar;

    QCheckBox* m_overrideMargin;
    QSpinBox* m_editableLayoutMargin;

    QCheckBox* m_splitterHandles;
    QCheckBox* m_lockSplitters;
    QCheckBox* m_overrideSplitterHandle;
    QSpinBox* m_splitterHandleGap;

    QCheckBox* m_buttonRaise;
    QCheckBox* m_buttonStretch;
};

GuiGeneralPageWidget::GuiGeneralPageWidget(LayoutProvider* layoutProvider, EditableLayout* editableLayout,
                                           ThemeRegistry* themeRegistry, PresetRegistry* presetRegistry,
                                           SettingsManager* settings)
    : m_layoutProvider{layoutProvider}
    , m_editableLayout{editableLayout}
    , m_themeRegistry{themeRegistry}
    , m_presetRegistry{presetRegistry}
    , m_settings{settings}
    , m_styles{new QComboBox(this)}
    , m_detectIconTheme{new QRadioButton(tr("Auto-detect theme"), this)}
    , m_lightTheme{new QRadioButton(tr("Light"), this)}
    , m_darkTheme{new QRadioButton(tr("Dark"), this)}
    , m_systemTheme{new QRadioButton(tr("Use system icons"), this)}
    , m_showMenuBar{new QCheckBox(tr("Show menu bar"), this)}
    , m_overrideMargin{new QCheckBox(tr("Override root margin") + u":"_s, this)}
    , m_editableLayoutMargin{new QSpinBox(this)}
    , m_splitterHandles{new QCheckBox(tr("Show splitter handles"), this)}
    , m_lockSplitters{new QCheckBox(tr("Lock splitters"), this)}
    , m_overrideSplitterHandle{new QCheckBox(tr("Override splitter handle size") + u":"_s, this)}
    , m_splitterHandleGap{new QSpinBox(this)}
    , m_buttonRaise{new QCheckBox(tr("Raise"), this)}
    , m_buttonStretch{new QCheckBox(tr("Stretch"), this)}
{
    auto* setupBox        = new QGroupBox(tr("Setup"), this);
    auto* setupBoxLayout  = new QHBoxLayout(setupBox);
    auto* quickSetup      = new QPushButton(tr("Quick Setup"), this);
    auto* importLayoutBtn = new QPushButton(tr("Import Layout"), this);
    auto* exportLayoutBtn = new QPushButton(tr("Export Layout"), this);

    setupBoxLayout->addWidget(quickSetup);
    setupBoxLayout->addWidget(importLayoutBtn);
    setupBoxLayout->addWidget(exportLayoutBtn);

    auto* appearanceGroup       = new QGroupBox(tr("Appearance"), this);
    auto* appearanceGroupLayout = new QGridLayout(appearanceGroup);

    auto* iconThemeBox       = new QGroupBox(tr("Icons"), appearanceGroup);
    auto* iconThemeBoxLayout = new QGridLayout(iconThemeBox);

    int row{0};
    iconThemeBoxLayout->addWidget(m_detectIconTheme, row++, 0, 1, 2);
    iconThemeBoxLayout->addWidget(m_lightTheme, row, 0);
    iconThemeBoxLayout->addWidget(m_darkTheme, row++, 1);
    iconThemeBoxLayout->addWidget(m_systemTheme, row++, 0, 1, 2);
    iconThemeBoxLayout->setColumnStretch(2, 1);

    row = 0;
    appearanceGroupLayout->addWidget(new QLabel(tr("Style") + u":"_s, appearanceGroup), row, 0);
    appearanceGroupLayout->addWidget(m_styles, row++, 1);
    appearanceGroupLayout->addWidget(iconThemeBox, row++, 0, 1, 2);
    appearanceGroupLayout->setColumnStretch(1, 1);

    auto* layoutGroup       = new QGroupBox(tr("Layout"), this);
    auto* layoutGroupLayout = new QGridLayout(layoutGroup);

    row = 0;
    layoutGroupLayout->addWidget(m_showMenuBar, row++, 0, 1, 3);
    layoutGroupLayout->addWidget(m_splitterHandles, row++, 0, 1, 3);
    layoutGroupLayout->addWidget(m_lockSplitters, row++, 0, 1, 3);
    layoutGroupLayout->addWidget(m_overrideSplitterHandle, row, 0);
    layoutGroupLayout->addWidget(m_splitterHandleGap, row++, 1);
    layoutGroupLayout->addWidget(m_overrideMargin, row, 0);
    layoutGroupLayout->addWidget(m_editableLayoutMargin, row++, 1);
    layoutGroupLayout->setColumnStretch(2, 1);

    m_editableLayoutMargin->setRange(0, 20);
    m_editableLayoutMargin->setSuffix(u" px"_s);

    m_splitterHandleGap->setRange(0, 20);
    m_splitterHandleGap->setSuffix(u" px"_s);

    auto* toolButtonGroup       = new QGroupBox(tr("Tool Buttons"), this);
    auto* toolButtonGroupLayout = new QVBoxLayout(toolButtonGroup);

    toolButtonGroupLayout->addWidget(m_buttonRaise);
    toolButtonGroupLayout->addWidget(m_buttonStretch);

    auto* mainLayout = new QGridLayout(this);

    row = 0;
    mainLayout->addWidget(setupBox, row++, 0, 1, 2);
    mainLayout->addWidget(appearanceGroup, row++, 0, 1, 2);
    mainLayout->addWidget(layoutGroup, row++, 0, 1, 2);
    mainLayout->addWidget(toolButtonGroup, row++, 0, 1, 2);
    mainLayout->setColumnStretch(1, 1);
    mainLayout->setRowStretch(mainLayout->rowCount(), 1);

    QObject::connect(quickSetup, &QPushButton::clicked, this, &GuiGeneralPageWidget::showQuickSetup);
    QObject::connect(importLayoutBtn, &QPushButton::clicked, this, &GuiGeneralPageWidget::importLayout);
    QObject::connect(exportLayoutBtn, &QPushButton::clicked, this, &GuiGeneralPageWidget::exportLayout);

    QObject::connect(m_overrideMargin, &QCheckBox::toggled, m_editableLayoutMargin, &QWidget::setEnabled);
    QObject::connect(m_overrideSplitterHandle, &QCheckBox::toggled, m_splitterHandleGap, &QWidget::setEnabled);
}

void GuiGeneralPageWidget::load()
{
    m_styles->clear();
    m_styles->addItem(tr("System default"));
    m_styles->addItems(QStyleFactory::keys());

    const auto style     = m_settings->value<Style>();
    const int styleIndex = m_styles->findText(style);
    m_styles->setCurrentIndex(style.isEmpty() || styleIndex < 0 ? 0 : styleIndex);

    const auto iconTheme = static_cast<IconThemeOption>(m_settings->value<IconTheme>());
    switch(iconTheme) {
        case IconThemeOption::AutoDetect:
            m_detectIconTheme->setChecked(true);
            break;
        case IconThemeOption::System:
            m_systemTheme->setChecked(true);
            break;
        case IconThemeOption::Light:
            m_lightTheme->setChecked(true);
            break;
        case IconThemeOption::Dark:
            m_darkTheme->setChecked(true);
            break;
    }

    m_showMenuBar->setChecked(m_settings->value<ShowMenuBar>());

    m_splitterHandles->setChecked(m_settings->value<ShowSplitterHandles>());
    m_lockSplitters->setChecked(m_settings->value<LockSplitterHandles>());

    m_overrideMargin->setChecked(m_settings->value<EditableLayoutMargin>() >= 0);
    m_editableLayoutMargin->setValue(m_settings->value<EditableLayoutMargin>());
    m_editableLayoutMargin->setEnabled(m_overrideMargin->isChecked());

    m_overrideSplitterHandle->setChecked(m_settings->value<SplitterHandleSize>() >= 0);
    m_splitterHandleGap->setValue(m_settings->value<SplitterHandleSize>());
    m_splitterHandleGap->setEnabled(m_overrideSplitterHandle->isChecked());

    const auto buttonOptions = m_settings->value<ToolButtonStyle>();
    m_buttonRaise->setChecked(buttonOptions & Raise);
    m_buttonStretch->setChecked(buttonOptions & Stretch);
}

void GuiGeneralPageWidget::apply()
{
    m_settings->set<Style>(m_styles->currentIndex() == 0 ? QString{} : m_styles->currentText());

    IconThemeOption iconThemeOption;
    if(m_detectIconTheme->isChecked()) {
        iconThemeOption = IconThemeOption::AutoDetect;
    }
    else if(m_lightTheme->isChecked()) {
        iconThemeOption = IconThemeOption::Light;
    }
    else if(m_darkTheme->isChecked()) {
        iconThemeOption = IconThemeOption::Dark;
    }
    else {
        iconThemeOption = IconThemeOption::System;
    }
    m_settings->set<IconTheme>(static_cast<int>(iconThemeOption));

    m_settings->set<ShowMenuBar>(m_showMenuBar->isChecked());

    m_settings->set<ShowSplitterHandles>(m_splitterHandles->isChecked());
    m_settings->set<LockSplitterHandles>(m_lockSplitters->isChecked());

    if(m_overrideMargin->isChecked()) {
        m_settings->set<EditableLayoutMargin>(m_editableLayoutMargin->value());
    }
    else {
        m_settings->reset<EditableLayoutMargin>();
    }

    if(m_overrideSplitterHandle->isChecked()) {
        m_settings->set<SplitterHandleSize>(m_splitterHandleGap->value());
    }
    else {
        m_settings->reset<SplitterHandleSize>();
    }

    ToolButtonOptions buttonOptions;
    buttonOptions.setFlag(Raise, m_buttonRaise->isChecked());
    buttonOptions.setFlag(Stretch, m_buttonStretch->isChecked());

    m_settings->set<ToolButtonStyle>(static_cast<int>(buttonOptions));
}

void GuiGeneralPageWidget::reset()
{
    m_settings->reset<Style>();
    m_settings->reset<IconTheme>();
    m_settings->reset<ShowMenuBar>();
    m_settings->reset<ShowSplitterHandles>();
    m_settings->reset<LockSplitterHandles>();
    m_settings->reset<EditableLayoutMargin>();
    m_settings->reset<SplitterHandleSize>();
    m_settings->reset<ToolButtonStyle>();
}

void GuiGeneralPageWidget::showQuickSetup()
{
    auto* quickSetup
        = new QuickSetupDialog(m_layoutProvider, m_themeRegistry, m_presetRegistry, m_editableLayout, m_settings, this);
    quickSetup->setAttribute(Qt::WA_DeleteOnClose);
    quickSetup->show();
}

void GuiGeneralPageWidget::importLayout()
{
    m_layoutProvider->importLayout(this);
}

void GuiGeneralPageWidget::exportLayout()
{
    m_editableLayout->exportLayout(this);
}

GuiGeneralPage::GuiGeneralPage(LayoutProvider* layoutProvider, EditableLayout* editableLayout,
                               ThemeRegistry* themeRegistry, PresetRegistry* presetRegistry, SettingsManager* settings,
                               QObject* parent)
    : SettingsPage{settings->settingsDialog(), parent}
{
    setId(Constants::Page::InterfaceGeneral);
    setName(tr("General"));
    setCategory({tr("Interface")});
    setWidgetCreator([layoutProvider, editableLayout, themeRegistry, presetRegistry, settings] {
        return new GuiGeneralPageWidget(layoutProvider, editableLayout, themeRegistry, presetRegistry, settings);
    });
}
} // namespace Fooyin

#include "guigeneralpage.moc"
#include "moc_guigeneralpage.cpp"
