/***************************************************************************
 * SPDX-FileCopyrightText: 2026 S. MANKOWSKI stephane@mankowski.fr
 * SPDX-License-Identifier: GPL-3.0-or-later
 ***************************************************************************/
#include "skgmcontroler.h"
#include "skgservices.h"
#include "skgtraces.h"

#include <kaboutdata.h>

#include <klocalizedcontext.h>
#include <klocalizedstring.h>

#include <qcommandlineoption.h>
#include <qcommandlineparser.h>
#include <qguiapplication.h>
#include <qicon.h>
#include <qqmlapplicationengine.h>
#include <qqmlcontext.h>

/**
 * To compute the version
 */
#define VER1_(x) #x
/**
 * To compute the version
 */
#define VER_(x) VER1_(x)
/**
 * To compute the version
 */
#define VER VER_(SKGVERSION)

#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
int main(int argc, char *argv[])
{
    SKGTRACEINFUNC(1)
    QGuiApplication app(argc, argv);
    KLocalizedString::setApplicationDomain("skrooge");
    const QIcon appIcon = SKGServices::fromTheme(QStringLiteral("skrooge"));
    if (!appIcon.isNull()) {
        app.setWindowIcon(appIcon);
    }

    KAboutData about(QStringLiteral("skrooge companion"),
                     i18nc("The name of the application", "Skrooge Companion"),
                     QStringLiteral(VER),
                     i18nc("The description of the application", "The Skrooge companion application for mobile devices"),
                     KAboutLicense::GPL_V3,
                     i18nc("Fullname", "(c) 2007-%1 Stephane MANKOWSKI & Guillaume DE BURE", QDate::currentDate().toString(QStringLiteral("yyyy"))),
                     QString(),
                     QStringLiteral("https://skrooge.org"));

    about.addAuthor(i18nc("Fullname", "Stephane MANKOWSKI"),
                    i18nc("A job description", "Architect & Developer"),
                    QStringLiteral("stephane@mankowski.fr"),
                    QString(),
                    QStringLiteral("miraks"));
    about.setOtherText(i18nc("The description of the application",
                             "The application name is inspired by Charles Dicken's tale <i>A Christmas Carol</i>, where the main character, Ebenezer Scrooge, "
                             "a grumpy old narrow man, gets visited by three ghosts who change the way he sees the world, in a good way."));
    about.setOrganizationDomain("kde.org");
    about.setDesktopFileName(QStringLiteral("org.kde.skroogecompanion"));
    KAboutData::setApplicationData(about);

    QCommandLineParser parser;
    parser.setApplicationDescription(i18nc("Application description", "Skrooge mobile accounts viewer"));
    parser.addHelpOption();
    parser.addPositionalArgument(QStringLiteral("file"), i18nc("CLI argument description", "Skrooge file to open"));
    QCommandLineOption passwordOption(QStringList() << QStringLiteral("p") << QStringLiteral("password"),
                                      i18nc("CLI option description", "Password for encrypted Skrooge file"),
                                      QStringLiteral("password"));
    parser.addOption(passwordOption);
    parser.process(app);

    SKGMControler controller;
    if (!parser.positionalArguments().isEmpty()) {
        controller.setFilePath(parser.positionalArguments().constFirst());
    }
    if (parser.isSet(passwordOption)) {
        controller.setPassword(parser.value(passwordOption));
    }

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
    engine.rootContext()->setContextProperty(QStringLiteral("mobileController"), &controller);
    engine.loadFromModule(QStringLiteral("org.kde.skroogecompanion"), QStringLiteral("Main"));
    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    if (!controller.filePath().isEmpty()) {
        if (controller.openFile()) {
            controller.ensureDataForPage(0);
        }
    }

    return app.exec();
}
