extends Node2D

signal back_title

var menu_item = 0
var menu_selection
var orig_position = 142

var option1 = false
var option2 = false
var option3 = false

func activate():
	menu_item = 0
	$Selection.rect_position = Vector2($Selection.rect_position.x, orig_position + (170 * menu_item))
	input_connect()
	
	if MemoryCard.get_config("fullscreen"):
		$Option1/Selection.rect_position = Vector2(629 + 250, $Option1/Selection.rect_position.y)
		option1 = true
	
	if not MemoryCard.get_config("music"):
		$Option2/Selection.rect_position = Vector2(629 + 250, $Option2/Selection.rect_position.y)
		option2 = true
	
	if not MemoryCard.get_config("sfx"):
		$Option3/Selection.rect_position = Vector2(629 + 250, $Option3/Selection.rect_position.y)
		option3 = true

func exit():
	input_disconnect()
	emit_signal("back_title")


func input_connect():
	# warning-ignore:return_value_discarded
	Controller.connect("controller_right", self, "on_right")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_left", self, "on_left")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_up", self, "on_up")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_down", self, "on_down")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.connect("controller_escape", self, "exit")

func input_disconnect():
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_right", self, "on_right")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_left", self, "on_left")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_up", self, "on_up")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_down", self, "on_down")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_enter", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_jump", self, "on_enter")
	# warning-ignore:return_value_discarded
	Controller.disconnect("controller_escape", self, "exit")

func on_up():
	menu_item = menu_item - 1
	if menu_item < 0: menu_item = 0
	$Selection.rect_position = Vector2($Selection.rect_position.x, orig_position + (70 * menu_item))
	Audio.play_sfx("menu_move")
	
func on_down():
	menu_item = menu_item + 1
	if menu_item > 2: menu_item = 2
	$Selection.rect_position = Vector2($Selection.rect_position.x, orig_position + (70 * menu_item))
	Audio.play_sfx("menu_move")
	
func on_right():
	if menu_item == 0:
		option1 = true
		$Option1/Selection.rect_position = Vector2(629 + 250, $Option1/Selection.rect_position.y)
		MemoryCard.set_config("fullscreen", true)
		OS.window_fullscreen = true
	if menu_item == 1:
		option2 = true
		$Option2/Selection.rect_position = Vector2(629 + 250, $Option2/Selection.rect_position.y)
		MemoryCard.set_config("music", false)
		Audio.mute_ost = true
		Audio.volume(0)
	if menu_item == 2:
		option3 = true
		$Option3/Selection.rect_position = Vector2(629 + 250, $Option3/Selection.rect_position.y)
		MemoryCard.set_config("sfx", false)
		Audio.mute_sfx = true
	Audio.play_sfx("menu_move")

	
func on_left():
	if menu_item == 0:
		option1 = false
		$Option1/Selection.rect_position = Vector2(629, $Option1/Selection.rect_position.y)
		MemoryCard.set_config("fullscreen", false)
		OS.window_fullscreen = false
	if menu_item == 1:
		option2 = false
		$Option2/Selection.rect_position = Vector2(629, $Option2/Selection.rect_position.y)
		MemoryCard.set_config("music", true)
		Audio.mute_ost = false
		Audio.volume(80)
	if menu_item == 2:
		option3 = false
		$Option3/Selection.rect_position = Vector2(629, $Option3/Selection.rect_position.y)
		MemoryCard.set_config("sfx", true)
		Audio.mute_sfx = false
	Audio.play_sfx("menu_move")
	
func on_enter():
	pass
