extends HBoxContainer

signal cards_selected(selected: Array)
signal hint_requested()
signal play_requested()
signal pass_requested()

var card_nodes: Array = []
var selected_cards: Array = []
const CARD_SCENE := preload("res://src/ui/components/card_node.tscn")

var training_controller: TrainingController = null

func update_hand(hand: Array) -> void:
	for cn in card_nodes:
		cn.queue_free()
	card_nodes.clear()
	selected_cards.clear()
	for c in hand:
		var node: Control = CARD_SCENE.instantiate()
		node.setup(c)
		node.card_clicked.connect(_on_card_clicked)
		node.card_double_clicked.connect(_on_card_double_clicked)
		add_child(node)
		card_nodes.append(node)

func _on_card_clicked(card_node: Node) -> void:
	card_node.set_selected(not card_node.is_selected)
	if card_node.is_selected:
		selected_cards.append(card_node.card_data)
	else:
		selected_cards.erase(card_node.card_data)
	cards_selected.emit(selected_cards)

func _on_card_double_clicked(card_node: Node) -> void:
	if not card_node.is_selected:
		card_node.set_selected(true)
		selected_cards.append(card_node.card_data)
	play_requested.emit()

func _on_hint_pressed() -> void:
	hint_requested.emit()

func _on_play_pressed() -> void:
	play_requested.emit()

func _on_pass_pressed() -> void:
	pass_requested.emit()

func disable_input() -> void:
	for cn in card_nodes:
		cn.mouse_filter = Control.MOUSE_FILTER_IGNORE

func enable_input() -> void:
	for cn in card_nodes:
		cn.mouse_filter = Control.MOUSE_FILTER_STOP

func clear_selection() -> void:
	for cn in card_nodes:
		cn.set_selected(false)
	selected_cards.clear()
