extends Control

export var normal_label_color = Color()
export var hover_label_color = Color()

var ready = false

func play_music():
	$music.play()
	
func show_hint():
	$press_anykey_hint/anim.play("hint")
	ready = true

func _input(event):
	if !ready:
		return
	if event is InputEventKey or event is InputEventMouseButton:
		if !event.pressed:
			$press_anykey_hint.visible = false
			$menu.visible = true
			ready = false

func _on_exit_button_pressed():
	get_tree().quit()

func _on_play_button_pressed():
	$fader/anim.play("fade_in")
	yield($fader/anim, "animation_finished")
	global.chapter = 1
	get_tree().change_scene("res://scenes/transition_screen.tscn")
	
func _on_chapters_button_pressed():
	$chapters_select_menu.show()
	$menu.hide()
	$chapters_select_menu/cancel.modulate = normal_label_color
	
func _on_options_button_pressed():
	$options.show()
	$menu.hide()
	$options/cancel.modulate = normal_label_color

func _on_cancel_button_pressed():
	$options.hide()
	$menu.show()
	$menu/chapters.modulate = normal_label_color
	$menu/play.modulate = normal_label_color
	$menu/options.modulate = normal_label_color
	$menu/exit.modulate = normal_label_color
	
func _on_cancel_2_button_pressed():
	$chapters_select_menu.hide()
	$menu.show()
	$menu/chapters.modulate = normal_label_color
	$menu/play.modulate = normal_label_color
	$menu/options.modulate = normal_label_color
	$menu/exit.modulate = normal_label_color

func _on_sound_control_pressed():
	var mpos_x = get_global_mouse_position().snapped(Vector2(3,3)).x
	$options/sound/tex.rect_size.x = mpos_x - 1
	print(mpos_x)
	if mpos_x == 3:
		AudioServer.set_bus_mute(0, true)
	else:
		AudioServer.set_bus_mute(0, false)
		AudioServer.set_bus_volume_db(0, range_lerp(mpos_x, 3, 30, -40, 0 ))
		
func _on_one_button_pressed():
	$fader/anim.play("fade_in")
	yield($fader/anim, "animation_finished")
	global.chapter = 1
	get_tree().change_scene("res://scenes/level_1.tscn")
	
func _on_two_button_pressed():
	$fader/anim.play("fade_in")
	yield($fader/anim, "animation_finished")
	global.chapter = 2
	get_tree().change_scene("res://scenes/level_2.tscn")
	
func _on_play_button_mouse_entered():
	$menu/play.modulate = hover_label_color
	$menu_sound.play()

func _on_chapters_button_mouse_exited():
	$menu/chapters.modulate = normal_label_color
	
func _on_chapters_button_mouse_entered():
	$menu/chapters.modulate = hover_label_color
	$menu_sound.play()

func _on_play_button_mouse_exited():
	$menu/play.modulate = normal_label_color

func _on_options_button_mouse_entered():
	$menu/options.modulate = hover_label_color
	$menu_sound.play()

func _on_options_button_mouse_exited():
	$menu/options.modulate = normal_label_color

func _on_exit_button_mouse_entered():
	$menu/exit.modulate = hover_label_color
	$menu_sound.play()

func _on_exit_button_mouse_exited():
	$menu/exit.modulate = normal_label_color
	
func _on_one_button_mouse_entered():
	$chapters_select_menu/one.modulate = hover_label_color
	$menu_sound.play()

func _on_one_button_mouse_exited():
	$chapters_select_menu/one.modulate = normal_label_color
	
func _on_two_button_mouse_entered():
	$chapters_select_menu/two.modulate = hover_label_color
	$menu_sound.play()

func _on_two_button_mouse_exited():
	$chapters_select_menu/two.modulate = normal_label_color
	
func _on_cancel_button_mouse_entered():
	$options/cancel.modulate = hover_label_color
	$menu_sound.play()

func _on_cancel_button_mouse_exited():
	$options/cancel.modulate = normal_label_color
	
func _on_cancel_2_button_mouse_entered():
	$chapters_select_menu/cancel.modulate = hover_label_color
	$menu_sound.play()

func _on_cancel_2_button_mouse_exited():
	$chapters_select_menu/cancel.modulate = normal_label_color
	
func _ready():
	print(global.chapter)
	yield(get_tree().create_timer(0.2),"timeout")
	$fader/anim.play("fade_out")
	yield($fader/anim,"animation_finished")
	$logo/anim.play("logo_enter")
