extends PanelContainer
class_name CharacterCard
## Character Card Component
##
## Displays a single character's summary information with select/delete actions.

#region Signals
signal selected(character_id: String)
signal delete_requested(character_id: String)
#endregion

#region Node References
@onready var name_label: Label = $MarginContainer/HBoxContainer/InfoVBox/NameLabel
@onready var class_label: Label = $MarginContainer/HBoxContainer/InfoVBox/ClassLabel
@onready var level_label: Label = $MarginContainer/HBoxContainer/InfoVBox/LevelLabel
@onready var gold_label: Label = $MarginContainer/HBoxContainer/InfoVBox/GoldLabel
@onready var location_label: Label = $MarginContainer/HBoxContainer/InfoVBox/LocationLabel
@onready var select_button: Button = $MarginContainer/HBoxContainer/ActionVBox/SelectButton
@onready var delete_button: Button = $MarginContainer/HBoxContainer/ActionVBox/DeleteButton
#endregion

#region Private Variables
var _character_id: String = ""
var _character_data: Dictionary = {}
var _pending_data: Dictionary = {}  # Store data if set before _ready
#endregion

#region Lifecycle
func _ready() -> void:
	#_apply_style()
	select_button.pressed.connect(_on_select_pressed)
	delete_button.pressed.connect(_on_delete_pressed)

	# Apply pending data if set before _ready
	if not _pending_data.is_empty():
		_apply_character_data(_pending_data)
		_pending_data = {}
#endregion

#region Public Methods
func set_character_data(data: Dictionary) -> void:
	"""Set character data and update display."""
	if not is_node_ready():
		# Store for later if called before _ready
		_pending_data = data
		return

	_apply_character_data(data)


func _apply_character_data(data: Dictionary) -> void:
	"""Actually apply the character data to UI elements."""
	_character_data = data
	_character_id = data.get("character_id", "")

	name_label.text = data.get("name", "Unknown")
	class_label.text = data.get("class_name", "Unknown Class")
	level_label.text = "Level %d" % data.get("level", 1)
	gold_label.text = "%d Gold" % data.get("gold", 0)
	location_label.text = _format_location(data.get("current_location", ""))


func get_character_id() -> String:
	return _character_id
#endregion

#region Private Methods
#func _apply_style() -> void:
	#"""Apply card styling."""
	#var style = StyleBoxFlat.new()
	#style.bg_color = ThemeColors.BACKGROUND_CARD
	#style.border_width_all = 1
	#style.border_color = ThemeColors.BORDER_DEFAULT
	#style.corner_radius_all = 8
	#style.content_margin_left = 0
	#style.content_margin_right = 0
	#style.content_margin_top = 0
	#style.content_margin_bottom = 0
	#add_theme_stylebox_override("panel", style)


func _format_location(location_id: String) -> String:
	"""Convert location_id to display name."""
	if location_id.is_empty():
		return "Unknown"
	return location_id.replace("_", " ").capitalize()
#endregion

#region Signal Handlers
func _on_select_pressed() -> void:
	selected.emit(_character_id)


func _on_delete_pressed() -> void:
	delete_requested.emit(_character_id)
#endregion
