extends NodeState

@export var player: Player
@export var animatedSprite2D: AnimatedSprite2D
@export var wallCheck: ShapeCast2D
@export var floorCheck: RayCast2D
@export var ledgeGrabBox: CollisionShape2D
@export var fallState: NodeState


const jumpGravity = player.GRAVITY
@onready var jumpHight = player.jumpHight
@onready var maxJumpCount = player.maxJumpCount
@onready var speed = player.speed
@onready var maxSpeed = player.maxAirSpeed
@onready var friction = player.airFriction
@onready var wallPushBack = player.wallPushBack

var currentJumpCount: int = 0
var wallJumpCoyoteTime: float = 0.3
var canWallJump: bool = false

@warning_ignore("unused_parameter")
func _physics_process(delta: float) -> void:
	await player.move
	if player.is_on_floor():
		currentJumpCount = 0

@warning_ignore("unused_parameter")
func on_process(delta: float) -> void:
	pass

func on_phyisics_process(delta: float) -> void:
	
	var direction: float = GameInputEvents.movement_input()
	
	player.velocity.y += jumpGravity * delta
	if player.is_on_floor():
		currentJumpCount = 0
		player.velocity.y = jumpHight
		fallState.coyoteJump = false
		currentJumpCount += 1
	
	if fallState.coyoteJump and currentJumpCount < maxJumpCount:
		player.velocity.y = jumpHight
		fallState.coyoteJump = false
		currentJumpCount += 1
	
	#multi jumps
	multiJump()
	
	
	#movment shit under here
	
	
	if direction:
		player.velocity.x += direction * speed * delta
		player.velocity.x = move_toward(player.velocity.x, clamp(player.velocity.x, -maxSpeed, maxSpeed), friction * delta)
	
	if direction != 0:
		animatedSprite2D.flip_h = false if direction > 0 else true
	
	if direction == 0:
		player.velocity.x = move_toward(player.velocity.x, 0, friction * delta)
	
	await player.move
	
	transition_states()

func enter() -> void:
	ledgeGrabBox.disabled = false
	animatedSprite2D.play("Jump")
	if not fallState.coyoteJump and not player.is_on_wall():
		currentJumpCount += 1
	multiJump()

func exit() -> void:
	ledgeGrabBox.disabled = true
	fallState.coyoteJump = false
	animatedSprite2D.stop()

func transition_states() -> void:
	#transition states
	
	#idle state
	if player.is_on_floor():
		transition.emit("idle")
	
	#fall state
	if player.velocity.y > 0:
		transition.emit("fall")
	
	if wallCheck.is_colliding() and not floorCheck.is_colliding() and player.velocity.y == 0 and player.is_on_floor():
		transition.emit("Grab")
	
	if GameInputEvents.dash_input() and player.canDash:
		transition.emit("dash")
	

func multiJump():
	if !player.is_on_floor() and currentJumpCount < maxJumpCount and Input.is_action_just_pressed("move_jump") and not player.is_on_wall() and not fallState.coyoteJump:
		player.velocity.y = jumpHight
		currentJumpCount += 1
	
	if player.is_on_wall():
		canWallJump = true
		wallJumpCoyote()
	
	if Input.is_action_just_pressed("move_jump") and canWallJump:
		var boost = 150
		player.velocity.y = jumpHight
		var wallDirection = player.get_wall_normal().x
		var direction: float = GameInputEvents.movement_input()
		if direction == wallDirection:
			player.velocity.x =+ (wallPushBack + boost) * wallDirection
		else:
			player.velocity.x =+ wallPushBack * wallDirection
		currentJumpCount = 0

func wallJumpCoyote():
	await get_tree().create_timer(wallJumpCoyoteTime).timeout
	canWallJump = false
