#include <Widget/ProgressIcon/ProgressIcon.hpp>
#include <ToolTip/ToolTip.hpp>

ProgressIcon::ProgressIcon(
    Material &icon,
    std::string label,
    bool obtained,
    ClickCallback onClicked
) : icon(icon),
    label(label),
    obtained(obtained),
    onClicked(onClicked)
{}

glm::ivec2 ProgressIcon::getMinSize()
{
    return glm::ivec2(10, 10);
}

void ProgressIcon::draw(GraphicsContext &context)
{
    if (obtained)
    {
        context.color = Color::progressDone;
    }
    else
    {
        float time = 0.5f * (1.0f + std::sin((float) SDL_GetTicks() / 200.0));
        context.color = Color::progressTodo * time + (1.0f - time) * Color::progressDone;
    }

    context.drawRect(0, 0, m_size.x, m_size.y);

    context.color = Color::progressTodo;
    context.drawRect(1, 1, m_size.x - 2, m_size.y - 2);

    context.color = glm::vec4(1.0f);
    context.drawMaterial(icon, 1, 1);

    if (isMouseInside())
    {
        ToolTip &tip = context.createToolTip();
        tip.addLine(glm::vec4(1.0f), label);

        if (obtained)
        {
            tip.addLine(Color::positiveStat, "COMPLETED");
        }
        else
        {
            tip.addLine(Color::faded, "IN PROGRESS");
        }
    }
}

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

    if (isMouseInside() && button == SDL_BUTTON_LEFT)
    {
        onClicked();
    }
}