#include <Widget/FluidLevelIndicator/FluidLevelIndicator.hpp>
#include <Fluid/Fluid.hpp>
#include <FluidRegistry/FluidRegistry.hpp>
#include <BlockRegistry/BlockRegistry.hpp>
#include <Utilities/Utilities.hpp>

FluidLevelIndicator::FluidLevelIndicator(const std::string &fluidName, const int &amount, int capacity) :
    m_fluidName(fluidName),
    m_amount(amount),
    m_capacity(capacity)
{}

glm::ivec2 FluidLevelIndicator::getMinSize()
{
    return glm::ivec2(10, m_capacity / 1000 + 1);
}

void FluidLevelIndicator::draw(GraphicsContext &context)
{
    context.color = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
    context.drawRect(0, 0, 10, m_capacity/1000 + 1);

    context.color = Color::faded;
    context.drawRect(1, 1, 8, m_capacity/1000 - 1);

    context.color = glm::vec4(1.0f);

    Fluid *fluid = fluidRegistry.getFluidByName(m_fluidName);
    if (fluid != nullptr)
    {
        int fluidX = 1;
        int fluidWidth = 8;
        int fluidHeight = m_amount / 1000;

        context.drawMaterial(
            fluid->icon,
            0, fluid->icon.height() - (fluidHeight % fluid->icon.height()),
            fluidWidth, fluidHeight,
            fluidX, m_capacity / 1000 - fluidHeight,
            fluidWidth, fluidHeight
        );

        if (isMouseInside())
        {
            ToolTip &tip = context.createToolTip();
            tip.addLine(glm::vec4(1.0f), fluid->displayName);
            tip.addLine(formatString("$+/$+ ml", m_amount, m_capacity));
        }
    }
    else if (isMouseInside())
    {
        ToolTip &tip = context.createToolTip();
        tip.addLine(formatString("$+/$+ ml", m_amount, m_capacity));
    }

    context.color = glm::vec4(0.0f, 0.0f, 0.0f, 1.0);
    
    int mark;
    for (mark=5000; mark<=m_capacity; mark+=5000)
    {
        int plotY = (m_capacity - mark)/1000;
        int markWidth = (mark % 10000) == 0 ? 4 : 2;

        if (mark == m_capacity)
        {
            markWidth = 9;
        }

        context.drawRect(1, plotY, markWidth, 1);
    }
}