class_name Player extends CharacterBody2D
@onready var playerGraphic: AnimatedSprite2D = $AnimatedSprite2D
@onready var audio_stream_player: AudioStreamPlayer = $AudioStreamPlayer
const GRASS_RUNNING = preload("res://assets/audio/footsteps/Grass Running.wav")

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const PUSH_FORCE = 500

var faceLeft = false
var pushRightEnabled = false
var pushLeftEnabled = false
var pushTarget
var isJumping = false

signal playerDeadSignal

@onready var right_cast: RayCast2D = $rightCast
@onready var left_cast: RayCast2D = $leftCast
@onready var right_target: Node2D = $rightTarget
@onready var left_target: Node2D = $leftTarget

var animPlaying = "idle"
var living = true


func _physics_process(delta: float) -> void:
	if living and not animPlaying == "hurt":
		# Add the gravity.
		if not is_on_floor():
			velocity += get_gravity() * delta
		else:
			isJumping = false
			

		# Handle jump.
		if Input.is_action_just_pressed("ui_accept") and is_on_floor():
			velocity.y = JUMP_VELOCITY
			isJumping = true
			playerGraphic.play("jump")
			
		# Get the input direction and handle the movement/deceleration.
		# As good practice, you should replace UI actions with custom gameplay actions.
		var direction := Input.get_axis("left", "right")
		if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
			pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE)
			pushRightEnabled = false
		if Input.is_action_just_pressed("shove") && pushLeftEnabled && faceLeft == true:
			pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE)
			pushLeftEnabled = false
		# shoot attack
		if Input.is_action_just_pressed("shoot"):
			if faceLeft == false:
				%SceneManager.makeBullet(right_target.global_transform, 700)
			if faceLeft == true:
				%SceneManager.makeBullet(left_target.global_transform, -700)
		
		
		if direction:
			velocity.x = direction * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)
		if direction:
			if not isJumping:
				playerGraphic.play("run")
				if not audio_stream_player.playing:
					audio_stream_player.stream=GRASS_RUNNING
					audio_stream_player.playing=true
		if direction ==0:
			if not isJumping:
				playerGraphic.play("idle")
			
		if direction <0:
			faceLeft = true
			playerGraphic.flip_h = true
		if direction >0:
			faceLeft = false
			playerGraphic.flip_h = false
			

		move_and_slide()
		if right_cast.is_colliding():
			var collider = right_cast.get_collider()
			if collider is Node:
				if collider is RigidBody2D:
					#record that we can shove right
					pushRightEnabled = true
					#record what object to shove
					pushTarget = collider
		if left_cast.is_colliding():
			var collider = left_cast.get_collider()
			if collider is Node:
				if collider is RigidBody2D:
					pushLeftEnabled = true
					pushTarget = collider
					
		for i in get_slide_collision_count():
			var c = get_slide_collision(i)
			if c.get_collider() is RigidBody2D:
				c.get_collider().apply_central_impulse(-c.get_normal() * 50)
				
func hurtPlayer(health):
	animPlaying = "hurt"
	playerGraphic.play(animPlaying)
func killPlayer():
	animPlaying = "death"
	living = false
	playerGraphic.play(animPlaying)

func _on_animation_finished() -> void:
	match animPlaying:
		"hurt":
			animPlaying="idle"
		"death":
			#emit death complete signal
			playerDeadSignal.emit()
