// SPDX-FileCopyrightText: 2024 Manuel Alcaraz Zambrano <manuel@alcarazzam.dev>
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QApplication>
#include <QCommandLineParser>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQuickStyle>
#include <QTextStream>
#include <QUrl>

#include "version-chessament.h"
#include <KAboutData>
#include <KIconTheme>
#include <KLocalizedQmlContext>
#include <KLocalizedString>

#include <QCoroQml>

#include "controller.h"

#ifdef Q_OS_WINDOWS
#include <QFont>
#include <Windows.h>
#endif

using namespace Qt::Literals::StringLiterals;

int main(int argc, char *argv[])
{
    KIconTheme::initTheme();
    QIcon::setFallbackThemeName("breeze"_L1);
    QApplication app(argc, argv);

    // Default to org.kde.desktop style unless the user forces another style
    if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE")) {
        QQuickStyle::setStyle(u"org.kde.desktop"_s);
    }

#ifdef Q_OS_WINDOWS
    if (AttachConsole(ATTACH_PARENT_PROCESS)) {
        freopen("CONOUT$", "w", stdout);
        freopen("CONOUT$", "w", stderr);
    }

    QApplication::setStyle(QStringLiteral("breeze"));
    QFont font(u"Segoe UI Emoji"_s);
    font.setPointSize(10);
    font.setHintingPreference(QFont::PreferNoHinting);
    app.setFont(font);
#endif

    KLocalizedString::setApplicationDomain("chessament");

    KAboutData aboutData(
        // The program name used internally.
        u"chessament"_s,
        // A displayable program name string.
        i18nc("@title", "Chessament"),
        // The program version string.
        QStringLiteral(CHESSAMENT_VERSION_STRING),
        // Short description of what the app does.
        i18n("Chess Tournament Manager"),
        // The license this code is released under.
        KAboutLicense::GPL_V3,
        // Copyright Statement.
        i18n("© 2024–2026 Manuel Alcaraz Zambrano"));
    aboutData.addAuthor(i18nc("@info:credit", "Manuel Alcaraz Zambrano"),
                        i18nc("@info:credit", "Author & Maintainer"),
                        u"manuel@alcarazzam.dev"_s,
                        u"https://alcarazzam.dev"_s);
    aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
    aboutData.addComponent(u"bbpPairings"_s,
                           i18n("Swiss-system chess tournament pairing engine."),
                           QString(),
                           u"https://github.com/BieremaBoyzProgramming/bbpPairings"_s,
                           KAboutLicense::LicenseKey::Apache_V2);
    KAboutData::setApplicationData(aboutData);
    QGuiApplication::setWindowIcon(QIcon::fromTheme(u"org.kde.chessament"_s));

    QCommandLineParser parser;
    aboutData.setupCommandLine(&parser);

    QCommandLineOption trfFile("import-trf"_L1, i18nc("Command line option description", "Import Tournament Report File."), "file"_L1);

    parser.addOption(trfFile);
    parser.addPositionalArgument(u"file"_s, i18nc("@info:shell Command line option description", "Event to open."));

    parser.process(app);
    aboutData.processCommandLine(&parser);

    QQmlApplicationEngine engine;

    QCoro::Qml::registerTypes();

    KLocalization::setupLocalizedContext(&engine);
    engine.loadFromModule("org.kde.chessament", u"Main"_s);

    auto *controller = engine.singletonInstance<Controller *>("org.kde.chessament", "Controller");

    const auto positionalArguments = parser.positionalArguments();

    if (parser.isSet(trfFile)) {
        if (!positionalArguments.isEmpty()) {
            QTextStream stream{stderr};
            stream << i18nc("@info:shell", "Error: Can't open an event when importing a TRF") << '\n';
            return 1;
        }
        const auto fileName = parser.value(trfFile);
        controller->importTrf(QUrl::fromLocalFile(fileName));
    }
    if (!positionalArguments.isEmpty()) {
        if (positionalArguments.size() > 1) {
            QTextStream stream{stderr};
            stream << i18nc("@info:shell", "Error: Can't open more that one event") << '\n';
            return 1;
        }
        const auto fileName = parser.positionalArguments().at(0);
        controller->openEvent(QUrl::fromLocalFile(fileName));
    }

    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    return QApplication::exec();
}
