extends Node2D

@onready var _lhs_fighter = $LhsFighter
@onready var _rhs_fighter = $RhsFighter
@onready var _timer = $Timer
@onready var _round_label = $CountdownLabel

signal next_round

var _lhs_figher_ready = false
var _rhs_figher_ready = false

var fight_script = {
	"lhs": [
		[],
		["Attack1.1", "Attack1.2", "Attack1.3"],
		[],
		[],
		["PowerUp"],
		["Attack2.1", "Attack2.2", "Attack2.3"],
		[],
		[],
		[],
	],
	"rhs": [
		[],
		[],
		["Attack1.1", "Attack1.2"],
		[],
		[],
		[],
		[],
		["Attack2.1"],
		[],
	]
}

# Called when the node enters the scene tree for the first time.
func _ready():
	_reset()
	_timer.start()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	if _lhs_fighter.is_animation_done() && _rhs_fighter.is_animation_done() && Input.is_anything_pressed():
		_reset()

func _reset():
	_lhs_fighter.init(fight_script["lhs"])
	_rhs_fighter.init(fight_script["rhs"])
	_round_label.init(len(fight_script["lhs"])+1)
	_lhs_figher_ready = true
	_rhs_figher_ready = true

func _on_timer_timeout():
	print_debug("%s -> Fight: _lhs_figher_ready -> %s && %s && %s && %s!" % [Time.get_ticks_msec(), _lhs_figher_ready, _rhs_figher_ready, _lhs_fighter.is_animation_idle(),_rhs_fighter.is_animation_idle()])
	if _lhs_figher_ready && _rhs_figher_ready && _lhs_fighter.is_animation_idle() && _rhs_fighter.is_animation_idle():
		_lhs_figher_ready = false
		_rhs_figher_ready = false
		
		print_debug("")
		next_round.emit()

func _on_lhs_fighter_actions_done():
	_lhs_figher_ready = true

func _on_rhs_fighter_actions_done():
	_rhs_figher_ready = true
