// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "ddesktopportal.h"
#include "filechooser.h"
#include "wayland/portalwaylandcontext.h"
#include "wayland/toplevelmodel.h"
#include "wayland/restoredata.h"

#include <QApplication>
#include <QDBusConnection>
#include <qstringliteral.h>
#include <QLoggingCategory>
#include <QTranslator>
#include <QStandardPaths>
#include <QDBusMetaType>

Q_LOGGING_CATEGORY(XdgDesktopDDE, "xdg-dde")

inline static bool onWayland()
{
    return QGuiApplication::platformName() == "wayland";
}

int main(int argc, char *argv[])
{
    // qputenv("PIPEWIRE_DEBUG", "4");
    // qputenv("QT_LOGGING_RULES", "dde.portal.ScreenCast*.debug=true");
    // qputenv("WAYLAND_DEBUG", "1");
    qDBusRegisterMetaType<RestoreData>();
    qDBusRegisterMetaType<QStringMap>();
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    /* Disable X11 session management
    As XdgDesktopPortal is a QGuiApplication it connects to our X session manager.
    By default Qt apps behave like "applications" and this can end up on our auto-restart list if the user saves their session.
    */
    QCoreApplication::setAttribute(Qt::AA_DisableSessionManager);
#endif

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
    QApplication a(argc, argv);
    a.setQuitOnLastWindowClosed(false);

    QTranslator translator;
    QString languagePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
                                                  QString("xdg-desktop-portal-dde/translations"),
                                                  QStandardPaths::LocateDirectory);
    if (!translator.load(languagePath+"/xdg-desktop-portal-dde_" + QLocale::system().name())) {
        qCWarning(XdgDesktopDDE) << "Failed to load translation file for locale:" << QLocale::system().name();
    }
    a.installTranslator(&translator);

    QDBusConnection sessionBus = QDBusConnection::sessionBus();
    if (sessionBus.registerService(QStringLiteral("org.freedesktop.impl.portal.desktop.dde"))) {
        if (onWayland()) {
            PortalWaylandContext *waylandContext = new PortalWaylandContext(&a);
            new FileChooserPortal(waylandContext);
            if (sessionBus.registerObject(QStringLiteral("/org/freedesktop/portal/desktop"),
                                          waylandContext,
                                          QDBusConnection::ExportAdaptors)) {
                qCDebug(XdgDesktopDDE) << "portal started on wayland";
            }
        } else {
            DDesktopPortal *desktopPortal = new DDesktopPortal(&a);
            if (sessionBus.registerObject(QStringLiteral("/org/freedesktop/portal/desktop"),
                                          desktopPortal,
                                          QDBusConnection::ExportAdaptors)) {
                qCDebug(XdgDesktopDDE) << "portal started on x11";
            }
        }

    } else {
        qCDebug(XdgDesktopDDE) << "Another portal is starting";
        return 1;
    }


    return a.exec();
}
