#include "pushbutton.h"

PushButton::PushButton(const std::string& text, const std::shared_ptr<kku::CoreFactory>& factory, unsigned int font_size) :
    Button(text, factory, font_size),
    _pressed(false)
{
    _color_idle = kku::Color{230, 230, 230, 255};
    _color_pressed = kku::Color{200, 200, 200, 255};
    _button_content = _core_factory->getRectangle();
    _button_content->setColor(_color_idle);
}

void PushButton::input(const kku::SystemEvent& event)
{
    switch (event.type)
    {
    default:
        break;

    case kku::SystemEvent::Type::MousePress:
    {
        const auto position = std::get<kku::SystemEvent::Mouse>(event.data).position;
        if (isUnderMouse(position))
        {
            _pressed = true;
            _button_content->setColor(_color_pressed);
        }
        break;
    }

    case kku::SystemEvent::Type::MouseRelease:
    {
        if (_pressed)
        {
            const auto position = std::get<kku::SystemEvent::Mouse>(event.data).position;
            _button_content->setColor(_color_idle);
            _pressed = false;
            if (isUnderMouse(position))
                _on_click_callback();
        }
        break;
    }
    }
}

void PushButton::setCallback(kku::lambda callback)
{
    _on_click_callback = callback;
}

void PushButton::setColors(kku::Color&& idle_color, kku::Color&& pressed_color)
{
    _color_idle = idle_color;
    _color_pressed = pressed_color;

    _button_content->setColor(_pressed ? pressed_color : idle_color);
}
