extends BossAttack

var fireball_scene = preload("res://scenes/nodes/boss/boss_attacks/fireball.tscn")

@export var fireball_directions : Array[Vector2]
@export var number_of_loops : int = 1
@export var frequency = 0.5
@export var fireball_speed = 300
var _iterator = 0

@export var distance_to_player : float = 300.0
var _frequency = 0.0

func start_prepare():
	if boss_behavior:
		boss_behavior.start_navigation(distance_to_player)

func prepare(_delta : float):
	if boss_behavior:
		boss_behavior.navigate()

func end_prepare():
	if boss_behavior:
		boss_behavior.end_navigation()

func start_perform():
	var fireball = fireball_scene.instantiate() as Fireball
	fireball.scale = Vector2(0.2, 0.2)
	add_child(fireball)
	fireball.global_position = get_parent().global_position
	var tween = get_tree().create_tween()
	tween.tween_property(fireball, "scale", Vector2(1, 1), 0.5)
	tween.play()
	await tween.finished
	fireball.queue_free()
	tween.kill()
	
	_frequency = frequency
	_iterator = 0

func perform(delta : float):
	if boss_behavior:
		boss_behavior.navigate()
	@warning_ignore("integer_division")
	if (_iterator / fireball_directions.size()) >= number_of_loops:
		emit_attack_performed()
		
	if _frequency > 0:
		_frequency -= delta
		if _frequency <= 0:
			var direction = fireball_directions[_iterator % fireball_directions.size()]
			var fireball = fireball_scene.instantiate() as Fireball
			add_child(fireball)
			fireball.global_position = get_parent().global_position
			fireball.start(direction, 1, fireball_speed)
			_iterator += 1
			_frequency = frequency
