extends CanvasLayer

var menu_selected = 0
var item_selected = 0
var item_no = []
var item_labels = []
var level_no = global.level_list.size()

func _ready():
	global.current_level = 0
	global.score = 0
	global.hud.clock.text = "00:00"
	var menus = [$MainMenu]
	for i in menus:
		item_labels.append(i.get_children())
		item_no.append(i.get_child_count()-1)
	upd_level_sel()
	color_option()
	
func _process(delta):
	var RIGHT = Input.is_action_just_pressed("ui_right")
	var UP = Input.is_action_just_pressed("ui_up")
	var LEFT = Input.is_action_just_pressed("ui_left")
	var DOWN = Input.is_action_just_pressed("ui_down")
	var SELECT = Input.is_action_just_pressed("shoot") 
	var BACK = Input.is_action_just_pressed("ui_cancel")

	if UP:
		decolor_option()
		if item_selected > 0:
			item_selected -= 1
		else:
			item_selected = item_no[menu_selected]
		color_option()
	elif DOWN:
		decolor_option()
		if item_selected < item_no[menu_selected]:
			item_selected += 1
		else:
			item_selected = 0
		color_option()
	
	if RIGHT:
		if menu_selected == 0:
			if item_selected == 0:
				if global.current_level < level_no-1:
					global.current_level += 1
				else:
					global.current_level = 0
				upd_level_sel()
	elif LEFT:
		if menu_selected == 0:
			if item_selected == 0:
				if global.current_level == 0:
					global.current_level = level_no-1
				else:
					global.current_level -=1
				upd_level_sel()

	if SELECT:
		decolor_option()
		match menu_selected:
			0:
				#Main menu
				match item_selected:
					0:
						set_process(false)
						global.game_root.change_scene("res://src/Main/GetReady.tscn",true,"fade_out",global.STATE.MENU)
					1: 
						$AnimationPlayer.play("credits")
						pass
		color_option()
	if BACK:
		decolor_option()

		color_option()

func decolor_option():
	item_labels[menu_selected][item_selected].modulate = Color.white
func color_option():
	item_labels[menu_selected][item_selected].modulate = Color.plum
func upd_level_sel():
	item_labels[0][0].text = "<PLAY STAGE "+str(global.current_level+1)+": "+global.level_names[global.current_level]+">"
