extends Node2D
# Race select: hover/click the race buttons to reveal and select. Keyboard (move_left/right, attack) still works.
# Dwarf preselected. Emits race_selected(race_name) when confirmed.

signal race_selected(race_name: String)

const RACES: Array[String] = ["Dwarf", "Elf", "Human"] # Wizard = Human

var selected_index: int = 0 # Dwarf preselected
var hovered_index: int = -1 # -1 = no hover
var _selection_confirmed: bool = false # One-shot: only first click counts (avoids overlapping buttons overwriting)

@onready var dwarf_node: Node2D = $Dwarf
@onready var elf_node: Node2D = $Elf
@onready var wizard_node: Node2D = $Wizard
@onready var button_dwarf: Button = $Control/ButtonDwarf
@onready var button_elf: Button = $Control/ButtonElf
@onready var button_wizard: Button = $Control/ButtonWizard

func _ready() -> void:
	var vp = get_viewport()
	if vp:
		position = vp.get_visible_rect().size / 2.0
	_update_display()
	_connect_buttons()

func _connect_buttons() -> void:
	var buttons := [button_dwarf, button_elf, button_wizard]
	for i in buttons.size():
		var btn: Button = buttons[i]
		if not btn:
			continue
		# Hover: show this race
		btn.mouse_entered.connect(_on_race_button_mouse_entered.bind(i))
		btn.mouse_exited.connect(_on_race_button_mouse_exited)
		# Click: select this race and confirm
		btn.pressed.connect(_on_race_button_pressed.bind(i))

func _on_race_button_mouse_entered(race_index: int) -> void:
	hovered_index = race_index
	_update_display()

func _on_race_button_mouse_exited() -> void:
	hovered_index = -1
	_update_display()

func _on_race_button_pressed(race_index: int) -> void:
	# Use the clicked button's index directly (don't rely on selected_index - another button may have fired too)
	selected_index = race_index
	_confirm_selection_with_index(race_index)

func _input(event: InputEvent) -> void:
	var vp = get_viewport()
	if not vp:
		return
	if event.is_action_pressed("move_left"):
		selected_index = (selected_index - 1 + RACES.size()) % RACES.size()
		hovered_index = -1
		_update_display()
		vp.set_input_as_handled()
	if event.is_action_pressed("move_right"):
		selected_index = (selected_index + 1) % RACES.size()
		hovered_index = -1
		_update_display()
		vp.set_input_as_handled()
	if event.is_action_pressed("attack"):
		# Attack can be mouse1 or keyboard: use hovered race if mouse is over a button, else keyboard selection
		var index_to_confirm := hovered_index if hovered_index >= 0 else selected_index
		_confirm_selection_with_index(index_to_confirm)
		vp.set_input_as_handled()

func _update_display() -> void:
	var show_index: int = hovered_index if hovered_index >= 0 else selected_index
	if dwarf_node:
		dwarf_node.visible = (show_index == 0)
	if elf_node:
		elf_node.visible = (show_index == 1)
	if wizard_node:
		wizard_node.visible = (show_index == 2)

func _confirm_selection_with_index(clicked_index: int) -> void:
	# Only process first confirmation (overlapping buttons can fire multiple signals; first click wins)
	if _selection_confirmed:
		return
	_selection_confirmed = true
	# Use the index from the button that was actually clicked (not selected_index which could be overwritten)
	if clicked_index < 0 or clicked_index >= RACES.size():
		clicked_index = selected_index
	var race := RACES[clicked_index]
	var gs = get_node_or_null("/root/GameState")
	if gs and "selected_race" in gs:
		gs.selected_race = race
		print("SelectClass: joiner clicked index ", clicked_index, " -> race '", race, "', GameState.selected_race = '", gs.selected_race, "'")
	else:
		print("SelectClass: joiner clicked index ", clicked_index, " -> race '", race, "' but GameState not found")
	race_selected.emit(race)
