class_name Interactor extends RayCast3D

@onready var action_label: Label = %ActionLabel

var selected: Interactable:
	get: return selected
	set(val):
		if val == selected: return

		if(selected):
			selected.highlighted = false
			action_label.visible = false

		selected = val

		if(selected):
			selected.highlighted = true
			action_label.text = selected.action_label
			action_label.visible = true

var held_object: Interactable

func _physics_process(_delta: float) -> void:
	if not is_colliding() or held_object:
		selected = null
		return

	var obj = get_collider()
	selected = obj if obj is Interactable else null


func _input(event: InputEvent) -> void:
	if not event.is_action_pressed('interact'): return
	if selected:
		selected.interact(self)
		return

	if held_object:
		drop_object()

func hold_object(obj: Interactable):
	held_object = obj
	obj.process_mode = Node.PROCESS_MODE_DISABLED
	obj.reparent(self)
	#obj.position = Vector3(0,0,-1.2)

func drop_object():
	if not held_object: return

	held_object.reparent(get_tree().root)
	held_object.process_mode = Node.PROCESS_MODE_INHERIT
	held_object = null
