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

namespace game{
namespace equipment {

Shield::Shield(float health, float cooldown, float restoreSpeed) noexcept
{
    this->health = this->capacity = health;
    this->cooldown = cooldown;
    this->restoreTime = 0;
    this->restoreSpeed = restoreSpeed;
}

float Shield::Hit(float damage) noexcept
{
    this->restoreTime = this->cooldown;

    this->health -= damage;

    if (this->health < 0) {
        damage = this->health * -1;
        this->health = 0;

        return damage;
    }

    return 0;
}

void Shield::Update() noexcept
{
    if (this->restoreTime > 0) {
        this->restoreTime -= window::deltaTime;
        return;
    }

    if (this->health < this->capacity) {
        this->health += this->restoreSpeed * window::deltaTime;

        if (this->health > this->capacity) {
            this->health = this->capacity;
        }
    }
}

void Shield::Render(vec2f position, vec2f size) const noexcept
{
    if (this->health > 0) {
        render::primitives2d::circle(position, size, color::yellow * color::alpha(0.4), 0, 42);
    }
}

}
}
