#include "button.h"
#include "debuggers.h"

namespace pabloader {
Button::Button(Debuggers* game, int32_t x, int32_t y, int32_t w, int32_t h, std::string text, GameState state)
    : game(game)
    , pos(x, y)
    , size(w, h)
    , text(text)
    , state(state)
{
}

bool Button::Update()
{
    auto x = game->GetMouseX();
    auto y = game->GetMouseY();
    auto newHover = x >= pos.x && x <= pos.x + size.x && y >= pos.y && y <= pos.y + size.y;
    if (!hover && newHover) {
        olc::SOUND::PlaySample(game->hoverSample);
    }
    hover = newHover;
    if (hover && game->GetMouse(0).bPressed) {
        olc::SOUND::PlaySample(game->clickSample);
        game->SwitchState(state);
        return true;
    }
    return false;
}

void Button::Draw()
{
    auto sw = 8 * text.size();
    auto sh = 8;
    game->DrawRect(pos.x, pos.y, size.x, size.y, hover ? olc::WHITE : olc::GREY);
    game->DrawString(pos.x + (size.x - sw) / 2, pos.y + (size.y - sh) / 2, text);
}
}