extends AnimationTree

const SUFFIX_LEFT := "_L"
const SUFFIX_RIGHT := "_R"

@onready var player: Player = $"../" as Player
@onready var attack: PlayerAttack = $"../Attack" as PlayerAttack
@onready var animation_player: AnimationPlayer = $"../Model/AnimationPlayer"


func _ready() -> void:
	assert(player, "player missing!")
	Music.track_started.connect(_on_music_track_started)
	attack.attacked.connect(_on_player_attacked)
	_set_bpm()
	_set_side()


func _process(_delta: float) -> void:
	advance(Music.audio_delta as float)


func _is_left() -> bool:
	return attack.side == PlayerAttack.Side.LEFT


func _suffix(invert: bool) -> String:
	if not invert:
		return SUFFIX_LEFT if _is_left() else SUFFIX_RIGHT
	return SUFFIX_LEFT if not _is_left() else SUFFIX_RIGHT


func _set_bpm() -> void:
	set(&"parameters/main_time_scale/scale", Music.bpm_factor)


func _set_side() -> void:
	set(&"parameters/idle_machine/conditions/side_L", _is_left())
	set(&"parameters/idle_machine/conditions/side_R", not _is_left())


func _on_music_track_started() -> void:
	_set_bpm()


func _on_player_attacked() -> void:
	_set_side()
