extends Node2D
class_name Table

signal seat_clicked(Seat)

@onready var seat_1: Seat = $Seat1
@onready var seat_2: Seat = $Seat2
@onready var seat_3: Seat = $Seat3

@onready var table: Sprite2D = $Table
@onready var table_defeat: Sprite2D = $TableDefeat
@onready var table_victory: Sprite2D = $TableVictory


func _ready() -> void:
	initialize()


func on_seat_clicked(seat: Seat) -> void:
	seat_clicked.emit(seat)


func initialize() -> void:
	table.visible = true
	table_defeat.visible = false
	table_victory.visible = false
	
	seat_1.set_guest(null)
	seat_2.set_guest(null)
	seat_3.set_guest(null)


func compute_score() -> float:
	if seat_1.guest == null and seat_2.guest == null and seat_2.guest == null:
		print("No guests at table %s" % name)
		return 0.0
		
	var score_guest1 = seat_1.guest.compute_score([seat_2.guest, seat_3.guest]) if seat_1.guest != null else 0.0
	var score_guest2 = seat_2.guest.compute_score([seat_1.guest, seat_3.guest]) if seat_2.guest != null else 0.0
	var score_guest3 = seat_3.guest.compute_score([seat_1.guest, seat_2.guest]) if seat_3.guest != null else 0.0
	
	var table_score = score_guest1 + score_guest2 + score_guest3
	
	print(table_score)
	table.visible = false
	table_defeat.visible = table_score < 0
	table_victory.visible = table_score >= 0
	
	return table_score
	
