#include <Widget/BatteryView/BatteryView.hpp>
#include <ToolTip/ToolTip.hpp>
#include <Utilities/Utilities.hpp>

BatteryView::BatteryView(const int &energy, int capacity, int divisor, int width) :
    m_energy(energy),
    m_capacity(capacity),
    m_divisor(divisor),
    m_width(width)
{}

void BatteryView::draw(GraphicsContext &context)
{
    if (isMouseInside())
    {
        ToolTip &tip = context.createToolTip();
        tip.addLine(formatString("$+/$+ J", m_energy, m_capacity));
    }
    
    context.color = m_energy == m_capacity ? Color::batteryTipCharged : Color::batteryTip;
    context.drawRect(m_width/3, 0, m_width-(2*m_width/3), 1);

    context.color = glm::vec4(1.0f);
    context.drawRect(0, 1, m_width, m_capacity/m_divisor-1);

    context.color = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
    context.drawRect(1, 2, m_width-2, m_capacity/m_divisor-3);

    int glowHeight = m_energy / m_divisor;
    if (glowHeight == m_capacity/m_divisor)
    {
        glowHeight--;
    }

    context.color = Color::batteryCharge;
    context.drawRect(0, m_capacity/m_divisor - glowHeight, m_width, glowHeight);

    int subglowHeight = std::min(glowHeight-1, m_capacity/m_divisor-3);
    if (subglowHeight <= 0) return;

    context.color = Color::batterySubglow;
    context.drawRect(1, m_capacity/m_divisor - 1 - subglowHeight, m_width-2, subglowHeight);
}

glm::ivec2 BatteryView::getMinSize()
{
    return glm::ivec2(m_width, m_capacity/m_divisor);
}