// SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: LGPL-2.0-or-later

#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>

#include <KAboutData>
#include <KLocalizedString>

#include "settings.h"
#include "setupstate.h"
#include "version.h"
#include "wizard.h"

std::unique_ptr<QCommandLineParser> createParser()
{
    auto parser = std::make_unique<QCommandLineParser>();
    parser->addOption(QCommandLineOption(QStringLiteral("test-wizard"), i18n("Opens the initial start wizard without modifying configuration")));
    parser->addOption(QCommandLineOption(QStringLiteral("test-apply-defaults"),
                                         i18n("Writes recommended setup defaults and exits without applying them to the running session")));
    parser->addOption(QCommandLineOption(QStringLiteral("test-apply-profile"),
                                         i18n("Writes setup defaults for a profile and exits without applying them to the running session"),
                                         i18n("profile")));
    return parser;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // start wizard
    KLocalizedString::setApplicationDomain("plasma_org.kde.plasma.mobileinitialstart");
    KAboutData aboutData(QStringLiteral("plasma-mobile-initial-start"),
                         QStringLiteral("SHIFT Initial Setup"),
                         QStringLiteral(PLASMA_MOBILE_VERSION_STRING),
                         QStringLiteral(""),
                         KAboutLicense::GPL,
                         i18n("© 2023-2026 Plasma Mobile contributors and Marco Allegretti"));
    aboutData.addAuthor(i18n("Devin Lin"), QString(), QStringLiteral("devin@kde.org"));
    aboutData.addAuthor(i18n("Marco Allegretti"));
    aboutData.setBugAddress("https://invent.kde.org/marcoa/shift-shell/-/issues");
    KAboutData::setApplicationData(aboutData);

    // parse command
    auto parser = createParser();
    aboutData.setupCommandLine(parser.get());
    parser->process(app);
    aboutData.processCommandLine(parser.get());

    const bool testWizard = parser->isSet(QStringLiteral("test-wizard"));
    const bool testApplyDefaults = parser->isSet(QStringLiteral("test-apply-defaults"));
    const QString testApplyProfile = parser->value(QStringLiteral("test-apply-profile"));
    if (testApplyDefaults || !testApplyProfile.isEmpty()) {
        if (!testApplyProfile.isEmpty()) {
            const QStringList supportedProfiles{QStringLiteral("mobile"), QStringLiteral("desktop"), QStringLiteral("gaming"), QStringLiteral("hybrid")};
            if (!supportedProfiles.contains(testApplyProfile)) {
                qCritical() << "Unsupported setup profile" << testApplyProfile;
                return 2;
            }
            SetupState::self()->applyExperienceDefaults(testApplyProfile);
        }

        SetupState::self()->apply(false);
        return 0;
    }

    if (!testWizard) {
        // if the wizard has already been run with a setup profile
        if (!Settings::self()->shouldStartWizard()) {
            qDebug() << "Wizard will not be started since it has already been run.";
            return 0;
        }
    }

    app.setWindowIcon(QIcon::fromTheme(QStringLiteral("start-here-shift")));

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextObject(new KLocalizedContext{&engine});

    Wizard *wizard = new Wizard{nullptr, &engine};
    wizard->setTestingMode(testWizard);
    wizard->load();

    qmlRegisterSingletonType<Wizard>("initialstart", 1, 0, "Wizard", [wizard](QQmlEngine *, QJSEngine *) -> QObject * {
        return wizard;
    });

    engine.load(QUrl(QStringLiteral("qrc:/org/kde/plasma/mobileinitialstart/initialstart/qml/Main.qml")));

    return app.exec();
}
