#include <Display/WidgetDisplay/WorldDisplay/WorldDisplay.hpp>
#include <Item/StatefulItem/Shoes/Shoes.hpp>
#include <Utilities/Utilities.hpp>
#include <string>

glm::vec4 Shoes::getUsesColor(int uses)
{
    if (uses > maxUses/2)
    {
        return Color::toolUsesLeftHigh;
    }
    else if (uses > maxUses/5)
    {
        return Color::toolUsesLeftMedium;
    }
    else
    {
        return Color::toolUsesLeftLow;
    }
}

Shoes::Shoes(std::string name, std::string displayName, Material &icon, int maxUses, float acceleration) :
    StatefulItem(name),
    displayName(displayName),
    icon(icon),
    maxUses(maxUses),
    acceleration(acceleration)
{
    setMaxStackSize(1);
}

void Shoes::draw(GraphicsContext &context, int x, int y, const ItemStack &stack)
{
    context.color = glm::vec4(1.0f);
    context.drawMaterial(icon, x, y);

    uint32_t usesLeft = maxUses - state(stack);
    if (usesLeft == maxUses)
    {
        return;
    }

    context.color = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
    context.drawRect(x, y+7, 8, 1);

    context.color = getUsesColor(usesLeft);
    context.drawRect(x, y+7, 8 * usesLeft / maxUses, 1);
}

std::string Shoes::getDisplayName(const ItemStack &stack)
{
    return displayName;
}

void Shoes::draw3D(GraphicsContext &context, WorldDisplay &worldDisplay, glm::vec3 pos, ItemStack &stack)
{
    worldDisplay.drawMeshedIcon(context, icon, pos);
}

void Shoes::drawFlat(GraphicsContext &context, WorldDisplay &worldDisplay, glm::mat4 matrix, ItemStack &stack)
{
    worldDisplay.drawMeshedIconFlat(context, icon, matrix);
}

float Shoes::generateAcceleration(ItemStack &stack)
{
    int &uses = state(stack);
    if (uses == maxUses)
    {
        return 0.0f;
    }

    uses++;
    return acceleration;
}

void Shoes::getExtraToolTipLines(const ItemStack &stack, ToolTip &tip)
{
    uint32_t usesLeft = maxUses - state(stack);
    tip.addLine(
        formatString(
            "Running left: $+$+",
            GraphicsContext::colorstr(getUsesColor(usesLeft)),
            formatTicks(usesLeft)
        )
    );
}