/*
   SPDX-FileCopyrightText: 2021-2023 Laurent Montel <montel@kde.org>
   SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>

   SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "knoteconfigmodule.h"

#include "config/noteactionconfig.h"
#include "config/notenetworkconfig.h"
#include "knotesglobalconfig.h"
#include "notesharedglobalconfig.h"
#include "print/knoteprintselectthemecombobox.h"
#include <config-knotes.h>

#include "configdialog/knotecollectionconfigwidget.h"
#include "configdialog/knotedisplayconfigwidget.h"
#include "configdialog/knoteeditorconfigwidget.h"
#include <KAuthorized>
#include <KLocalizedString>

#include <knewstuff_version.h>
#if KNEWSTUFF_VERSION < QT_VERSION_CHECK(5, 240, 0)
#include <KNS3/QtQuickDialogWrapper>
#else
#include <KNSWidgets/QtQuickDialogWrapper>
#endif
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWhatsThis>

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
KNoteDisplayConfig::KNoteDisplayConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
#else
KNoteDisplayConfig::KNoteDisplayConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : KCModule(parent, data, args)
#endif
{
#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto lay = new QVBoxLayout(this);
    QWidget *w = new KNoteDisplayConfigWidget(true, this);
#else
    auto lay = new QVBoxLayout(widget());
    QWidget *w = new KNoteDisplayConfigWidget(true, widget());
#endif
    lay->addWidget(w);
    lay->addStretch();
    addConfig(KNotesGlobalConfig::self(), w);
    load();
}

void KNoteDisplayConfig::load()
{
    KCModule::load();
}

void KNoteDisplayConfig::save()
{
    KCModule::save();
}

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
KNoteEditorConfig::KNoteEditorConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
#else
KNoteEditorConfig::KNoteEditorConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : KCModule(parent, data, args)
#endif
{
#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto lay = new QVBoxLayout(this);
    QWidget *w = new KNoteEditorConfigWidget(this);
#else
    auto lay = new QVBoxLayout(widget());
    QWidget *w = new KNoteEditorConfigWidget(widget());
#endif
    lay->addWidget(w);
    lay->addStretch();
    addConfig(KNotesGlobalConfig::self(), w);
    load();
}

void KNoteEditorConfig::save()
{
    KCModule::save();
}

void KNoteEditorConfig::load()
{
    KCModule::load();
}

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
KNoteMiscConfig::KNoteMiscConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
#else
KNoteMiscConfig::KNoteMiscConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : KCModule(parent, data, args)
#endif
{
#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto lay = new QVBoxLayout(this);
#else
    auto lay = new QVBoxLayout(widget());
#endif
    lay->setContentsMargins(0, 0, 0, 0);

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto kcfg_SystemTrayShowNotes = new QCheckBox(i18n("Show number of notes in tray icon"), this);
#else
    auto kcfg_SystemTrayShowNotes = new QCheckBox(i18n("Show number of notes in tray icon"), widget());
#endif

    kcfg_SystemTrayShowNotes->setObjectName(QStringLiteral("kcfg_SystemTrayShowNotes"));
    lay->addWidget(kcfg_SystemTrayShowNotes);

    auto hbox = new QHBoxLayout;
    lay->addLayout(hbox);
#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto label_DefaultTitle = new QLabel(i18n("Default Title:"), this);
#else
    auto label_DefaultTitle = new QLabel(i18n("Default Title:"), widget());
#endif
    hbox->addWidget(label_DefaultTitle);

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    mDefaultTitle = new QLineEdit(this);
#else
    mDefaultTitle = new QLineEdit(widget());
#endif
    label_DefaultTitle->setBuddy(mDefaultTitle);
    hbox->addWidget(mDefaultTitle);

    auto howItWorks = new QLabel(i18n("<a href=\"whatsthis\">How does this work?</a>"));
    connect(howItWorks, &QLabel::linkActivated, this, &KNoteMiscConfig::slotHelpLinkClicked);
    lay->addWidget(howItWorks);

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    addConfig(KNotesGlobalConfig::self(), this);
#else
    addConfig(KNotesGlobalConfig::self(), widget());
#endif
    howItWorks->setContextMenuPolicy(Qt::NoContextMenu);
    lay->addStretch();
    load();
    connect(mDefaultTitle, &QLineEdit::textChanged, this, &KNoteMiscConfig::markAsChanged);
}

void KNoteMiscConfig::load()
{
    KCModule::load();
    mDefaultTitle->setText(NoteShared::NoteSharedGlobalConfig::self()->defaultTitle());
}

void KNoteMiscConfig::save()
{
    KCModule::save();
    NoteShared::NoteSharedGlobalConfig::self()->setDefaultTitle(mDefaultTitle->text());
    NoteShared::NoteSharedGlobalConfig::self()->save();
}

void KNoteMiscConfig::defaults()
{
    KCModule::defaults();
    const bool bUseDefaults = NoteShared::NoteSharedGlobalConfig::self()->useDefaults(true);
    mDefaultTitle->setText(NoteShared::NoteSharedGlobalConfig::self()->defaultTitle());
    NoteShared::NoteSharedGlobalConfig::self()->useDefaults(bUseDefaults);
}

void KNoteMiscConfig::slotHelpLinkClicked(const QString &)
{
    const QString help = i18n(
        "<qt>"
        "<p>You can customize title note. "
        "You can use:</p>"
        "<ul>"
        "<li>%d current date (short format)</li>"
        "<li>%l current date (long format)</li>"
        "<li>%t current time</li>"
        "</ul>"
        "</qt>");

    QWhatsThis::showText(QCursor::pos(), help);
}

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
KNotePrintConfig::KNotePrintConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
#else
KNotePrintConfig::KNotePrintConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : KCModule(parent, data, args)
#endif
{
#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto lay = new QVBoxLayout(this);
    auto w = new QWidget(this);
#else
    auto lay = new QVBoxLayout(widget());
    auto w = new QWidget(widget());
#endif
    lay->addWidget(w);
    auto layout = new QGridLayout(w);
    layout->setContentsMargins(0, 0, 0, 0);

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto label_PrintAction = new QLabel(i18n("Theme:"), this);
#else
    auto label_PrintAction = new QLabel(i18n("Theme:"), widget());
#endif
    layout->addWidget(label_PrintAction, 0, 0);

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    mSelectTheme = new KNotePrintSelectThemeComboBox(this);
#else
    mSelectTheme = new KNotePrintSelectThemeComboBox(widget());
#endif
    connect(mSelectTheme, &QComboBox::activated, this, &KNotePrintConfig::slotThemeChanged);
    label_PrintAction->setBuddy(mSelectTheme);
    layout->addWidget(mSelectTheme, 0, 1);
    if (KAuthorized::authorize(QStringLiteral("ghns"))) {
        auto getNewTheme = new QToolButton;
        getNewTheme->setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")));
        getNewTheme->setToolTip(i18n("Download new printing themes"));
        connect(getNewTheme, &QToolButton::clicked, this, &KNotePrintConfig::slotDownloadNewThemes);
        layout->addWidget(getNewTheme, 0, 2);
    }
    lay->addStretch();
    load();
}

void KNotePrintConfig::slotDownloadNewThemes()
{
#if KNEWSTUFF_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto newStuffDialog = new KNS3::QtQuickDialogWrapper(QStringLiteral("kwinswitcher.knsrc"));
    connect(newStuffDialog, &KNS3::QtQuickDialogWrapper::closed, this, [newStuffDialog, this]() {
#else
    auto newStuffDialog = new KNSWidgets::QtQuickDialogWrapper(QStringLiteral("kwinswitcher.knsrc"));
    connect(newStuffDialog, &KNSWidgets::QtQuickDialogWrapper::closed, this, [newStuffDialog, this]() {
#endif
        if (!newStuffDialog->changedEntries().isEmpty()) {
            mSelectTheme->loadThemes();
        }
        newStuffDialog->deleteLater();
    });
    newStuffDialog->open();
}

void KNotePrintConfig::slotThemeChanged()
{
#if KNEWSTUFF_VERSION < QT_VERSION_CHECK(5, 240, 0)
    Q_EMIT changed(true);
#else
    markAsChanged();
#endif
}

void KNotePrintConfig::save()
{
    KNotesGlobalConfig::self()->setTheme(mSelectTheme->selectedTheme());
}

void KNotePrintConfig::load()
{
    mSelectTheme->loadThemes();
}

void KNotePrintConfig::defaults()
{
    mSelectTheme->selectDefaultTheme();
#if KNEWSTUFF_VERSION < QT_VERSION_CHECK(5, 240, 0)
    Q_EMIT changed(true);
#else
    markAsChanged();
#endif
}

#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
KNoteCollectionConfig::KNoteCollectionConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
#else
KNoteCollectionConfig::KNoteCollectionConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : KCModule(parent, data, args)
#endif
{
#if KCMUTILS_VERSION < QT_VERSION_CHECK(5, 240, 0)
    auto lay = new QHBoxLayout(this);
    mCollectionConfigWidget = new KNoteCollectionConfigWidget(this);
#else
    auto lay = new QHBoxLayout(widget());
    mCollectionConfigWidget = new KNoteCollectionConfigWidget(widget());

#endif
    lay->addWidget(mCollectionConfigWidget);
    connect(mCollectionConfigWidget, &KNoteCollectionConfigWidget::emitChanged, this, &KNoteCollectionConfig::markAsChanged);
    load();
}

void KNoteCollectionConfig::save()
{
    mCollectionConfigWidget->save();
}

void KNoteCollectionConfig::load()
{
    // Nothing
}
