extends Control

# Smart ist es die notwendigen Resourcen vor dem Skriptstart zu laden
onready var current_scene := get_tree().get_current_scene()
onready var pause_overlay: ColorRect = $HUDOverlay
onready var timer: Label = $HUDOverlay/GetBackTimer
onready var currency: Label = $HUDOverlay/Currency
onready var kills: Label = $HUDOverlay/Kills
onready var frees: Label = $HUDOverlay/Frees
onready var levelState := get_tree().root.get_child(3).get_node("%LevelState")
onready var signalManager := get_tree().root.get_child(3).get_node("%SignalManager")


func _ready():
	#TODO Connect what HOW?
	signalManager.connect("currency_updated", self, "update_interface")
	signalManager.connect("kills_updated", self, "update_interface")
	signalManager.connect("frees_updated", self, "update_interface")
	signalManager.connect("terminal_activated", self, "start_timer")
	update_interface()

func _process(delta):
	if timer.visible:
		timer.text = String(round($HUDOverlay/GetBackTimer/Timer.time_left))

func start_timer(time):
	timer.visible = true
	$HUDOverlay/GetBackTimer/Timer.wait_time = time
	$HUDOverlay/GetBackTimer/Timer.start()
	$HUDOverlay/GetBackTimer/AnimationPlayer.play("Redlight")
	_zoom_timer()


func _zoom_timer() -> void:
	var tween := create_tween()
	tween.tween_property(timer, "rect_scale", Vector2(1,1), 1.0)


func update_interface() -> void:
	var wallet = GlobalState.gsr.wallet
	if levelState != null:
		wallet += levelState.currency
		kills.text = "Kills: %s" % levelState.kills
		frees.text = "Freed: %s" % levelState.frees

	currency.text = "Orbs: %s" % wallet

