extends Control

@onready var menu_list : Array = [
	{
		"option": %STORY,
		"scene"	:  preload("res://scenes/character_select.tscn")
	},
	{
		"option": %"VS-COM",
		"scene"	:  preload("res://scenes/character_select.tscn")
	},
	{
		"option": %"VS-MAN",
		"scene"	:  preload("res://scenes/multiplayer_setup.tscn")
	},
	{
		"option": %STORY,
		"scene"	:  preload("res://scenes/character_select.tscn")
	},
	{
		"option": %RECORD,
		"scene"	:  preload("res://scenes/character_select.tscn")
	},
	{
		"option": %OPTION,
		"scene"	:  preload("res://scenes/character_select.tscn")
	}
]
@onready var switch_sound : AudioStreamPlayer = %SwitchSound
@onready var select_sound : AudioStreamPlayer = %SelectSound
@onready var menu : Control = %Menu
@onready var start : Label = %START
@onready var target : TextureRect = %Target

var opened : bool = false
var menu_choice : int = 0
var shot_tween : Tween
var glow_tween : Tween

func _ready() -> void:
	shot_tween = create_tween()
	target.scale = Vector2(2,2)
	target.modulate.a = 0
	shot_tween.set_parallel(true)
	shot_tween.tween_property(target, "modulate:a", 1, 1)
	shot_tween.tween_property(target, "scale", Vector2(1,1), 1)
	shot_tween.tween_property(target, "rotation", PI / 4, 1)
	glow_tween = create_tween()
	glow_tween.set_loops(-1)
	glow_tween.tween_property(target, "modulate:a", .8, .5)
	glow_tween.tween_property(target, "modulate:a", 1, .5)

func _process(delta: float) -> void:
	if Input.is_action_just_pressed("ui_menu"):
		opened = !opened
		start.visible = !opened
		menu.visible = opened
		select_sound.play()
		if opened:
			switch_menu(0)
	
	if opened:
		
		var menu_dir = 0
		menu_dir += 1 if Input.is_action_just_pressed("ui_down") else 0
		menu_dir -= 1 if Input.is_action_just_pressed("ui_up") else 0
		if menu_dir != 0:
			switch_sound.play()
			switch_menu(wrapi(menu_choice + menu_dir, 0, len(menu_list)))
		if Input.is_action_just_pressed("ui_accept"):
			Game.switch_scene(menu_list[menu_choice].scene, false)


func switch_menu(new_choice : int) -> void:
	menu_list[menu_choice].option.visible = false
	menu_choice = new_choice
	menu_list[menu_choice].option.visible = true
