#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) {
    if(this->is_action_done() && this->get_action()->get_require_state_complete())
        this->end_state();
}

void State::interrupt_state() {
    this->_end_state();
    this->queue_free();
}

void State::end_state() {
    this->interrupt_state();
    this->emit_signal(this->is_action_done() ? "state_finished" : "state_failed");
    this->emit_signal("end_state");
}

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

void State::_end_state() {}

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

bool State::is_action_done_interrupt() const {
    return this->is_action_done() && !this->action->get_require_state_complete();
}
}
