/*
 * SPDX-FileCopyrightText: 2026 Laurent Montel <montel@kde.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "configuresettingsdialog.hpp"
#include "general/configuregeneralwidget.hpp"
#include "input/configureinputwidget.hpp"
#include "misc/configuremiscwidget.hpp"
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
#include <KWindowConfig>
#include <QPushButton>
#include <QWindow>

namespace
{
const char myConfigGroupName[] = "ConfigureSettingsDialog";
}
using namespace Qt::Literals::StringLiterals;
ConfigureSettingsDialog::ConfigureSettingsDialog(QWidget *parent)
    : KPageDialog(parent)
    , mConfigureGeneralWidget(new ConfigureGeneralWidget(this))
    , mConfigureInputWidget(new ConfigureInputWidget(this))
#if WITH_DBUS
    , mConfigureMiscWidget(new ConfigureMiscWidget(this))
#endif
{
    setWindowTitle(i18nc("@title:window", "Configure Drawy"));
    setFaceType(KPageDialog::List);

    const QString generalPageName = i18n("General");
    mConfigureGeneralWidgetPage = new KPageWidgetItem(mConfigureGeneralWidget, generalPageName);
    mConfigureGeneralWidgetPage->setIcon(QIcon::fromTheme(u"settings-configure"_s));
    addPage(mConfigureGeneralWidgetPage);

    const QString inputPageName = i18n("Input");
    mConfigureInputWidgetPage = new KPageWidgetItem(mConfigureInputWidget, inputPageName);
    mConfigureInputWidgetPage->setIcon(QIcon::fromTheme(u"dialog-input-devices-symbolic"_s));
    addPage(mConfigureInputWidgetPage);

#if WITH_DBUS
    const QString miscPageName = i18n("Misc");
    mConfigureMiscWidgetPage = new KPageWidgetItem(mConfigureMiscWidget, miscPageName);
    mConfigureMiscWidgetPage->setIcon(QIcon::fromTheme(u"preferences-other"_s));
    addPage(mConfigureMiscWidgetPage);
#endif
    buttonBox()->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);

    connect(buttonBox()->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &ConfigureSettingsDialog::slotAccepted);
    connect(buttonBox()->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &ConfigureSettingsDialog::reject);
    connect(buttonBox()->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &ConfigureSettingsDialog::slotRestoreDefaults);
    readConfig();
    load();
}

ConfigureSettingsDialog::~ConfigureSettingsDialog()
{
    writeConfig();
}

void ConfigureSettingsDialog::readConfig()
{
    create(); // ensure a window is created
    windowHandle()->resize(QSize(800, 600));
    const KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
    KWindowConfig::restoreWindowSize(windowHandle(), group);
    resize(windowHandle()->size()); // workaround for QTBUG-40584
}

void ConfigureSettingsDialog::writeConfig()
{
    KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
    KWindowConfig::saveWindowSize(windowHandle(), group);
}

void ConfigureSettingsDialog::slotAccepted()
{
    mConfigureGeneralWidget->save();
    mConfigureInputWidget->save();
#if WITH_DBUS
    mConfigureMiscWidget->save();
#endif
}

void ConfigureSettingsDialog::load()
{
    mConfigureGeneralWidget->load();
    mConfigureInputWidget->load();
#if WITH_DBUS
    mConfigureMiscWidget->load();
#endif
}

void ConfigureSettingsDialog::slotRestoreDefaults()
{
    mConfigureGeneralWidget->restoreToDefaults();
    mConfigureInputWidget->restoreToDefaults();
#if WITH_DBUS
    mConfigureMiscWidget->restoreToDefaults();
#endif
}

#include "moc_configuresettingsdialog.cpp"
