class_name PlayerSetup extends PanelContainer

const DEFAULT_PLAYER_NAME := "Mysterious Gfolfer X"

const DEFAULT_PLAYER_LIFE := 100.0

const DEFAULT_PLAYER_BALLS := {
	GameBall.Type.BASIC: -1,
}

@export var input_method: InputMethod.Type:
	set = _set_input_method

@export var player_name: String:
	set(value):
		if not is_instance_valid(name_input):
			await ready
		name_input.text = value
	get:
		return name_input.text

@export var player_color: Color:
	set(value):
		if not is_instance_valid(color_input):
			await ready
		color_input.color = value
		color_input.color_changed.emit(value)
	get:
		return color_input.color

var character_controller: CharacterController

var selected_character_index := 0:
	set = _set_selected_character_index

@onready var keyboard_symbol: Label = %KeyboardSymbol
@onready var gamepad_symbol: Label = %GamepadSymbol
@onready var sony_symbol: Label = %SonySymbol
@onready var xbox_symbol: Label = %XboxSymbol
@onready var name_input: LineEdit = %Name
@onready var color_input: ColorPickerButton = %Color
@onready var char_name: Label = %CharName
@onready var character_container: Node3D = %CharacterContainer

# TODO genericize for selectable characters


func _ready() -> void:
	selected_character_index = selected_character_index


func _set_input_method(value: InputMethod.Type) -> void:
	if not is_instance_valid(keyboard_symbol):
		await ready
	keyboard_symbol.hide()
	gamepad_symbol.hide()
	sony_symbol.hide()
	xbox_symbol.hide()

	match value:
		InputMethod.Type.GAMEPAD, InputMethod.Type.NINTENDO:
			gamepad_symbol.show()
		InputMethod.Type.SONY:
			sony_symbol.show()
		InputMethod.Type.XBOX:
			xbox_symbol.show()
		_:  # Assume everything else is mouse+keyboard
			keyboard_symbol.show()

	input_method = value


func _set_selected_character_index(value: int) -> void:
	selected_character_index = wrapi(value, 0, len(Character.catalog.characters))

	# Remove any existing character preview
	for child: Node in character_container.get_children():
		child.queue_free()

	# Get new character and instantiate preview
	character_controller = get_selected_character().controller_scene.instantiate()

	character_container.add_child(character_controller)
	character_controller.set_color(player_color)
	char_name.text = get_selected_character().name


func get_selected_character() -> Character:
	return Character.catalog.characters[selected_character_index]


func select_next_character() -> void:
	selected_character_index += 1


func select_previous_character() -> void:
	selected_character_index -= 1


## Build a `WorldPlayer` representing the selections made here.
func build_player() -> WorldPlayer:
	var player := WorldPlayer.new()
	player.life = DEFAULT_PLAYER_LIFE
	player.name = player_name if player_name else DEFAULT_PLAYER_NAME
	player.color = player_color

	# TODO club selection
	player.driver = Club.catalog.debug_driver
	player.iron = Club.catalog.debug_iron
	player.wedge = Club.catalog.debug_wedge
	player.putter = Club.catalog.debug_putter
	player.special = null

	# TODO ball selection maybe?
	player._balls = DEFAULT_PLAYER_BALLS.duplicate(true)

	# TODO set character selection

	# TODO set input method

	return player


func _on_remove_button_pressed() -> void:
	queue_free()


func _on_color_changed(color: Color) -> void:
	if character_controller:
		character_controller.set_color(color)
