extends Node2D

@export var human: Human
@export var ray : ShapeCast2D
var can_interact_with : Node2D
@onready var world: World = get_parent().get_parent();
@onready var pathFinder : HumanPathFinder = $"../PathFinder"

var possible_interactables : Array[Node2D]

func _unhandled_input(event: InputEvent) -> void:
	human.stop_interaction()
	human.velocityVector = Vector2(0, 0)
	human.wants_to_grab = false
	human.wants_to_interact_with = null
	human.velocityVector = Input.get_vector("move_left", "move_right", "move_up", "move_down")

	if event.is_action_pressed("grab"):
		if can_interact_with:
			human.wants_to_interact_with = can_interact_with
		else:
			human.wants_to_grab = true

	# moving using either touch or mouse button
	if event is InputEventMouseButton or event is InputEventScreenTouch:
		var tile_pos = world.local_to_map(world.to_local(get_global_mouse_position()))
		if event.pressed:
			pathFinder.destination = get_global_mouse_position()

func _process(delta) -> void:
	ray.target_position = human.last_facing_direction * 48

	if human.last_facing_direction.y > 0 :
		ray.target_position = Vector2(0, 48)
		(ray.shape as RectangleShape2D).size = Vector2(50, 20)
	elif human.last_facing_direction.y < 0:
		ray.target_position = Vector2(0, -48)
		(ray.shape as RectangleShape2D).size = Vector2(50, 20)
	elif human.last_facing_direction.x > 0 :
		ray.target_position = Vector2(48, 0)
		(ray.shape as RectangleShape2D).size = Vector2(20, 50)
	else:
		ray.target_position = Vector2(-48, 0)
		(ray.shape as RectangleShape2D).size = Vector2(20, 50)

	# find all the possible interactables
	possible_interactables = []
	if ray.is_colliding():
		var nbCollisions = ray.get_collision_count()
		for n in range(nbCollisions):
			var colider = ray.get_collider(n) as Node2D
			if colider != null and colider != get_parent():
				if (
						colider and colider.get_parent().has_method("start_interaction") and
						colider.get_parent().has_method("enable_interaction_clue") and
						colider.get_parent().has_method("disable_interaction_clue")
					):
					possible_interactables.append(colider.get_parent())
	
	# find the closest interactable
	var distance_to_closest = Vector2(0, 0) 
	var closest: Node2D = null
	for potential_closest in possible_interactables:
		var distance_to_human = abs(potential_closest.global_position - human.global_position)
		
		if distance_to_closest == Vector2(0, 0):
			distance_to_closest = distance_to_human
			closest = potential_closest
		if distance_to_human < distance_to_closest:
			closest = potential_closest
			distance_to_closest = distance_to_human
	
	# if the new closest is different from the one we had before, notify the one we had before that it shouldn't keep its bubble visible
	if can_interact_with != null:
		if can_interact_with != closest:
			can_interact_with.get_children()
			can_interact_with.disable_interaction_clue()
	# don't retrigger enabling the clue if it was already enabled
	if can_interact_with != closest:
		can_interact_with = closest
		if can_interact_with:
			can_interact_with.enable_interaction_clue(human)
