extends Control

onready var scene = $"/root/scene" as GameInstance

func _physics_process(_delta):
	if not visible:
		return
	var children = scene.systems.get_children()
	var total_sink = 0
	var total_source = 0
	var total_alerts = 0
	for child in children:
		if child is PowerNetwork:
			var network = child as PowerNetwork
			total_sink += network.total_usage
			total_source += network.total_source
			total_alerts += network.unpowered
	$Margin/Container/EnergyBox/CurrentSource/Value.text = "%.0f kW" % total_source
	$Margin/Container/EnergyBox/CurrentSink/Value.text = "%.0f kW" % total_sink
	$Margin/Container/EnergyBox/CurrentAlerts/Value.text = str(total_alerts)

func _map_button_pressed():
	scene.ui.open_popup(GameUI.PopupName.EnergyUsage)

func force_close():
	scene.ui.close_popup(GameUI.PopupName.EnergyUsage)
