#include "WaylandInterface.h"

#include "FileLogger.h"
#include "WaylandSurface.h"
#include "WaylandBuffer.h"
#include "WaylandSeatInterface.h"
#include "WaylandEglInterface.h"

#include <cstring>
#include <sstream>

void WaylandInterface::registryHandleGlobalEvent(void *data, struct wl_registry *registry,
        uint32_t name, const char *interface, uint32_t version)
{
    auto thisClass = static_cast<WaylandInterface*>(data);

    std::stringstream sstrm;
    sstrm << "interface: " << interface << " version " << version << " name " << name;
    MLOG_INFO(sstrm.str());

    if (strcmp(interface, wl_compositor_interface.name) == 0)
    {
        thisClass->setCompositor(static_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 4)));
    }
    else if (strcmp(interface, wl_shm_interface.name) == 0)
    {
        thisClass->setSharedMemory(static_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1)));
    }
    else if (strcmp(interface, xdg_wm_base_interface.name) == 0)
    {
        thisClass->setXdgBase(static_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1)));
    }
    else if (strcmp(interface, wl_seat_interface.name) == 0)
    {
        thisClass->setSeat(static_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 5)));
    }
}

void WaylandInterface::registryHandleGlobalRemoveEvent(void *data, struct wl_registry *registry, uint32_t name)
{

}


WaylandInterface::WaylandInterface(DesktopManager* desktopManager)
    :       mDesktopManager(desktopManager),
            mBuffer(std::make_shared<WaylandBuffer>())
{
    mRegistryListener.global = registryHandleGlobalEvent;
    mRegistryListener.global_remove = registryHandleGlobalRemoveEvent;
}

WaylandInterface::~WaylandInterface()
{

}

void WaylandInterface::initialize()
{
    mDisplay = wl_display_connect(nullptr);

    if (!mDisplay)
    {
        MLOG_ERROR("Display connect error");
        return;
    }

    auto registry = wl_display_get_registry(mDisplay);
    if (!registry)
    {
        MLOG_ERROR("Failed to get registry");
        return;
    }

    wl_registry_add_listener(registry, &mRegistryListener, this);
    wl_display_roundtrip(mDisplay);

    if (mUseHardwareRendering)
    {
        mEglInterface = std::make_unique<WaylandEglInterface>();
        mEglInterface->initialize(mDisplay);
    }
}

void WaylandInterface::setSeat(wl_seat* seat)
{
    mSeatInterface = std::make_unique<WaylandSeatInterface>(seat);
}

void WaylandInterface::setXdgBase(xdg_wm_base* xdg_base)
{
    mXdgBase = xdg_base;
    auto xdg_ping_handler = [](void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial)
    {
        auto thisClass = static_cast<WaylandInterface*>(data);
        thisClass->doXdgPong(serial);
    };
    mXdgBaseListener.ping = xdg_ping_handler;
    xdg_wm_base_add_listener(mXdgBase, &mXdgBaseListener, this);
}

void WaylandInterface::setCompositor(wl_compositor* compositor)
{
    mCompositor = compositor;
}

void WaylandInterface::setSharedMemory(wl_shm* shared_memory)
{
    mBuffer->setSharedMemory(shared_memory);
}

void WaylandInterface::doXdgPong(uint32_t serial)
{
    xdg_wm_base_pong(mXdgBase, serial);
}

void WaylandInterface::addWindow(mt::Window* window)
{
    auto surface = std::make_unique<WaylandSurface>(window);
    mSurfaces.push_back(std::move(surface));
}

void WaylandInterface::showWindow(mt::Window* window)
{
    if (mSurfaces.empty())
    {
        return;
    }

    mSurfaces[0]->initialize(mCompositor, mXdgBase, mBuffer, mEglInterface.get());
}

void WaylandInterface::shutDown()
{
    if (mDisplay)
    {
        wl_display_disconnect(mDisplay);
    }
}

void WaylandInterface::loop()
{
    while (wl_display_dispatch(mDisplay) != -1)
    {
        /* This space deliberately left blank */
    }
}
