/*
    SPDX-License-Identifier: GPL-2.0-or-later
    SPDX-FileCopyrightText: %{CURRENT_YEAR} %{AUTHOR} <%{EMAIL}>
*/

#include <QtGlobal>
#ifdef Q_OS_ANDROID
#include <QGuiApplication>
#else
#include <QApplication>
#endif

#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QUrl>

#include "app.h"
#include "version-{{ application_name_lower }}.h"
#include <KAboutData>
#include <KLocalizedContext>
#include <KLocalizedString>

#ifndef BUILD_WITH_MOCKS
#include "entities_registration.h"
#include "controller_registration.h"
#include "persistence_registration.h"
#endif
#include "{{ application_name_lower }}config.h"

using namespace Qt::Literals::StringLiterals;

#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
int main(int argc, char *argv[])
{
#ifdef Q_OS_ANDROID
    QGuiApplication app(argc, argv);
    QQuickStyle::setStyle(QStringLiteral("org.kde.breeze"));
#else
    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);
    }
#endif

#ifndef BUILD_WITH_MOCKS
    // These registrations are necessary to make the app work with the backend generated by the code generator of Qleany

    new {{ application_cpp_domain_name }}::Entities::EntitiesRegistration(&app);
    auto *persistenceRegistration = new {{ application_cpp_domain_name }}::Persistence::PersistenceRegistration(&app);
    new {{ application_cpp_domain_name }}::Controller::ControllerRegistration(&app, persistenceRegistration->repositoryProvider());
#endif

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

    QApplication::setStyle(QStringLiteral("breeze"));
    auto font = app.font();
    font.setPointSize(10);
    app.setFont(font);
#endif

    KLocalizedString::setApplicationDomain("{{ application_name_lower }}");
    QCoreApplication::setOrganizationName(u"KDE"_s);

    KAboutData aboutData(
        // The program name used internally.
        u"{{ application_name_pascal }}"_s,
        // A displayable program name string.
        i18nc("@title", "{{ application_name_pascal }}"),
        // The program version string.
        QStringLiteral({{ application_name_upper }}_VERSION_STRING),
        // Short description of what the app does.
        i18n("Application Description"),
        // The license this code is released under.
        KAboutLicense::GPL,
        // Copyright Statement.
        i18n("(c) %{CURRENT_YEAR}"));
    aboutData.addAuthor(i18nc("@info:credit", "%{AUTHOR}"),
                        i18nc("@info:credit", "Maintainer"),
                        u"%{EMAIL}"_s,
                        u"https://yourwebsite.com"_s);
    aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
    KAboutData::setApplicationData(aboutData);
    QGuiApplication::setWindowIcon(QIcon::fromTheme(u"org.kde.{{ application_name_lower }}"_s));

    QQmlApplicationEngine engine;

    auto config = {{ application_name_pascal }}Config::self();

    qmlRegisterSingletonInstance("org.kde.{{ application_name_lower }}.private", 1, 0, "Config", config);

    engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
#ifdef BUILD_WITH_MOCKS
    engine.addImportPath(QApplication::applicationDirPath() + QStringLiteral("/mock_imports"));
#endif
    engine.loadFromModule("org.kde.{{ application_name_lower }}", u"Main.qml");

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

    return app.exec();
}
