#include "XcbEventInterface.h"

#include "KeyboardEvent.h"
#include "DiscretePoint.h"
#include "MouseEvent.h"
#include "PaintEvent.h"
#include "Keyboard.h"

#include <xcb/xcb.h>

std::unique_ptr<XcbEventInterface> XcbEventInterface::Create()
{
    return std::make_unique<XcbEventInterface>();
}

std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyPress(xcb_key_press_event_t* event, Keyboard* keyboard) const
{
    auto ui_event = KeyboardEvent::Create();
    ui_event->SetAction(KeyboardEvent::Action::Pressed);
    ui_event->SetKeyString(keyboard->GetKeyString(event->detail));
    return ui_event;
}

std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyRelease(xcb_key_press_event_t* event, Keyboard* keyboard) const
{
    auto ui_event = KeyboardEvent::Create();
    ui_event->SetAction(KeyboardEvent::Action::Released);
    ui_event->SetKeyString(keyboard->GetKeyString(event->detail));
    return ui_event;
}

std::unique_ptr<MouseEvent> XcbEventInterface::ConvertButtonPress(xcb_button_press_event_t* event) const
{
    auto ui_event = MouseEvent::Create();
    auto x = static_cast<unsigned>(event->event_x);
    auto y = static_cast<unsigned>(event->event_y);
    ui_event->SetClientLocation(DiscretePoint(x, y));

    auto screen_x = static_cast<unsigned>(event->root_x);
    auto screen_y = static_cast<unsigned>(event->root_y);
    ui_event->SetScreenLocation(DiscretePoint(x, y));
    ui_event->SetAction(MouseEvent::Action::Pressed);
    return ui_event;
}

std::unique_ptr<MouseEvent> XcbEventInterface::ConvertButtonRelease(xcb_button_press_event_t* event) const
{
    auto ui_event = MouseEvent::Create();
    auto x = static_cast<unsigned>(event->event_x);
    auto y = static_cast<unsigned>(event->event_y);
    ui_event->SetClientLocation(DiscretePoint(x, y));

    auto screen_x = static_cast<unsigned>(event->root_x);
    auto screen_y = static_cast<unsigned>(event->root_y);
    ui_event->SetScreenLocation(DiscretePoint(x, y));

    ui_event->SetAction(MouseEvent::Action::Released);
    return ui_event;
}
