extends Node2D
class_name Arena

@export var player: Player

@export var normal_color: Color
@export var bloacked_color: Color
@export var critical_color: Color
@export var hp_color: Color

@onready var wave_index_label: Label = %WaveIndexLabel
@onready var wave_time_label: Label = %WaveTimeLabel
@onready var spawner: Spawner = $Spawner
@onready var upgrade_panel: UpgradePanel = $GameUI/UpgradePanel

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	Global.player = player
	Global.on_create_block_text.connect(_on_create_block_text)
	Global.on_create_damage_text.connect(_on_create_damage_text)
	Global.on_upgrade_selected.connect(_on_upgrade_selected)
	Global.on_create_heal_text.connect(_on_create_heal_text)
	
	spawner.start_wave()

func _process(delta: float) -> void:
	if Global.game_paused: 
		return
	wave_index_label.text = spawner.get_wave_text()
	wave_time_label.text = spawner.get_wave_timer_text()

func crteat_floating_text(unit: Node2D) -> FloatingText:
	var instace := Global.FLOATING_TEXT_SCENE.instantiate() as FloatingText
	get_tree().root.add_child(instace)
	var random_pos := randf_range(0, TAU) * 35
	var spawn_pos := unit.global_position + Vector2.RIGHT.rotated(random_pos)
	instace.global_position = spawn_pos
	
	return instace

func show_upgrades() -> void:
	upgrade_panel.show()

func start_new_wave() -> void:
	Global.game_paused = false
	Global.player.update_player_new_weave()
	spawner.wave_index += 1
	spawner.start_wave()

func _on_create_block_text(unit: Node2D) -> void:
	var text := crteat_floating_text(unit)
	text.setup("Blocked!", bloacked_color)
	
	
	
func _on_create_damage_text(unit: Node2D, hitbox: HitboxComponent) -> void:
	var text := crteat_floating_text(unit)
	var color := critical_color if hitbox.critical else normal_color
	text.setup(str(hitbox.damage), color)
	
func _on_create_heal_text(unit: Node2D, heal: float) -> void:
	var text := crteat_floating_text(unit)
	text.setup("+ %s" % heal, hp_color)
	
func _on_upgrade_selected() -> void:
	upgrade_panel.hide()
	start_new_wave()

func _on_spawner_on_wave_completed() -> void:
	if not Global.player: return
	await get_tree().create_timer(1.0).timeout
	show_upgrades()
