#include <QtWaylandCompositor/private/qwayland-server-qt-windowmanager.h>

QT_BEGIN_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")

namespace QtWaylandServer {
    qt_windowmanager::qt_windowmanager(struct ::wl_client *client, int id, int version)
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
        init(client, id, version);
    }

    qt_windowmanager::qt_windowmanager(struct ::wl_display *display, int version)
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
        init(display, version);
    }

    qt_windowmanager::qt_windowmanager(struct ::wl_resource *resource)
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
        init(resource);
    }

    qt_windowmanager::qt_windowmanager()
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
    }

    qt_windowmanager::~qt_windowmanager()
    {
        for (auto resource : qAsConst(m_resource_map))
            wl_resource_set_implementation(resource->handle, nullptr, nullptr, nullptr);

        if (m_global) {
            wl_global_destroy(m_global);
            wl_list_remove(&m_displayDestroyedListener.link);
        }
    }

    void qt_windowmanager::init(struct ::wl_client *client, int id, int version)
    {
        m_resource = bind(client, id, version);
    }

    void qt_windowmanager::init(struct ::wl_resource *resource)
    {
        m_resource = bind(resource);
    }

    qt_windowmanager::Resource *qt_windowmanager::add(struct ::wl_client *client, int version)
    {
        Resource *resource = bind(client, 0, version);
        m_resource_map.insert(client, resource);
        return resource;
    }

    qt_windowmanager::Resource *qt_windowmanager::add(struct ::wl_client *client, int id, int version)
    {
        Resource *resource = bind(client, id, version);
        m_resource_map.insert(client, resource);
        return resource;
    }

    void qt_windowmanager::init(struct ::wl_display *display, int version)
    {
        m_global = wl_global_create(display, &::qt_windowmanager_interface, version, this, bind_func);
        m_globalVersion = version;
        m_displayDestroyedListener.notify = qt_windowmanager::display_destroy_func;
        m_displayDestroyedListener.parent = this;
        wl_display_add_destroy_listener(display, &m_displayDestroyedListener);
    }

    const struct wl_interface *qt_windowmanager::interface()
    {
        return &::qt_windowmanager_interface;
    }

    qt_windowmanager::Resource *qt_windowmanager::windowmanager_allocate()
    {
        return new Resource;
    }

    void qt_windowmanager::windowmanager_bind_resource(Resource *)
    {
    }

    void qt_windowmanager::windowmanager_destroy_resource(Resource *)
    {
    }

    void qt_windowmanager::bind_func(struct ::wl_client *client, void *data, uint32_t version, uint32_t id)
    {
        qt_windowmanager *that = static_cast<qt_windowmanager *>(data);
        that->add(client, id, qMin(that->m_globalVersion, version));
    }

    void qt_windowmanager::display_destroy_func(struct ::wl_listener *listener, void *data)
    {
        Q_UNUSED(data);
        qt_windowmanager *that = static_cast<qt_windowmanager::DisplayDestroyedListener *>(listener)->parent;
        that->m_global = nullptr;
    }

    void qt_windowmanager::destroy_func(struct ::wl_resource *client_resource)
    {
        Resource *resource = Resource::fromResource(client_resource);
        Q_ASSERT(resource);
        qt_windowmanager *that = resource->windowmanager_object;
        that->m_resource_map.remove(resource->client(), resource);
        that->windowmanager_destroy_resource(resource);
        delete resource;
    }

    qt_windowmanager::Resource *qt_windowmanager::bind(struct ::wl_client *client, uint32_t id, int version)
    {
        Q_ASSERT_X(!wl_client_get_object(client, id), "QWaylandObject bind", QStringLiteral("binding to object %1 more than once").arg(id).toLocal8Bit().constData());
        struct ::wl_resource *handle = wl_resource_create(client, &::qt_windowmanager_interface, version, id);
        return bind(handle);
    }

    qt_windowmanager::Resource *qt_windowmanager::bind(struct ::wl_resource *handle)
    {
        Resource *resource = windowmanager_allocate();
        resource->windowmanager_object = this;

        wl_resource_set_implementation(handle, &m_qt_windowmanager_interface, resource, destroy_func);
        resource->handle = handle;
        windowmanager_bind_resource(resource);
        return resource;
    }
    qt_windowmanager::Resource *qt_windowmanager::Resource::fromResource(struct ::wl_resource *resource)
    {
        if (Q_UNLIKELY(!resource))
            return nullptr;
        if (wl_resource_instance_of(resource, &::qt_windowmanager_interface, &m_qt_windowmanager_interface))
            return static_cast<Resource *>(wl_resource_get_user_data(resource));
        return nullptr;
    }

    const struct ::qt_windowmanager_interface qt_windowmanager::m_qt_windowmanager_interface = {
        qt_windowmanager::handle_open_url
    };

    void qt_windowmanager::windowmanager_open_url(Resource *, uint32_t , const QString &)
    {
    }


    void qt_windowmanager::handle_open_url(
        ::wl_client *client,
        struct wl_resource *resource,
        uint32_t remaining,
        const char *url)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<qt_windowmanager *>(r->windowmanager_object)->windowmanager_open_url(
            r,
            remaining,
            QString::fromUtf8(url));
    }

    void qt_windowmanager::send_hints(int32_t show_is_fullscreen)
    {
        send_hints(
            m_resource->handle,
            show_is_fullscreen);
    }

    void qt_windowmanager::send_hints(struct ::wl_resource *resource, int32_t show_is_fullscreen)
    {
        qt_windowmanager_send_hints(
            resource,
            show_is_fullscreen);
    }


    void qt_windowmanager::send_quit()
    {
        send_quit(
            m_resource->handle);
    }

    void qt_windowmanager::send_quit(struct ::wl_resource *resource)
    {
        qt_windowmanager_send_quit(
            resource);
    }

}

QT_WARNING_POP
QT_END_NAMESPACE
