extends CanvasLayer

var waiting_for_bind = false
var paused = false
var finish = false
var current_menu = "main_menu"
var current_binding = ""

signal _pressed

func _input(event):
	if waiting_for_bind:
		if event as InputEventKey:
			if event.scancode == 16777217:
				reset_controlls_binding()
			else:
				var _key = event.scancode
				emit_signal("_pressed", event)
		elif event as InputEventJoypadButton:
			var _key = event.button_index
			emit_signal("_pressed", event)
	elif event as InputEventKey:
		if event.is_pressed() and event.scancode == 16777217:
			if !paused and !finish:
				pause()
			else:
				if current_menu == "main_menu":
					unpause()
				elif current_menu == "controlls_menu":
					_on_cancel_button_pressed()
				elif current_menu == "options_menu":
					_on_cancel_button_2_pressed()
				elif current_menu == "leaderboard":
					_on_cancel_button_3_pressed()

func pause():
	get_tree().paused = true
	$game.hide()
	$pause_menu.show()
	paused = true
	Audio.play_sound("menu", false)
	
func unpause():
	get_tree().paused = false
	$game.show()
	$pause_menu.hide()
	paused = false
	Audio.play_sound("menu", false)
		
func _on_resume_button_pressed():
	get_tree().paused = false
	$game.show()
	$pause_menu.hide()
	paused = false
	Audio.play_sound("menu", false)
		
func _on_restart_button_pressed():
	get_tree().paused = false
	Global.checkpoint = -1
	Global.saved_time = 0
	get_parent().restart_level()
	Audio.play_sound("menu", false)
			
func _on_controlls_button_pressed():
	$pause_menu/controlls_menu/move_right_key.text = TranslationServer.translate("KEY_MOVE_RIGHT") + ": " + Global._input_map.move_right
	$pause_menu/controlls_menu/move_left_key.text = TranslationServer.translate("KEY_MOVE_LEFT") + ": " + Global._input_map.move_left
	$pause_menu/controlls_menu/jump_key.text = TranslationServer.translate("KEY_JUMP") + ": " + Global._input_map.jump
	$pause_menu/main_menu.hide()
	$pause_menu/controlls_menu.show()
	current_menu = "controlls_menu"
	Audio.play_sound("menu", false)
		
func _on_options_button_pressed():
	$pause_menu/main_menu.hide()
	$pause_menu/options_menu.show()
	$pause_menu/options_menu/CheckBox.pressed = Global.show_timer
	current_menu = "options_menu"
	Audio.play_sound("menu", false)
	
func _on_leaderboard_button_pressed():
	$final_screen.hide()
	$leaderboard.get_scores()
	$leaderboard.show()
	current_menu = "leaderboard"
	Audio.play_sound("menu", false)
	
func _on_quit_to_title_button_pressed():
	unpause()
	get_tree().change_scene("res://scenes/main_screen.tscn")
	
func _on_exit_button_pressed():
	get_tree().quit()
	
func _on_start_screen_button_pressed():
	get_tree().change_scene("res://scenes/main_screen.tscn")
	
func reset_controlls_binding():
	get_node("pause_menu/controlls_menu/move_right_key/bind_move_right_key_button").text = TranslationServer.translate("KEY_BIND_KEY")
	get_node("pause_menu/controlls_menu/move_left_key/bind_move_left_key_button").text = TranslationServer.translate("KEY_BIND_KEY")
	get_node("pause_menu/controlls_menu/jump_key/bind_jump_key_button").text = TranslationServer.translate("KEY_BIND_KEY")
	waiting_for_bind = false
	
func _on_cancel_button_pressed():
	$pause_menu/main_menu.show()
	$pause_menu/controlls_menu.hide()
	if waiting_for_bind:
		reset_controlls_binding()
	current_menu = "main_menu"
	Audio.play_sound("menu", false)

func _on_cancel_button_2_pressed():
	$pause_menu/main_menu.show()
	$pause_menu/options_menu.hide()
	current_menu = "main_menu"
	Audio.play_sound("menu", false)

func _on_cancel_button_3_pressed():
	$leaderboard.hide()
	$final_screen.show()
	current_menu = "main_menu"
	Audio.play_sound("menu", false)
	
func _on_bind_key_button_pressed(action):
	if waiting_for_bind:
		reset_controlls_binding()
	current_binding = action
	get_node("pause_menu/controlls_menu/"+ action +"_key/bind_"+ action +"_key_button").text = TranslationServer.translate("KEY_PRESS_ANY_KEY")
	waiting_for_bind = true
	var _event = yield(self, "_pressed")
	if action != current_binding:
		return
	waiting_for_bind = false
	InputMap.action_erase_events(action + "_key")
	InputMap.action_add_event(action + "_key", _event)
	Global._input_map[action] = _event.as_text()
	get_node("pause_menu/controlls_menu/"+action+"_key").text = TranslationServer.translate("KEY_" + action.to_upper()) + ": " + _event.as_text()
	get_node("pause_menu/controlls_menu/"+action+"_key/bind_"+action+"_key_button").text = TranslationServer.translate("KEY_BIND_KEY")
	Audio.play_sound("menu", false)

func _on_sound_level_value_changed(value):
	if value != 0:
		Audio.change_sound_volume(value)
		if Audio.muted.sound:
			Audio.mute_bus(1, false)
	else:
		Audio.mute_bus(1, true)
		
func _on_music_level_value_changed(value):
	if value != 0:
		Audio.change_music_volume(value)
		if Audio.muted.music:
			Audio.mute_bus(2, false)
	else:
		Audio.mute_bus(2, true)
		
func _on_CheckBox_toggled(button_pressed):
	Global.show_timer = button_pressed
	$game/time.visible = Global.show_timer
	Audio.play_sound("menu", false)
	
func show_final_screen():
	finish = true
	Global.level.set_process(false)
	var time = Global.level.time
	var time_passed = Global.convert_time_to_friendly_str(time)
	$game.hide()
	$final_screen/label.text = TranslationServer.translate("KEY_FINAL_TEXT") + ": " +  time_passed
	$final_screen.show()
	
func _on_name_focus_entered():
	$final_screen/name.placeholder_text = ""
		
func load_leaderboards_table(scores):
	$leaderboard/table.text = ""
	for i in scores.size():
		$leaderboard/table.text += str(i+1)+". "+scores[i][0]+" : "+Global.convert_time_to_friendly_str(scores[i][1]) + "\n" 

func _on_save_result_button_pressed():
	if !$final_screen/name.text.empty() and $final_screen/name.text != TranslationServer.translate("KEY_TYPE_NAME"):
		var _name = $final_screen/name.text
		var _score = Global.level.time
		$leaderboard.post_scores(_name, _score)
		$final_screen/save_result_button.disabled = true

func _ready():
	$game/time.visible = Global.show_timer
	$final_screen/name.placeholder_text = TranslationServer.translate("KEY_TYPE_NAME")
