extends CharacterBody2D
var input_vector = Vector2.ZERO
var roll_vector = Vector2.DOWN
var stats = PlayerStats

const 	PlayerHurtSound = preload("res://Player/PlayerHurtSound.tscn")

const ACCELERATION = 500
const MAX_SPEED = 80
const ROLL_SPEED = 120
const FRICTION = 500

enum {
	MOVE,
	ROLL,
	ATTACK
}

var state = MOVE

@onready var animationTree = get_node("AnimationTree")
@onready var blinkAnimationPlayer = get_node("BlinkAnimationPlayer")
@onready var animationPlayer = get_node("AnimationPlayer")
@onready var animationState = animationTree.get("parameters/playback")
@onready var swordHitbox = get_node("HitboxPivot/SwordHitbox")
@onready var hurtBox = get_node("Hurtbox")


func _ready():
	stats.connect("no_health", Callable(self, "queue_free"))
	animationTree.active = true
	swordHitbox.knockback_vector = roll_vector
	blinkAnimationPlayer.play("Stop")

func _process(delta):
	match state:
		MOVE:
			move_state(delta)
			
		ROLL:
			roll_state()
			
		ATTACK:
			attack_state()

func move_state(delta):
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = input_vector.normalized()
	
	if input_vector != Vector2.ZERO:
		roll_vector = input_vector
		swordHitbox.knockback_vector = input_vector
		animationTree.set("parameters/Idle/blend_position", input_vector)
		animationTree.set("parameters/Run/blend_position", input_vector)
		animationTree.set("parameters/Attack/blend_position", input_vector)
		animationTree.set("parameters/Roll/blend_position", input_vector)
		animationState.travel("Run")
		velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
	else:
		#animationPlayer.play("IdleRight")
		animationState.travel("Idle")
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

	#velocity = move_and_slide(velocity)
	move()
	
	if Input.is_action_just_pressed("roll"):
		state = ROLL
	
	if Input.is_action_just_pressed("attack"):
		state = ATTACK

func roll_animation_finished():
	state = MOVE

func roll_state():
	velocity = roll_vector * ROLL_SPEED
	animationState.travel("Roll")
	move()

func attack_state():
	#velocity = Vector2.ZERO
	#animationTree.set("parameters/Attack/blend_position", input_vector)
	velocity = Vector2.ZERO
	animationState.travel("Attack")
	#velocity = velocity.move_toward(Vector2.ZERO, FRICTION*delta)
	#velocity = move_and_slide(velocity)

func move():
	set_velocity(velocity)
	move_and_slide()
	velocity = velocity
	
func attack_aninimation_finished():
	state = MOVE

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass


func _on_Hurtbox_area_entered(_area):
	stats.health -= 1
	hurtBox.start_invincibility(0.6)
	hurtBox.create_hit_effect()
	var playerHurtSounds = PlayerHurtSound.instantiate()
	get_tree().current_scene.add_child(playerHurtSounds)


func _on_Hurtbox_invincibility_started():
	blinkAnimationPlayer.play("Start")


func _on_Hurtbox_invincibility_ended():
	blinkAnimationPlayer.play("Stop")
