#include <render/primitives.h>
#include <color.h>
#include <window.h>
#include "healthBar.h"

namespace game {

constexpr vec4f losedHealthColor = color::uint2rgba(0xff0044);
constexpr vec4f addedHealthColor = color::uint2rgba(0x2ce8f5);
constexpr vec4f remainHealthColor = color::uint2rgba(0x0095e9);
constexpr vec4f alarmHealthColor = color::uint2rgba(0xe800dc);
constexpr vec4f backgroundColor = color::uint2rgba(0x262b44);
constexpr vec4f alarmBackgroundColor = color::uint2rgba(0x422533);

void HealthBar::Reset(int health)
{
    this->health = health;
    this->healthDiff = 0;
    this->effectTime = 0;
}

void HealthBar::SetHealth(int health)
{
    if (this->health == health) {
        return;
    }

    healthDiff = health - this->health;
    this->health = health;
    effectTime = 1;
}

void HealthBar::Update()
{
    if (effectTime > 0) {
        effectTime -= window::deltaTime;
    }
}

void HealthBar::Render()
{
    vec4f healthColor;
    vec4f background;

    if (health > alarmHealth) {
        healthColor = remainHealthColor;
        background = backgroundColor;
    } else {
        healthColor = alarmHealthColor;
        background = alarmBackgroundColor;
    }

    render::primitives2d::quad({0, .25}, {10, 1}, background);
    render::primitives2d::quad({0, .25}, {(float) health / 10.0f, 1}, healthColor);

    if (effectTime > 0) {
        vec4f startColor;

        float length = (float) abs(healthDiff) / 10.0f;
        float position = (float) health / 10.0f;

        if (healthDiff > 0) {
            startColor = addedHealthColor;
            position -= length;
        } else {
            startColor = losedHealthColor;
        }

        startColor = mix(startColor, vec4f(1,1,1,0), effectTime);

        render::primitives2d::quad({position, .25}, {length, 1}, startColor);
    }
}

}

