extends CanvasLayer
class_name HUD

export var coins_amount:int = 0 
export var PauseMenu: PackedScene;
onready var Lable = get_node("CoinsContainer/Label")

var _paused = false;
var _pause_ref;
var _data_ref = null;
func _ready() -> void:
	_data_ref = GameState.get_run_data()
	var time_remaining = (GameState.GAME_TIMEOUT_MIN * 60) - _data_ref.time
	$UI/TimeLabel.text = Utils.parse_seconds_to_display_str(time_remaining)

func _physics_process(delta: float) -> void:
	_handle_pause()
	var fresh_data = GameState.get_run_data()
	for key in fresh_data:
		if not fresh_data[key] == _data_ref[key]:
			match(str(key)):
				"coins":
					$UI/CoinLabel.text = str(fresh_data.coins)
					_play_update_label_animation($UI/CoinLabel/AnimationPlayer)
				"orbs":
					$UI/OrbsLable.text = str(fresh_data.orbs)
					_play_update_label_animation($UI/OrbsLable/AnimationPlayer)
				"deaths":
					$UI/DeathsLabel.text = str(fresh_data.deaths)
					_play_update_label_animation($UI/DeathsLabel/AnimationPlayer)
				"time":
					var time_remaining = (GameState.GAME_TIMEOUT_MIN * 60) - fresh_data.time
					$UI/TimeLabel.text = Utils.parse_seconds_to_display_str(time_remaining)
					
			pass
	_data_ref = fresh_data
	
func _play_update_label_animation(player:AnimationPlayer):
	player.seek(0)
	player.play("updated")
	
func _handle_pause():
	if Input.is_action_just_pressed("pause"):
		if _paused:
			_pause_ref.dismiss()
		else:
			_pause_ref = PauseMenu.instance()
			_pause_ref.connect("dismissed", self,"_on_pause_menu_dismissed")
			add_child(_pause_ref)
			get_tree().paused = true
			_paused = true
				
func _on_pause_menu_dismissed():
	_paused = false
	get_tree().paused = false
