extends CanvasLayer

const DEBUG_LOGS := false

@export var simulation_manager: Node

@onready var village_stats_label: Label = $VillagePanel/MarginContainer/VillageStatsLabel


func _ready() -> void:
	if simulation_manager == null:
		push_error("VillageUI: simulation_manager missing")
		return

	if simulation_manager.has_signal("village_changed"):
		simulation_manager.village_changed.connect(_on_village_changed)
	else:
		push_error("VillageUI: SimulationManager has no village_changed signal")

	if "village" in simulation_manager:
		_on_village_changed(simulation_manager.village)


func _on_village_changed(village: SimVillage) -> void:
	village_stats_label.text = (
		"""
	Village

	Food: %s
	Wood: %s
	Safety: %s
	Knowledge: %s
	Starving: %s

	Food Mod: %.2f
	Safety Mod: %.2f
	Knowledge Mod: %.2f
	"""
		% [
			round(village.food),
			round(village.wood),
			round(village.safety),
			round(village.knowledge),
			simulation_manager.get_starving_count(),
			village.food_modifier,
			village.safety_modifier,
			village.knowledge_modifier
		]
	)
