extends CanvasLayer

var pause_menu_labels = ["continue", "restart", "exit"]
var current_menu_item = 0
var paused = false

func _input(event):
	if event.is_action_released("ui_enter"):
		if global.game_over:
			global.restart_level()
		if get_tree().paused:
			accept()
	if event.is_action_released("ui_esc"):
		if !paused: 
			get_tree().paused = true
			ui_show_pause_menu()
			paused = true
		else: 
			get_tree().paused = false
			ui_hide_pause_menu()
			paused = false
	if paused:
		if event.is_action_released("ui_down"):
			var new_current_menu_item = fposmod(current_menu_item + 1, 3)
			change_menu_item(current_menu_item, new_current_menu_item)
			current_menu_item = new_current_menu_item
		elif  event.is_action_released("ui_up"):
			var new_current_menu_item = fposmod(current_menu_item - 1, 3)
			change_menu_item(current_menu_item, new_current_menu_item)
			current_menu_item = new_current_menu_item
			
func change_menu_item(c, n):
	$ui/pause_menu.get_child(c).text = pause_menu_labels[c]
	$ui/pause_menu.get_child(n).text = "--- " + pause_menu_labels[n] + " ---"
		
func ui_show_pause_menu():
	$ui/pause_menu.show()

func ui_hide_pause_menu():
	$ui/pause_menu.hide()
	
func accept():
	get_tree().paused = false
	if current_menu_item == 0:
		ui_hide_pause_menu()
	elif current_menu_item == 1:
		global.restart_level()
	elif current_menu_item == 2:
		get_tree().change_scene("res://scenes/start_screen.tscn")
	
func ui_update_score(s):
	$ui/score.text = ("%08d" % s)
	
func ui_update_high_score(hs):
	$ui/high_score.text = ("%08d" % hs)
	
func ui_update_lives(l):
	var lives = $ui/lives.get_children()
	for live in lives:
		live.visible = false
	for i in range(l):
		lives[i].visible = true
		
func ui_update_enemies_count(c):
	$ui/enemies_count.text = str(c)
		
func ui_show_game_over_screen(s):
	var show_timer = Timer.new()
	add_child(show_timer)
	show_timer.wait_time = 1
	show_timer.start()
	yield(show_timer, "timeout")
#	$ui/frame.modulate = Color(1,0,0)
	$ui/game_over_label.visible = true
	$ui/final_score.text = "your score: " + ("%08d" % s)
	$ui/final_score.visible = true
	$ui/anim.play("New Anim")
	
func ui_update_wave(w):
	$ui/waves.text = "wave: " + str(w)
	
func _ready():
	global.ui = self