extends Node3D

signal heavy_attack()

@export var animation_speed: float = 10.0

@onready var animation_tree: AnimationTree = $AnimationTree
@onready var playback: AnimationNodeStateMachinePlayback = animation_tree["parameters/playback"]
@onready var skeleton_3d: Skeleton3D = $CharacterRig/GameRig/Skeleton3D
@onready var off_hand_slot: Node3D = %OffHandSlot
@onready var villager_meshes: Array[MeshInstance3D] = [
	$CharacterRig/GameRig/Skeleton3D/Villager_01,	
	$CharacterRig/GameRig/Skeleton3D/Villager_02
]

var run_path: String = "parameters/MoveSpace/blend_position"
var run_weight_target: float = -1.0

func _physics_process(delta: float) -> void:
	animation_tree[run_path] = move_toward(
		animation_tree[run_path],
		run_weight_target,
		delta * animation_speed
	)

func update_animation_tree(direction: Vector3) -> void:
	if direction.is_zero_approx():
		run_weight_target = -1.0
	else:
		run_weight_target = 1.0


func is_idle() -> bool:
	return playback.get_current_node() == "MoveSpace"

func is_dashing() -> bool:
	return playback.get_current_node() == "Dash"

func is_slashing() -> bool:
	return playback.get_current_node() == "Slash"

func is_overhead() -> bool:
	return playback.get_current_node() in ["Overhead", "OverheadRecover"]

func set_active_mesh(active_mesh: MeshInstance3D) -> void:
	for child in skeleton_3d.get_children():
		child.visible = false
	active_mesh.visible = true

func equip_offhand(equipment_scene: PackedScene) -> void:
	for child in off_hand_slot.get_children():
		child.queue_free()
	var new_equipment = equipment_scene.instantiate()
	off_hand_slot.add_child(new_equipment)

func _on_animation_tree_animation_finished(anim_name: StringName) -> void:
	if anim_name == "Overhead":
		heavy_attack.emit()
