extends HandWalkState

@export var time_to_square := 1.0

var target := Vector2.ZERO

func _enter_state():
	# CASE UP
	if hand_walk.current_direction == hand_walk.CURRENT_DIRECTION.UP:
		if hand_walk.current_path_node.up_connection:
			move_to_target(hand_walk.current_path_node.up_connection)
		elif hand_walk.current_path_node.down_connection:
			hand_walk.current_direction = hand_walk.CURRENT_DIRECTION.DOWN
			move_to_target(hand_walk.current_path_node.down_connection)

	# CASE RIGHT
	elif hand_walk.current_direction == hand_walk.CURRENT_DIRECTION.RIGHT:
		if hand_walk.current_path_node.right_connection:
			move_to_target(hand_walk.current_path_node.right_connection)
		elif hand_walk.current_path_node.left_connection:
			hand_walk.current_direction = hand_walk.CURRENT_DIRECTION.LEFT
			move_to_target(hand_walk.current_path_node.left_connection)
	
	# CASE DOWN
	elif hand_walk.current_direction == hand_walk.CURRENT_DIRECTION.DOWN:
		if hand_walk.current_path_node.down_connection:
			move_to_target(hand_walk.current_path_node.down_connection)
		elif hand_walk.current_path_node.up_connection:
			hand_walk.current_direction = hand_walk.CURRENT_DIRECTION.UP
			move_to_target(hand_walk.current_path_node.up_connection)
	
	# CASE LEFT
	elif hand_walk.current_direction == hand_walk.CURRENT_DIRECTION.LEFT:
		if hand_walk.current_path_node.left_connection:
			move_to_target(hand_walk.current_path_node.left_connection)
		elif hand_walk.current_path_node.right_connection:
			hand_walk.current_direction = hand_walk.CURRENT_DIRECTION.RIGHT
			move_to_target(hand_walk.current_path_node.right_connection)
	
	hand_walk.animation_player.play(GameConstants.ANIM_WALK)

func _exit_state():
	target = Vector2.ZERO

func move_to_target(new_path_node : PathNode):
	target = new_path_node.global_position
	var tween = create_tween().tween_property(hand_walk, "global_position", target, time_to_square)
	await tween.finished
	hand_walk.current_path_node = new_path_node
	hand_walk.state_machine.switch_state($"../HandWalkStateIdle")
