class_name SpawnController extends Node3D

@export_category("SpawnController")

@onready var blue_spawners: Node3D = $Blue
@onready var red_spawners: Node3D = $Red

const SINGLE_SPAWN_CONTROLLER_PATH = "res://scenes/maps/base/single_spawn_controller.gd"
enum Sides {BLUE, RED, UNDEFINED}

@export var side: Sides = Sides.UNDEFINED

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var single_spawn_controller: GDScript = ResourceLoader.load(SINGLE_SPAWN_CONTROLLER_PATH)
	for spawn: Node3D in blue_spawners.get_children():
		spawn.set_script(single_spawn_controller)
	for spawn: Node3D in red_spawners.get_children():
		spawn.set_script(single_spawn_controller)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _get_available_spawn(spawn_set: Node3D) -> Node3D:
	if multiplayer.is_server():
		var spawns: Array[Node3D] = []
		for spawn: Node3D in spawn_set.get_children():
			if not spawn.busy:
				spawns.push_back(spawn)
		var random_index: int = randi_range(0, spawns.size() - 1)
		return spawns[random_index]
	return null


func get_spawner(team: Sides) -> Vector3:
	match team:
		Sides.BLUE:
			var spawn := _get_available_spawn(blue_spawners)
			return spawn.choose_spawn_location()
		Sides.RED:
			print("red")
			return Vector3(0,0,0)
		_:
			return Vector3(0,0,0)
#	Get all spawners for each team
