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 hud_timer: MarginContainer = $GetBackTimer
onready var time_label: Label = $GetBackTimer/TimeLabel
onready var currency: Label = $Currency
onready var level_state := get_tree().root.get_child(4).get_node("%LevelState")
onready var signal_manager := get_tree().root.get_child(4).get_node("%SignalManager")


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

func _process(_delta):
	if hud_timer.visible:
		time_label.text = String(round($GetBackTimer/Timer.time_left))
		signal_manager.emit_signal("remaining_time_changed", $GetBackTimer/Timer.time_left)

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


func _zoom_timer() -> void:
	var tween := create_tween()
	#tween.parallel().tween_property(hud_timer, "rect_position", Vector2(295, 0), 1.0)
	tween.parallel().tween_property(hud_timer, "rect_scale", Vector2(1,1), 1.0)


func update_interface() -> void:
	var wallet = GlobalState.gsr.wallet
	if level_state != null:
		wallet += level_state.currency

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

