@tool
extends Area2D
class_name Seat

signal seat_clicked(Seat)

@export var guest: MaskedChara:
	set(new_resource):
		guest = new_resource
		_on_chara_resource_changed()
		
@onready var character: Character = $Character
@onready var empty: Sprite2D = $Empty


func _process(delta: float) -> void:
	character.scale.x = 0.15
	character.scale.y = 0.15
	character.position.y = -135


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	guest = null
	character.visible = false
	empty.visible = true
	
	
func _on_chara_resource_changed() -> void:
	if guest == null:
		return
	
	if character != null:
		empty.visible = false
		character.visible = true
		character.chara_resource = guest.character
		character.mask_eyes.mask_resource = guest.face_mask
		character.mask_mouth.mask_resource = guest.mouth_mask


func set_guest(p_guest: MaskedChara) -> void:
	if p_guest == null:
		return
		
	guest = p_guest
	empty.visible = false
	character.visible = true
	character.chara_resource = guest.character
	character.mask_eyes.mask_resource = guest.face_mask
	character.mask_mouth.mask_resource = guest.mouth_mask

func _on_mouse_entered() -> void:
	if guest != null:
		return
	Input.set_default_cursor_shape(Input.CursorShape.CURSOR_POINTING_HAND)


func _on_mouse_exited() -> void:
	if guest != null:
		return
	Input.set_default_cursor_shape(Input.CursorShape.CURSOR_ARROW)


func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event is InputEventMouseButton and event.is_pressed() and guest == null:
		Input.set_default_cursor_shape(Input.CursorShape.CURSOR_ARROW)
		seat_clicked.emit(self)
