#include <Sound/Sound.hpp>
#include <Widget/Switch/Switch.hpp>

Switch::Switch(bool &state, std::function<void(bool)> onUpdated) : state(state), onUpdated(onUpdated) {}

glm::ivec2 Switch::getMinSize()
{
    return glm::ivec2(width, height);
}

void Switch::draw(GraphicsContext &context)
{
    context.color = glm::vec4(1.0f);
    context.drawMaterial(Material::switchUI, isFocused() ? width : 0, state ? 8 : 0, width, height, 0, 0, width, height);
}

void Switch::onMouseUp(Uint8 button)
{
    Widget::onMouseUp(button);

    if (button == SDL_BUTTON_LEFT)
    {
        if (isMouseInside())
        {
            Sound::switchFlip.play();
            state = !state;
            onUpdated(state);
        }
    }
}

void Switch::onKeyDown(SDL_Keycode keycode)
{
    if (keycode == SDLK_SPACE || keycode == SDLK_RETURN)
    {
        state = !state;
        onUpdated(state);
    }
}

void Switch::getInternalTabOrder(std::vector<Widget*> &tabOrder)
{
    tabOrder.push_back(this);
}