class_name VisitorSpawner extends Node2D

@onready var timer : Timer = $Timer
@export var total_visitors : int = 0
@export var min_time : float = 5
@export var max_time : float = 10
var visitors_remaining : int
signal visitor_spawned(current : int, total : int)

func _ready() -> void:
	Guild.visitor_spawner = self
	visitors_remaining = total_visitors
	if visitors_remaining > 0:
		timer.start(randf_range(min_time, max_time))
	setup_ui.call_deferred()

func setup_ui():
	Game.setup_visitor_ui(self)


func spawn_visitor() -> void:
	Guild.spawn_visitor(global_position)
	visitors_remaining-=1
	visitor_spawned.emit(visitors_remaining, total_visitors)


func _on_timer_timeout() -> void:
	if visitors_remaining > 0:
		spawn_visitor()
	else:
		timer.stop()
