#include "state.hpp"
#include "goap/action.hpp"
#include "goap/actor_world_state.hpp"
#include "utils/godot_macros.hpp"

namespace goap {
void State::_bind_methods() {
#define CLASSNAME State
    GDSIGNAL("state_finished");
    GDSIGNAL("state_failed");
    GDSIGNAL("end_state");
}

void State::_enter_tree() {
    this->world_state = this->get_node<ActorWorldState>("../ActorWorldState");
}

void State::_process(double delta_time) {
    if(this->is_action_done())
        this->state_finished();
}

Action const *State::get_action() const {
    return this->action;
}

void State::_end_state() {}

void State::state_finished() {
    this->end_state();
    this->emit_signal("state_finished");
}

void State::state_failed() {
    this->end_state();
    this->emit_signal("state_failed");
}

bool State::is_action_done() const {
    return this->action->is_completed(this->world_state);
}

void State::end_state() {
    this->_end_state();
    this->queue_free();
    this->emit_signal("end_state");
}
}