// Copyright (C) 2023-2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#pragma once

#include "wglobal.h"

#include <QPointer>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformvulkaninstance.h>
#include <qpa/qplatformnativeinterface.h>

QT_BEGIN_NAMESPACE
class QInputDevice;
QT_END_NAMESPACE

WAYLIB_SERVER_BEGIN_NAMESPACE

class WOutput;
class WInputDevice;
class QWlrootsScreen;
class Q_DECL_HIDDEN QWlrootsIntegration : public QPlatformIntegration, public QPlatformNativeInterface
{
public:
    QWlrootsIntegration(const QStringList &parameters, std::function<void()> onInitialized);
    ~QWlrootsIntegration();

    static QWlrootsIntegration *instance();

    QWlrootsScreen *addScreen(WOutput *output);
    void removeScreen(WOutput *output);
    QWlrootsScreen *getScreenFrom(const WOutput *output);
    inline const QList<QWlrootsScreen*> &screens() const {
        return m_screens;
    }

    QPointer<QInputDevice> addInputDevice(WInputDevice *device, const QString &seatName);
    bool removeInputDevice(WInputDevice *device);
    QInputDevice *getInputDeviceFrom(WInputDevice *device);

    void initialize() override;
    void destroy() override;

    bool hasCapability(QPlatformIntegration::Capability cap) const override;
    QPlatformFontDatabase *fontDatabase() const override;

    QPlatformWindow *createPlatformWindow(QWindow *window) const override;
    QPlatformBackingStore *createPlatformBackingStore(QWindow *window) const override;
#ifndef QT_NO_OPENGL
    QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const override;
    QOpenGLContext::OpenGLModuleType openGLModuleType() override;
#endif
    QAbstractEventDispatcher *createEventDispatcher() const override;

    QPlatformNativeInterface *nativeInterface() const override;

    QPlatformPixmap *createPlatformPixmap(QPlatformPixmap::PixelType type) const override;
    QPlatformWindow *createForeignWindow(QWindow *, WId) const override;
    QPlatformSharedGraphicsCache *createPlatformSharedGraphicsCache(const char *cacheId) const override;
    QPaintEngine *createImagePaintEngine(QPaintDevice *paintDevice) const override;

#ifndef QT_NO_CLIPBOARD
    QPlatformClipboard *clipboard() const override;
#endif
#if QT_CONFIG(draganddrop)
    QPlatformDrag *drag() const override;
#endif
    QPlatformInputContext *inputContext() const override;
#if QT_CONFIG(accessibility)
    QPlatformAccessibility *accessibility() const override;
#endif

    QPlatformServices *services() const override;

    QVariant styleHint(StyleHint hint) const override;
    Qt::WindowState defaultWindowState(Qt::WindowFlags flags) const override;

    QStringList themeNames() const override;
    QPlatformTheme *createPlatformTheme(const QString &name) const override;

    using CreatePlatformThemeCallback = std::function<QPlatformTheme *(const QString &name)>;
    static WAYLIB_SERVER_EXPORT void setCreatePlatformThemeCallback(
        CreatePlatformThemeCallback callback);

    QPlatformOffscreenSurface *createPlatformOffscreenSurface(QOffscreenSurface *surface) const override;

#ifndef QT_NO_SESSIONMANAGER
    QPlatformSessionManager *createPlatformSessionManager(const QString &id, const QString &key) const override;
#endif

    void sync() override;
    void setApplicationIcon(const QIcon &icon) const override;
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
    void setApplicationBadge(qint64 number) override;
#endif
    void beep() const override;
    void quit() const override;

#if QT_CONFIG(vulkan)
    QPlatformVulkanInstance *createPlatformVulkanInstance(QVulkanInstance *instance) const override;
#endif

    // native interfaces
    void *nativeResourceForScreen(const QByteArray &resource, QScreen *screen) override;

private:
    friend class QWlrootsScreen;

    static CreatePlatformThemeCallback s_createPlatformThemeCallback;
    mutable std::unique_ptr<QPlatformFontDatabase> m_fontDb;
    std::unique_ptr<QPlatformServices> m_services;
    std::unique_ptr<QPlatformPlaceholderScreen> m_placeholderScreen;

    QList<QWlrootsScreen*> m_screens;
    static QWlrootsIntegration *m_instance;
    std::function<void()> m_onInitialized;
};

WAYLIB_SERVER_END_NAMESPACE
