extends Node3D
class_name PlayerPlaceholder

@export var owner_id: int = 0
@export var initial_position: Vector3 = Vector3(0, 0, 0)
# -- Components
@onready var client_node: CharacterBody3D = $PlayerControlledNode
@onready var server_node: PlayerServerNode = $ServerControlledNode
@export var character_speed: int = 5
@onready var bullet_starting_poing: Node3D = $ServerControlledNode/BulletStartingPoint



# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	set_multiplayer_authority(1)
	client_node.set_multiplayer_authority(owner_id)
	$PlayerControlledNode/CameraMount.set_multiplayer_authority(owner_id)
	switch_players_camera.rpc_id(owner_id)
	server_node.global_position = initial_position
	client_node.global_position = server_node.global_position
	client_node.rotation.y = server_node.rotation.y
	client_node.rotation.x = server_node.rotation.x
	
	if multiplayer.is_server():
		$Timer.start()
	if owner_id != multiplayer.get_unique_id():
		client_node.queue_free()
	else:
		pass
		#client_node._add_legs_to_first_view()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	#if desired_player_state.globa_position != real_player_state.globa_position:
		#desired_player_state.globa_position = real_player_state.globa_position
	
	pass
	
func _physics_process(delta: float) -> void:
	pass
	
@rpc("authority", "call_local", "reliable")
func switch_players_camera():
	$PlayerControlledNode/CameraMount/Camera3D.make_current()
	
func set_server_position(position: Vector3):
	initial_position = position


func _on_Timer_timeout():
	$Timer.start()   


func _on_timer_timeout() -> void:
	if multiplayer.is_server():
		verify_position.rpc_id(owner_id)
		verify_rotation.rpc_id(owner_id)
		$Timer.start()   
		
	pass # Replace with function body.

@rpc("authority", "call_local")
func verify_position():
	var desired_position: Vector3 = client_node.global_position
	send_position.rpc_id(1, desired_position.x, desired_position.y, desired_position.z)

@rpc("any_peer", "call_local")
func send_position(x: float, y: float, z: float):
	var desired_position: Vector3 = Vector3(x, y, z)
	if multiplayer.is_server():
		var real_position: Vector3 = server_node.global_position
		var difference: Vector3 = desired_position - real_position
		if is_vector_a_lower_than_b(difference, Vector3(0.3, 0.3, 0.3)):
			server_node.global_position = desired_position
		else:
			var new_position: Vector3 = desired_position.lerp(real_position, 0.5)
			adjust_position.rpc_id(owner_id, new_position.x, new_position.y, new_position.z)

@rpc("authority", "call_local")
func adjust_position(x: float, y: float, z: float):
	var desired_position: Vector3 = Vector3(x, y, z)
	if owner_id == multiplayer.get_unique_id():
		push_warning("player position is not valid, adjusting")
		client_node.global_position = desired_position
	

@rpc("authority", "call_local")
func verify_rotation():
	var desired_rotation: Vector3 = client_node.rotation
	send_rotation.rpc_id(1, desired_rotation.x, desired_rotation.y, desired_rotation.z)

@rpc("any_peer", "call_local")
func send_rotation(x: float, y: float, z: float):
	var desired_rotation: Vector3 = Vector3(x, y, z)
	if multiplayer.is_server():
		var real_rotation: Vector3 = server_node.rotation
		var difference: Vector3 = desired_rotation - real_rotation
		if is_vector_a_lower_than_b(difference, Vector3(0.3, 0.3, 0.3)):
			server_node.rotation = desired_rotation
		else:
			var new_rotation: Vector3 = desired_rotation.lerp(real_rotation, 0.5)
			adjust_rotation.rpc_id(owner_id, new_rotation.x, new_rotation.y, new_rotation.z)
		
@rpc("any_peer", "call_local")
func adjust_rotation(x: float, y: float, z: float):
	var desired_rotation: Vector3 = Vector3(x, y, z)
	if owner_id == multiplayer.get_unique_id():
		push_warning("player rotation is not valid, adjusting")
		client_node.rotation = desired_rotation

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
