class_name PlayerState extends State

const IDLE = "Idle"
const WALKING = "Walking"

var player: Player


func _ready() -> void:
	await owner.ready
	player = owner as Player
	assert(player != null, "The PlayerState state type must be used only in the player scene. It needs the owner to be a Player node.")


func _enter(_previous_state_path: String, _data: Dictionary[StringName, Variant] = {}) -> void:
	pass


func _exit() -> void:
	pass


func _handle_input(_event: InputEvent) -> void:
	pass


func _state_update(_delta: float) -> void:
	pass


func _state_physics_update(_delta: float) -> void:
	pass


func match_state(exclude: Array[StringName] = []) -> StringName:
	var movement_vector: Vector2 = player.get_movement_vector()
	
	if movement_vector.length() > player.DEADZONE:
		if WALKING in exclude: return ""
		return WALKING
	elif movement_vector.length() < player.DEADZONE:
		if IDLE in exclude: return ""
		return IDLE
	
	return ""


func watch_state(exclude: Array[StringName] = [], data: Dictionary[StringName, Variant] = {}) -> bool:
	var next_state: StringName = match_state(exclude)
	if next_state != "":
		finished.emit(next_state, data)
		return true
	
	return false
