#include "ukuiglobalcursor.h"
#include "ukuidisplay_p.h"

#include "kywc-capture-v1.h"

#include <QDebug>

class UkuiGolbalCursorPrivate
{
public:
    UkuiGolbalCursorPrivate();
    ~UkuiGolbalCursorPrivate();

    wayland::kywc_capture_manager_v1_t m_captureManager = nullptr;
    wayland::kywc_capture_cursor_v1_t m_captureCursor = nullptr;
};

UkuiGolbalCursorPrivate::UkuiGolbalCursorPrivate()
{
    m_captureManager = static_cast<wayland::kywc_capture_manager_v1_t>(ukuiDisplay()->bind(wayland::detail::kywc_capture_cursor_v1_interface.name, m_captureManager, wayland::detail::kywc_capture_cursor_v1_interface.version));
    if (!m_captureManager) {
        qWarning() << "Failed to bind kywc_capture_manager_v1";
        return;
    }

    m_captureCursor = m_captureManager.capture_cursor(ukuiDisplay()->seat(), 0);
}

UkuiGolbalCursorPrivate::~UkuiGolbalCursorPrivate()
{
}

UkuiGolbalCursor *ukuiGolbalCursor()
{
    static UkuiGolbalCursor *instance = nullptr;
    if (!instance) {
        instance = new UkuiGolbalCursor();
    }
    return instance;
}

UkuiGolbalCursor::UkuiGolbalCursor()
    : m_ukuiGolbalCursorPrivate(new UkuiGolbalCursorPrivate())
{
}

UkuiGolbalCursor::~UkuiGolbalCursor()
{
    delete m_ukuiGolbalCursorPrivate;
    m_ukuiGolbalCursorPrivate = nullptr;
}

std::function<void()> &UkuiGolbalCursor::on_enter()
{
    return m_ukuiGolbalCursorPrivate->m_captureCursor.on_enter();
}

std::function<void()> &UkuiGolbalCursor::on_leave()
{
    return m_ukuiGolbalCursorPrivate->m_captureCursor.on_leave();
}

std::function<void(int, int)> &UkuiGolbalCursor::on_position()
{
    return m_ukuiGolbalCursorPrivate->m_captureCursor.on_position();
}

std::function<void(int, int)> &UkuiGolbalCursor::on_hotspot()
{
    return m_ukuiGolbalCursorPrivate->m_captureCursor.on_hotspot();
}