extends Node3D
## Tower Defense — Lecture 26 (Starter)
##
## Main game world with enemy spawning, wave management,
## and player health tracking. Enemies follow a Path3D and
## damage the player when they reach the end.

# ============================================================
# CONSTANTS
# ============================================================

const PLAYER_START_HP := 100

# Enemy types and wave definitions
const WAVE_1 := ["slime", "slime", "slime"]
const WAVE_2 := ["slime", "slime", "goblin", "slime"]
const WAVE_3 := ["goblin", "goblin", "slime", "goblin", "slime"]
const WAVE_4 := ["goblin", "goblin", "goblin", "orc"]
const WAVE_5 := ["orc", "goblin", "goblin", "orc", "slime"]

const SPAWN_DELAY := 1.0  # Seconds between each enemy in a wave
const WAVE_DELAY := 5.0   # Seconds between waves

# ============================================================
# STATE
# ============================================================

var player_hp: int = PLAYER_START_HP
var current_wave: int = 0
var enemies_remaining_in_wave: int = 0
var is_spawning: bool = false

# Wave list
var waves: Array[Array] = [WAVE_1, WAVE_2, WAVE_3, WAVE_4, WAVE_5]

# ============================================================
# NODES
# ============================================================

@onready var hp_label: Label = $UI/HPLabel
@onready var wave_label: Label = $UI/WaveLabel
@onready var spawn_timer: Timer = $SpawnTimer
@onready var wave_timer: Timer = $WaveTimer
@onready var enemy_path: Path3D = $EnemyPath

# Enemy scene preload
var enemy_scene := preload("res://scenes/enemy.tscn")

# ============================================================
# LIFECYCLE
# ============================================================

func _ready() -> void:
	_update_ui()
	_start_next_wave()


# ============================================================
# WAVE MANAGEMENT
# ============================================================

## Starts the next wave if available
func _start_next_wave() -> void:
	if current_wave >= waves.size():
		_game_won()
		return
	
	current_wave += 1
	var wave_enemies := waves[current_wave - 1]
	enemies_remaining_in_wave = wave_enemies.size()
	
	_update_ui()
	
	# Start spawning enemies
	is_spawning = true
	_spawn_next_enemy_in_wave(wave_enemies, 0)


## Recursively spawns enemies with delay
func _spawn_next_enemy_in_wave(wave_enemies: Array, index: int) -> void:
	if index >= wave_enemies.size():
		is_spawning = false
		return
	
	_spawn_enemy(wave_enemies[index])
	
	# Schedule next enemy
	await get_tree().create_timer(SPAWN_DELAY).timeout
	_spawn_next_enemy_in_wave(wave_enemies, index + 1)


## Spawns a single enemy of the given type
func _spawn_enemy(enemy_type: String) -> void:
	# === TODO 1: Instantiate and configure enemy ===
	# Instantiate enemy_scene
	# Add enemy as child of enemy_path
	# Set enemy.enemy_type to the passed enemy_type parameter
	# Call enemy.initialize()
	# Connect enemy.reached_end signal to _on_enemy_reached_end
	# Connect enemy.died signal to _on_enemy_died
	pass


# ============================================================
# ENEMY CALLBACKS
# ============================================================

## Called when enemy reaches end of path
func _on_enemy_reached_end(damage: int) -> void:
	# === TODO 2: Handle enemy reaching the end ===
	# Subtract damage from player_hp
	# Clamp player_hp to minimum of 0 using maxi()
	# Call _update_ui()
	# Decrement enemies_remaining_in_wave
	# Call _check_wave_complete()
	# If player_hp <= 0, call _game_over()
	pass


## Called when enemy is killed by tower
func _on_enemy_died() -> void:
	# === TODO 3: Handle enemy death ===
	# Decrement enemies_remaining_in_wave
	# Call _check_wave_complete()
	pass


## Checks if wave is complete and starts next wave
func _check_wave_complete() -> void:
	# === TODO 4: Check wave completion and schedule next wave ===
	# If enemies_remaining_in_wave <= 0 AND not is_spawning:
	#   Start wave_timer with WAVE_DELAY seconds
	#   Await wave_timer.timeout
	#   Call _start_next_wave()
	pass


# ============================================================
# UI
# ============================================================

func _update_ui() -> void:
	hp_label.text = "HP: %d" % player_hp
	wave_label.text = "Wave: %d / %d" % [current_wave, waves.size()]


# ============================================================
# GAME OVER / WIN
# ============================================================

func _game_over() -> void:
	print("Game Over!")
	get_tree().paused = true
	# TODO: Show game over screen in Lecture 28


func _game_won() -> void:
	print("Victory! All waves defeated!")
	get_tree().paused = true
	# TODO: Show victory screen in Lecture 28
