#include "WaylandSeatInterface.h"

#include "FileLogger.h"

void WaylandSeatInterface::seatCapabilitiesEvent(void *data, struct wl_seat *wl_seat, uint32_t capabilities)
{
    auto thisClass = static_cast<WaylandSeatInterface*>(data);
    thisClass->onSeatCapabilitiesEvent(capabilities);
}

void WaylandSeatInterface::onSeatCapabilitiesEvent(uint32_t capabilities)
{
    const bool have_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
    if (have_pointer && mPointerInterface == nullptr)
    {
        auto pointer = wl_seat_get_pointer(mSeat);
        mPointerInterface = std::make_unique<WaylandPointerInterface>(pointer);
    }
    else if (!have_pointer && mPointerInterface!= nullptr)
    {
        mPointerInterface.reset();
    }

    const bool have_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
    if (have_keyboard && mKeyboard == nullptr)
    {
        auto keyboard = wl_seat_get_keyboard(mSeat);
        mKeyboard = std::make_unique<WaylandKeyboard>(keyboard);
    }
    else if (!have_keyboard && mKeyboard!= nullptr)
    {
        mKeyboard.reset();
    }
}

WaylandSeatInterface::WaylandSeatInterface(wl_seat* seat)
    : mSeat(seat)
{
    mSeatListener.capabilities = seatCapabilitiesEvent;
    mSeatListener.name = seatNameEvent;

    wl_seat_add_listener(mSeat, &mSeatListener, this);
}

void WaylandSeatInterface::seatNameEvent(void *data, struct wl_seat *wl_seat, const char *name)
{
    MLOG_INFO("seat name: " << name);
}
