#include "rts_actions.hpp"
#include "rts_states.hpp"
#include "goap/actor_world_state.hpp"
#include "goap/state.hpp"
#include <godot_cpp/core/memory.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

MoveToTarget::MoveToTarget()
: Action() {
    effects.insert("is_at_target", true);
}

goap::State *MoveToTarget::get_apply_state(goap::ActorWorldState *context) const {
    gd::Node3D *target = gd::Object::cast_to<gd::Node3D>(context->get_world_property("target_node"));
    if(target == nullptr) {
        gd::UtilityFunctions::push_warning("Failed to get target node of action ", get_static_class());
        return nullptr;
    } else {
        MoveTo *state = this->create_state<MoveTo>();
        state->target_node = target;
        return state;
    }
}

FireAtTarget::FireAtTarget()
: Action() {
    effects.insert("target_dead", true);
    required.insert("can_see_target", true);
}

goap::State *FireAtTarget::get_apply_state(goap::ActorWorldState *context) const {
    Animate *state = memnew(Animate);
    state->animation = "fire_weapon";
    return state;
}

FindTarget::FindTarget()
: Action() {
    effects.insert("can_see_target", true);
}

goap::State *FindTarget::get_apply_state(goap::ActorWorldState *context) const {
    gd::Node3D *target = gd::Object::cast_to<gd::Node3D>(context->get_world_property("target_node"));
    MoveTo *state = memnew(MoveTo);
    state->target_node = target;
    return state;
}
