extends Area2D

const STATES: Array[CompressedTexture2D] = [
	preload("res://assets/textures/survivor_slot.png"),
	preload("res://assets/textures/survivor_slot_highlighted.png"),
]

@onready var sprite: Sprite2D = $Sprite2D

var highlighted: bool = false:
	set(new_value):
		highlighted = new_value
		if new_value:
			sprite.texture = STATES[1]
		else:
			sprite.texture = STATES[0]

func is_occupied():
	return get_children().any(func (child): return child.is_in_group("survivor"))

func drop(object: Node2D):
	object.reparent(self)

func _on_mouse_entered():
	var da = GlobalInput.dragged_object
	if da == null:
		return
	
	var object_is_survivor = da.is_in_group("survivor")
	var spot_is_free = not is_occupied()
	var spot_is_not_source = not is_ancestor_of(da)
	
	if object_is_survivor && spot_is_free && spot_is_not_source:
		highlighted = true
		GlobalInput.set_drop_spot(self)

func _on_mouse_exited():
	if GlobalInput.drop_spot == self:
		highlighted = false
		GlobalInput.set_drop_spot(null)
