#include "health.hpp"
#include "utils/godot_macros.h"
#include <algorithm>
#include <godot_cpp/core/object.hpp>
#include <godot_cpp/core/property_info.hpp>

namespace godot {
void Health::_bind_methods() {
#define CLASSNAME Health
    GDPROPERTY(max_health, Variant::INT);
    GDSIGNAL("damage", PropertyInfo(Variant::INT, "delta"), PropertyInfo(Variant::INT, "remaining"));
    GDSIGNAL("heal", PropertyInfo(Variant::INT, "delta"), PropertyInfo(Variant::INT, "remaining"));
    GDSIGNAL("health_changed", PropertyInfo(Variant::INT, "delta"), PropertyInfo(Variant::INT, "remaining"));
    GDSIGNAL("death", PropertyInfo(Variant::INT, "delta"));
}

void Health::_enter_tree() {
    this->health = this->max_health;
}

void Health::damage(int amount) {
    this->health -= amount;
    this->emit_signal("damage", amount, this->health);
    this->emit_signal("health_changed", -amount, this->health);
    if(this->health <= 0) {
        this->emit_signal("death", amount);
    }
}

void Health::heal(int amount) {
    this->health = std::min(this->health + amount, this->max_health);
    this->emit_signal("health_changed", amount, this->health);
    this->emit_signal("heal", amount, this->health);
}

int Health::get_health() const {
    return this->health;
}

void Health::set_max_health(int max_health) {
    this->max_health = std::abs(max_health);
}

int Health::get_max_health() const {
    return this->max_health;
}
}
