extends CharacterBody3D

const SPEED = 3.0
const RUN_SPEED = 6.0
const JUMP_VELOCITY = 4.5

@onready var camera_point = $camera_point

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

@onready var animation_player = $Visuals/player/AnimationPlayer
@onready var visuals = $Visuals

var walking = false
var running = false

func _ready():
	animation_player.set_blend_time("idle", "walk", 0.2)
	animation_player.set_blend_time("walk", "idle", 0.2)
	animation_player.set_blend_time("idle", "run", 0.2)
	animation_player.set_blend_time("run", "idle", 0.2)
	animation_player.set_blend_time("walk", "run", 0.2)
	animation_player.set_blend_time("run", "walk", 0.2)
	animation_player.set_blend_time("idle", "mma kick", 0.2)
	animation_player.set_blend_time("walk", "mma kick", 0.2)
	animation_player.set_blend_time("run", "mma kick", 0.2)
	animation_player.set_blend_time("mma kick", "idle", 0.2)
	animation_player.set_blend_time("mma kick", "walk", 0.2)
	animation_player.set_blend_time("mma kick", "run", 0.2)

	# Optionally, sync animations with physics process for smoother transitions
	animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_PHYSICS

var _was_on_floor_last_frame = false
var _snapped_to_stairs_last_frame = false

func _snap_down_to_stairs_check():
	var did_snap = false
	
	if not is_on_floor() and velocity.y <= 0 and (_was_on_floor_last_frame or _snapped_to_stairs_last_frame) and $StairsBelowRayCast3D.is_colliding():
		var body_test_params = PhysicsTestMotionParameters3D.new()
		var body_test_result = PhysicsTestMotionResult3D.new()
		
		var max_step_down = -0.5
		
		body_test_params.from = self.global_transform
		body_test_params.motion = Vector3(0, max_step_down, 0)
		
		if PhysicsServer3D.body_test_motion(self.get_rid(), body_test_params, body_test_result):
			var translate_y = body_test_result.get_travel().y
			self.position.y += translate_y
			apply_floor_snap()
			did_snap = true
	
	_snapped_to_stairs_last_frame = did_snap
	_was_on_floor_last_frame = is_on_floor()

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	var input_dir = Input.get_vector("left", "right", "forward", "backward")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

	# Check for running
	if Input.is_action_pressed("run"):
		running = true
	else:
		running = false

	if direction:
		if running:
			velocity.x = direction.x * RUN_SPEED
			velocity.z = direction.z * RUN_SPEED
		else:
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED

		visuals.look_at(direction + position)

		if !walking:
			walking = true
			if running:
				animation_player.play("run")
			else:
				animation_player.play("walk")
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

		if walking:
			walking = false
			animation_player.play("idle")

	# Check for "mma kick" animation
	if Input.is_action_just_pressed("mma kick"):
		animation_player.play("mma kick")
		animation_player.queue("") # Clear any queued animations

	move_and_slide()
	_snap_down_to_stairs_check()  i did logifor s player to go down stairs but now how do i go up