#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtPlatformHeaders/QXcbWindowFunctions>
#include <QQuickView>
#include <QScreen>
#include "base/launcheritem.h"
#include "base/categoryitem.h"
#include "core/modelmanager.h"
#include "core/taskbarmanager.h"
#include "core/appmanager.h"
#include "core/pluginitem.h"
#include "logger.h"
#include "kgridview/kgridview.h"
#include "utils/getscreenshot.h"
#include "utils/desktopbackground.h"
#include "ukui-log4qt.h"
#include "utils/generalsetting.h"
#include "utils/wirelessstatus.h"
#include "utils/wiredlinkstate.h"
#include "utils/accountlogout.h"
#include "utils/blurhelper.h"
#include "base/trayicon.h"
#include "utils/taskbarmask.h"
#include "base/sidebarstate.h"
#include <QQuickStyle>
#include "utils/showclendar.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

    QApplication app(argc, argv);

#ifndef QT_DEBUG
    initUkuiLog4qt("ukui-tablet-desktop");
#endif
    QQuickView qmlView;
    QQuickStyle::setStyle("org.ukui.style");

    // qml logger
    // usage in qml:
    // QmlLogger.debug(msg);QmlLogger.warning(msg); ...
    common::QmlLogger qmlLogger;

    qmlView.engine()->addImportPath("qrc:/ui/qml");
    qmlView.engine()->rootContext()->setContextProperty("qmlLogger", &qmlLogger);

    ModelManager modelMgr;
    qmlView.engine()->rootContext()->setContextProperty("modelManager", &modelMgr);

    AppManager appMgr;
    qmlView.engine()->rootContext()->setContextProperty("appManager", &appMgr);

    TrayIcon trayIcon;
    qmlView.engine()->rootContext()->setContextProperty("trayIcon", &trayIcon);

    SidebarState sidebarState;
    qmlView.engine()->rootContext()->setContextProperty("sidebarState", &sidebarState);

    ShowClendar showCal;
    qmlView.engine()->rootContext()->setContextProperty("showCal", &showCal);

    bool openGLEnabled = QQuickView::sceneGraphBackend() == "software" ? false : true;
    qmlView.engine()->rootContext()->setContextProperty("openGLEnabled", QVariant::fromValue<bool>(openGLEnabled));

    // qml js:
    // if (type == LauncherItem.AppIcon) {} ...
    qmlRegisterType<LauncherItem>("org.ukui.tablet", 1, 0, "LauncherItem");
    qmlRegisterType<CategoryItem>("org.ukui.tablet", 1, 0, "CategoryItem");
    qmlRegisterType<GetScreenShot>("org.ukui.tablet", 1, 0, "ScreenShot");
    qmlRegisterType<DesktopBackground>("org.ukui.tablet", 1, 0, "Background");
    qmlRegisterType<KQuickGridView>("org.ukui.tablet", 1, 0, "KGridView");
    qmlRegisterType<LauncherItem>("org.ukui.tablet", 1, 0, "LauncherItem");
    qmlRegisterType<CGeneralSetting>("MyGeneralSetting", 1, 0, "CGeneralSetting");
    qmlRegisterType<WirelessStatus>("org.ukui.tablet", 1, 0, "WirelessStatus");
    qmlRegisterType<WiredLinkState>("org.ukui.tablet", 1, 0, "WiredLinkState");
    qmlRegisterType<AccountLogout>("org.ukui.tablet", 1, 0, "AccountLogout");
    qmlRegisterType<BlurHelper>("org.ukui.tablet", 1, 0, "BlurHelper");
    qmlRegisterType<PluginItem>("org.ukui.tablet", 1, 0, "PluginItem");
    qmlRegisterType<TrayIcon>("org.ukui.tablet", 1, 0, "TrayIcon");
    qmlRegisterType<TaskbarMask>("org.ukui.tablet", 1, 0, "TaskbarMask");

    QObject::connect(QApplication::screens().first(), &QScreen::primaryOrientationChanged, [&] {
        qmlView.setGeometry(qmlView.screen()->geometry());
    });

    qmlView.setSource(QUrl("qrc:/ui/qml/main.qml"));
    QXcbWindowFunctions::setWmWindowType(&qmlView, QXcbWindowFunctions::Desktop);
    qmlView.setGeometry(qmlView.screen()->geometry());
    qmlView.show();
    if (qmlView.rootObject())
        qmlView.rootObject()->setState("Show");


    return app.exec();
}
