extends Node2D

var current_menu = 0
var current_menu_item = 0
var menu_labels = ["start", "settings", "records", "exit"]
var settings_labels = ["sounds: ", "music: ", "mute: ", "cancel"]
var settings_parameters = [100, 100, false, ""]
 
func _process(delta):
	var up = Input.is_action_just_pressed("ui_up")
	var down = Input.is_action_just_pressed("ui_down")
	var left = Input.is_action_pressed("ui_left")
	var right = Input.is_action_pressed("ui_right")
	var enter = Input.is_action_just_pressed("ui_accept")

	if up:
		var new_current_menu_item = fposmod(current_menu_item - 1, 4)
		change_menu_item(current_menu_item, new_current_menu_item)
		current_menu_item = new_current_menu_item
	elif down:
		var new_current_menu_item = fposmod(current_menu_item + 1, 4)
		change_menu_item(current_menu_item, new_current_menu_item)
		current_menu_item = new_current_menu_item
	elif enter:
		accept()
		
	if left:
		change_parameter(-1)
	elif right:
		change_parameter(1)
		
func change_parameter(x):
	if current_menu == 1:
		if current_menu_item == 0:
			settings_parameters[0] = clamp(settings_parameters[0], 1, 99) + x
			$menu.get_child(0).text = "<<< " + settings_labels[0] + str(settings_parameters[0]) + " >>>"
			change_sounds_volume(settings_parameters[0])
		elif current_menu_item == 1:
			settings_parameters[1] = clamp(settings_parameters[1], 1, 99) + x
			$menu.get_child(1).text = "<<< " + settings_labels[1] + str(settings_parameters[1]) + " >>>"
			change_music_volume(settings_parameters[1])
			
func change_menu_item(c, n):
	if current_menu == 0:
		$menu.get_child(c).text = menu_labels[c]
		$menu.get_child(n).text = "--- " + menu_labels[n] + " ---"
	elif current_menu == 1:
		if c != 2:
			$menu.get_child(c).text = settings_labels[c] + str(settings_parameters[c])
		elif c == 2:
			if settings_parameters[2] == false:
				$menu.get_child(2).text = settings_labels[2] + "no"
			else:
				$menu.get_child(2).text = settings_labels[2] + "yes"
		if n == 0 or n == 1:
			$menu.get_child(n).text = "<<< " + settings_labels[n] + str(settings_parameters[n]) + " >>>"
		elif n == 2:
			if settings_parameters[2] == false:
				$menu.get_child(2).text = "--- " + settings_labels[2] + "no" + " ---"
			else:
				$menu.get_child(2).text = "--- " + settings_labels[2] + "yes" + " ---"
		else:
			$menu.get_child(n).text = "--- " + settings_labels[n] + str(settings_parameters[n]) + " ---"
			
func accept():
	if current_menu == 0:
		if current_menu_item == 0:
			start_game()
		elif current_menu_item == 1:
			show_settings_menu()
		elif current_menu_item == 2:
			pass
		elif current_menu_item == 3:
			get_tree().quit()
	elif current_menu == 1:
		if current_menu_item == 2:
			settings_parameters[2] = !settings_parameters[2]
			if settings_parameters[2] == false:
				$menu.get_child(2).text = "--- " + settings_labels[2] + "no" + " ---"
			else:
				$menu.get_child(2).text = "--- " + settings_labels[2] + "yes" + " ---"
			mute_audio(settings_parameters[2])
		if current_menu_item == 3:
			show_main_menu()
			
func show_main_menu():
	var items = $menu.get_children()
	for i in items:
		i.text = menu_labels[i.get_index()]
	items[0].text = "--- " + menu_labels[0] + " ---"
	current_menu = 0
	current_menu_item = 0
		
func show_settings_menu():
	var items = $menu.get_children()
	for i in items:
		if i.get_index() == 2:
			if settings_parameters[2] == false:
				$menu.get_child(2).text = settings_labels[2] + "no"
			else:
				$menu.get_child(2).text = settings_labels[2] + "yes"
		else:
			i.text = settings_labels[i.get_index()] + str(settings_parameters[i.get_index()])
			
	items[0].text = "<<< " + settings_labels[0] + str(settings_parameters[0]) + " >>>"
	current_menu = 1
	current_menu_item = 0
	
func change_sounds_volume(v):
	var volume = range_lerp(v, 0, 100, -72, 0)
	AudioServer.set_bus_volume_db(1, volume)
	$sounds_volume_test.play()
	
func change_music_volume(v):
	var volume = range_lerp(v, 0, 100, -72, 0)
	AudioServer.set_bus_volume_db(2, volume)
	$music_volume_test.play()
	
func mute_audio(m):
	AudioServer.set_bus_mute(0, m)
	$music_volume_test.play()
		
func start_game():
	get_tree().change_scene("res://scenes/game.tscn")
	
func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	global.highscore = global.load_highscore()