extends CharacterBody3D
class_name CharacterWrapper

@export var die_script: GDScript = null
@export var owner_placeholder: CharacterBody3D = null

@export var interpolation_speed: float = 50.0  # How quickly the model corrects position
@export var velocity_influence: float = 1.0  # How much velocity is used for smoothing
@export var snap_threshold: float = 2.0  # If desync is larger than this, snap faster

@export var interpolation_delay: float = 0.1  # Stay 100ms behind for smooth interpolation

var position_buffer = []  # Stores past positions (tuples of (timestamp, position))
var previous_position: Vector3 = Vector3.ZERO
var pseudo_velocity: Vector3 = Vector3.ZERO  # Approximate velocity without CharacterBody3D

var previous_server_pos: Vector3 = Vector3.ZERO

func _process(delta):
	# Record the latest server position with timestamp
	
	global_rotation = owner_placeholder.global_rotation
	# -- This code is 100% generated by chatGPT and I can't understand it fully yet
	
	var position_difference := global_position - owner_placeholder.global_position
	if is_vector_a_lower_than_b(position_difference, Vector3(0.3, 0.3, 0.3)) and \
		is_vector_a_lower_than_b(Vector3(-0.3, -0.3, -0.3), position_difference):
		var server_pos = owner_placeholder.global_transform.origin
		pseudo_velocity = (server_pos - previous_position) / delta
		previous_position = server_pos  # Store for next frame
		position_buffer.append([Time.get_ticks_msec() / 1000.0, server_pos])
		
		
		while position_buffer.size() > 2 and position_buffer[1][0] < (Time.get_ticks_msec() / 1000.0) - interpolation_delay:
			position_buffer.pop_front()
		
		if position_buffer.size() >= 2:
			var t_now = (Time.get_ticks_msec() / 1000.0) - interpolation_delay
			var prev_point = position_buffer[0]
			var next_point = position_buffer[1]

			var alpha = (t_now - prev_point[0]) / (next_point[0] - prev_point[0])
			alpha = clamp(alpha, 0.0, 1.0)
			global_transform.origin = prev_point[1].lerp(next_point[1], alpha)
	else:
		global_position = owner_placeholder.global_position

func _ready() -> void:
	set_multiplayer_authority(multiplayer.get_unique_id())
	global_position = owner_placeholder.global_position
	
#func _physics_process(delta: float) -> void:
	##if not is_on_floor():
		##velocity += get_gravity() * delta
	##global_position = owner_placeholder.global_position
		#
	#var server_pos = owner_placeholder.global_transform.origin
	#var server_velocity = (server_pos - previous_server_pos) / delta
	#
	#if server_velocity.length() < 0.1:
		#velocity = Vector3.ZERO  # Stop predicting movement when nearly still
	#var distance_to_server = global_transform.origin.distance_to(server_pos)
	#if distance_to_server > snap_threshold:
		## If the desync is too large, quickly correct it
		#global_transform.origin = server_pos
	#else:
		## Otherwise, smoothly adjust position
		#global_transform.origin = global_transform.origin.lerp(server_pos, interpolation_speed * delta)
	#
	#global_transform.origin += velocity * velocity_influence * delta
	## Update stored velocity
	#velocity = velocity.lerp(server_velocity, delta * interpolation_speed)
	#previous_server_pos = server_pos
	##velocity = owner_placeholder.velocity
	##global_transform.origin = global_transform.origin.lerp(server_pos, interpolation_speed * delta)
	##global_transform.origin += velocity * velocity_influence * delta
	##velocity = velocity.lerp(server_velocity, delta * interpolation_speed)
	#move_and_slide()

# Set the owner placeholder, so the characters can send the requests to a node
# it depends on
func set_owner_placeholder(owner_placeholder: Node3D):
	owner_placeholder = owner_placeholder
	
func die():
	push_warning("TODO: Implement ragdoll kind of dying and respawn character as an object")
	queue_free()


func _on_area_body_part_hit(damage: int) -> void:
	# The owner node should be aware of how to take damage, so we need to
	# pass the value.
	if owner_placeholder:
		if owner_placeholder.has_method("take_damage"):
			owner_placeholder.take_damage(damage)
		else:
			push_warning("Node doesn't know how to take the damage")

func is_vector_a_lower_than_b(vec_a: Vector3, vec_b: Vector3) -> bool:
	return vec_a.x < vec_b.x and vec_a.y < vec_b.y and vec_a.z < vec_b.z
