#include "rts_items.hpp"
#include "unit.hpp"
#include "entity_health.hpp"

Medkit::Medkit()
: Item() {
    this->capabilities.insert(ItemCapability::Consume);
    this->capabilities.insert(ItemCapability::Heal);
    this->capabilities.insert(ItemCapability::Revive);
}

bool Medkit::can_use_on(Unit *used_by, gd::Object *used_on) const {
    return gd::Object::cast_to<Unit>(used_on) != nullptr;
}

void Medkit::use_on(Unit *used_by, gd::Object *used_on) const {
    gd::Object::cast_to<Unit>(used_on)->get_entity_health()->healed_by(3, used_by);
}

Handgun::Handgun()
: Item() {
    this->animation = "fire_weapon";
    this->capabilities.insert(ItemCapability::WeaponEquip);
    this->capabilities.insert(ItemCapability::WeaponRanged);
}

bool Handgun::can_use_on(Unit *used_by, gd::Object *used_on) const {
    return gd::Object::cast_to<Unit>(used_on) != nullptr
        && used_by->get_world_state()->get_can_see_target();
}

void Handgun::apply_stats(Stats &stats) const {
    stats.weapon_range = 10.f;
}

void Handgun::use_on(Unit* used_by, gd::Object* used_on) const {
    Unit *target{gd::Object::cast_to<Unit>(used_on)};
    used_by->aim_at(target);
    EntityHealth *health{target->get_entity_health()};
    health->damaged_by(4, used_by);
    gd::Vector3 const owner_position{used_by->get_global_position()};
    gd::Vector3 const target_position{target->get_global_position()};
    used_by->look_at(owner_position-(target_position-owner_position));
}

Lasercutter::Lasercutter()
: Item() {
    this->capabilities.insert(ItemCapability::UtilityEquip);
}

Welder::Welder()
: Item() {
    this->capabilities.insert(ItemCapability::UtilityEquip);
}
