# ClassSelectionMenu.gd
# Menü zur Auswahl der Charakterklasse beim Spielstart
extends CanvasLayer

signal class_selected(character_class: CharacterClass)

const WARRIOR_CLASS = preload("res://classes/warrior.tres")
const ROGUE_CLASS = preload("res://classes/rogue.tres")
const MAGE_CLASS = preload("res://classes/mage.tres")

var selected_class: CharacterClass = null

@onready var panel = $Panel
@onready var warrior_btn = $Panel/VBoxContainer/ClassButtons/WarriorBtn
@onready var rogue_btn = $Panel/VBoxContainer/ClassButtons/RogueBtn
@onready var mage_btn = $Panel/VBoxContainer/ClassButtons/MageBtn
@onready var start_btn = $Panel/VBoxContainer/StartBtn
@onready var class_info = $Panel/VBoxContainer/ClassInfo
@onready var stats_label = $Panel/VBoxContainer/StatsLabel

func _ready():
	# Spiel pausieren während Menü offen
	get_tree().paused = true
	process_mode = Node.PROCESS_MODE_ALWAYS

	# Buttons verbinden
	warrior_btn.pressed.connect(_on_warrior_selected)
	rogue_btn.pressed.connect(_on_rogue_selected)
	mage_btn.pressed.connect(_on_mage_selected)
	start_btn.pressed.connect(_on_start_pressed)

	# Start-Button deaktiviert bis Klasse gewählt
	start_btn.disabled = true
	class_info.text = "Wähle eine Klasse!"
	stats_label.text = ""

func _on_warrior_selected():
	selected_class = WARRIOR_CLASS
	_update_selection("Krieger", WARRIOR_CLASS)
	_highlight_button(warrior_btn)

func _on_rogue_selected():
	selected_class = ROGUE_CLASS
	_update_selection("Schurke", ROGUE_CLASS)
	_highlight_button(rogue_btn)

func _on_mage_selected():
	selected_class = MAGE_CLASS
	_update_selection("Magier", MAGE_CLASS)
	_highlight_button(mage_btn)

func _update_selection(cls_name: String, char_class: CharacterClass):
	var main_stat_name = ""
	match char_class.main_stat:
		CharacterClass.MainStat.STRENGTH:
			main_stat_name = "Stärke"
		CharacterClass.MainStat.AGILITY:
			main_stat_name = "Beweglichkeit"
		CharacterClass.MainStat.INTELLIGENCE:
			main_stat_name = "Intelligenz"

	class_info.text = cls_name + " - Haupt-Stat: " + main_stat_name
	stats_label.text = "STR: " + str(char_class.base_strength) + "  AGI: " + str(char_class.base_agility) + "  INT: " + str(char_class.base_intelligence) + "  STA: " + str(char_class.base_stamina)
	start_btn.disabled = false

func _highlight_button(active_btn: Button):
	# Alle Buttons zurücksetzen
	warrior_btn.modulate = Color(1, 1, 1)
	rogue_btn.modulate = Color(1, 1, 1)
	mage_btn.modulate = Color(1, 1, 1)
	# Aktiven Button hervorheben
	active_btn.modulate = Color(1, 0.8, 0.2)

func _on_start_pressed():
	if selected_class == null:
		return

	# Signal senden und Menü schließen
	class_selected.emit(selected_class)
	get_tree().paused = false
	queue_free()
