extends Control

signal card_clicked(card_node)
signal card_double_clicked(card_node)

var card_data: Card = null
var is_selected: bool = false

@onready var texture_rect: TextureRect = $TextureRect
@onready var label: Label = $Label

func setup(card: Card) -> void:
	card_data = card
	update_display()

func update_display() -> void:
	if card_data == null:
		return
	var suits := ["S", "H", "C", "D", "SJ", "BJ"]
	var ranks := ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "SJ", "BJ"]
	var suit := card_data.suit()
	var rank := card_data.rank()
	if rank < ranks.size() and suit < suits.size():
		label.text = "%s %s" % [suits[suit], ranks[rank]]
	modulate = Color.WHITE if not is_selected else Color(1.2, 1.2, 0.8)

func set_selected(sel: bool) -> void:
	is_selected = sel
	update_display()

func _on_gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			if event.double_click:
				card_double_clicked.emit(self)
			else:
				card_clicked.emit(self)
