class_name StateAnimatedActor
extends State
## Animation and movement state class
##
## A state intended to control the movement and animation
## functions of a KinimaticBody2D node. Initialized with a
## player, movement node, and kinematicbody2d 
## I think if we also had modifier states that transfered along with the enter and entrance of nodes, that would be helpful.
##
## @WIP

# export (NodePath) var jump_node

# onready var jump_state: State = get_node(jump_node)

## Deprecated
# export var animation_name: String

export var move_speed: float = 60

export(Array, String) var animation_sequence
# this just wouldn't work well. Maybe I can try a resource 
# export(Array, NodePath) var transition_state_nodes

#TODO: I think these two variables accidentally serve the same purpose
# I think one was intended to be a string of the current animation name. 
var animation_index: int = 0
var current_animation_sequence: int = 0
var animation_suffix: String = '';


var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")

var animations: AnimatedSprite
var move_component: Node
var parent: KinematicBody2D

#var animation_sequence_timer: Timer



func _ready():
	# Only add timout node if timer value was specified.
	if(timeout_seconds > 0.0):
		state_timeout = Timer.new()
		state_timeout.wait_time = timeout_seconds
		state_timeout.one_shot = true
		state_timeout.autostart = false
		add_child(state_timeout)


func enter() -> void:
#	animations.connect("animation_finished", self, "_on_AnimatedSprite_animation_finished")
	# Reset animation suffix in case there isn't one
	animation_suffix = ''
	print(parent.name, " entering State: ", self.name)
	#modifier_stack_ref = state_modifiers
	if modifier_stack_ref.empty() == false:
		if modifier_stack_ref[-1].animation_name != '':
			if modifier_stack_ref[-1].animation_suffix:
				if animation_sequence.size() > 0:
					animation_index = 0
					current_animation_sequence = 0
					# Guess I don't need this one anymore
					#if animations.frames.get_animation_names().has(animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name): 
					if animations.frames.has_animation(animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name):
						animation_suffix = modifier_stack_ref[-1].animation_name
			else:
				animations.play(modifier_stack_ref[-1].animation_name)
				return
	if animation_sequence.size() > 0:
		animation_index = 0
		current_animation_sequence = 0
		animations.play(animation_sequence[animation_index] + animation_suffix)
		return

func exit() -> void:
#	animations.disconnect("animation_finished", self, "_on_AnimatedSprite_animation_finished")
	pass

func process_input(_event: InputEvent) -> State:
	return null

func process_frame(_delta: float) -> State:
	if animation_sequence.size() > 0 and current_animation_sequence != animation_index:
		if animation_index >= animation_sequence.size():
			animation_index = 0
		##TODO: Add a safety check here. 
		animations.play(animation_sequence[animation_index] + animation_suffix)
		print("An animation sequence: ", animations.animation , animation_index)
		current_animation_sequence = animation_index
	return null

func process_physics(_delta: float) -> State:
	return null

#func _on_AnimatedSprite_animation_finished():
#	if animations.frames.get_animation_loop(animations.animation) == false:
#		animation_index += 1
