extends Node3D

var lift_already_used : bool = false

@export var end_location : Vector3 = Vector3.ZERO
@export var lift_time : float = 1.0

@onready var button: MeshInstance3D = $Cylinder_001

var player : CharacterBody3D

func finished_climbing() -> void:
	player.SetAllowedInputsAll()

func start_climbing() -> void:
	var player_start_location = player.global_position
	var height_difference = end_location - global_position
	
	var elevator_tween = get_tree().create_tween()
	elevator_tween.set_parallel(true)
	elevator_tween.set_ease(Tween.EASE_IN_OUT)
	elevator_tween.set_trans(Tween.TRANS_CUBIC)
	elevator_tween.tween_property(self, "global_position", end_location, lift_time)
	elevator_tween.tween_property(player, "global_position", player_start_location + height_difference, lift_time)
	elevator_tween.tween_callback(finished_climbing)
	

func _on_area_3d_body_entered(body: Node3D) -> void:
	if lift_already_used:
		return
		
	if is_instance_of(body, CharacterBody3D):
		player = body
		player.SetAllowedInputsMoveCamera()
		
		lift_already_used = true
		
		var button_tween = get_tree().create_tween()
		var button_final_pos = button.global_position + Vector3.DOWN * 0.35
		button_tween.tween_property(button, "global_position", button_final_pos, 0.3)
		button_tween.tween_callback(start_climbing)
		
