#include <Color/Color.hpp>
#include <Log/Log.hpp>
#include <Sound/Sound.hpp>
#include <Widget/Label/Label.hpp>
#include <Widget/Layout/MenuButton/MenuButton.hpp>

MenuButton::MenuButton(Widget *widget, Callback callback) :
    Layout(widget),
    onActivated(callback)
{}

MenuButton::MenuButton(std::string label, Callback callback) :
    Layout(new Label(label)),
    onActivated(callback)
{}

void MenuButton::layout()
{
    ChildNode &childNode = m_children.back();
    childNode.pos = glm::ivec2(margin, margin);
    childNode.widget->onResize(m_size - glm::ivec2(2 * margin));
}

glm::ivec2 MenuButton::getMinSize()
{
    return m_children.back().widget->getMinSize() + glm::ivec2(2 * margin);
}

void MenuButton::draw(GraphicsContext &context)
{
    context.color = backgroundColor.sample(isMouseInside() ? Color::selection : glm::vec4(0.0f));
    context.drawRect(0, 0, m_size.x, m_size.y);
    Layout::draw(context);
}

void MenuButton::onMouseDown(Uint8 button)
{
    // NOP
}

void MenuButton::onMouseUp(Uint8 button)
{
    if (isMouseInside() && button == SDL_BUTTON_LEFT)
    {
        Sound::select.play();
        onActivated();
        closePopup();
    }
}