extends ItemSelector

@export_file("*.tscn") var game_path: String = ""
@export_file("*.tscn") var menu_path: String = ""
@export var animation: AnimationPlayer = null
@export var scroll: ScrollContainer = null
@export var specs: Array[PlayerSpecs] = []
@export_group("Labels")
@export var speed_label: Label = null
@export var accel_label: Label = null
@export var brake_label: Label = null
@export var control_label: Label = null

var _prev_index: int = 0


func _on_item_navigated(index: int) -> void:
	speed_label.text = (
			'■'.repeat(specs[index].max_speed_level) +
			'□'.repeat(3 - specs[index].max_speed_level)
	)
	accel_label.text = (
			'■'.repeat(specs[index].acceleration_level) +
			'□'.repeat(3 - specs[index].acceleration_level)
	)
	brake_label.text = (
			'■'.repeat(specs[index].brake_level) +
			'□'.repeat(3 - specs[index].brake_level)
	)
	control_label.text = (
			'■'.repeat(specs[index].control_level) +
			'□'.repeat(3 - specs[index].control_level)
	)

	if index > _prev_index:
		scroll_down()
	elif index < _prev_index:
		scroll_up()

	_prev_index = index


func _on_item_selected(index: int) -> void:
	control_enabled = false
	Globals.current_player_specs = specs[index]
	Globals.current_track = 1
	Globals.total_time = 0.0
	Globals.raw_score = 0.0
	Globals.player_lives = 3
	await PulseSound0.finished
	get_tree().change_scene_to_file(game_path)


func _on_return() -> void:
	animation.play(&"fade_in")
	await animation.animation_finished
	get_tree().change_scene_to_file(menu_path)


func scroll_down() -> void:
	for i in range(5):
		scroll.scroll_vertical += 8
		await get_tree().create_timer(0.04).timeout


func scroll_up() -> void:
	for i in range(5):
		scroll.scroll_vertical -= 8
		await get_tree().create_timer(0.04).timeout
