#include "damage_box.h"
#include "health_status.h"
#include "hitbox.h"

void DamageBox::_bind_methods() {
	ClassDB::bind_method(D_METHOD("attack_motion_begin"), &self_type::attack_motion_begin);
	ClassDB::bind_method(D_METHOD("attack_motion_end"), &self_type::attack_motion_end);
}

void DamageBox::attack_motion_begin() {
	set_monitoring(true);
}

void DamageBox::attack_motion_end() {
	this->already_hit.clear();
	set_monitoring(false);
}

void DamageBox::on_body_entered(Node3D *node) {
	if (Hitbox * box{ cast_to<Hitbox>(node) }) {
		if (!this->already_hit.has(box->get_health())) {
			box->get_health()->damage(this->damage * box->get_damage_modifier());
			this->already_hit.insert(box->get_health());
		}
	}
}

void DamageBox::_notification(int what) {
	if (Engine::get_singleton()->is_editor_hint()) {
		return;
	}
	switch (what) {
		default:
			return;
		case NOTIFICATION_ENTER_TREE:
			connect("body_entered", callable_mp(this, &self_type::on_body_entered));
			connect("area_entered", callable_mp(this, &self_type::on_body_entered));
			set_monitoring(false);
			return;
	}
}

void DamageBox::set_damage(int amount) {
	this->damage = amount;
}

int DamageBox::get_damage() const {
	return this->damage;
}
