extends StateAnimatedActor

export (NodePath) var jump_node
export (NodePath) var idle_node
export (NodePath) var fall_node
export (NodePath) var move_node
export (NodePath) var draw_weapon_node

onready var jump_state: State = get_node(jump_node)
onready var idle_state: State = get_node(idle_node)
onready var fall_state: State = get_node(fall_node)
onready var move_state: State = get_node(move_node)
onready var weapon_state_modifier: StateModifier = get_node(draw_weapon_node)


var can_fire = true
signal do_attack()

func enter() -> void:
	#.enter()
	##TODO: Turn this to clear only the animation modifiers.
	modifier_stack_ref.clear()
	mod_animation_sequence.clear()
	#mod_animation_sequence = animation_sequence.duplicate(true)
	mod_animation_sequence.append_array(animation_sequence)
	# Reset animation suffix in case there isn't one
	animation_suffix = '_shoot'
	animation_index = 0
	current_animation_sequence = 0
	#var enter_animation = ''
	var enter_frame = 0
	
	if debug_state:
		print(parent.name, " entering State: ", self.name)
	move_component.current_movement_state = self.name
	emit_signal("state_entered")

	state_timeout.start()
	can_fire = true
#	if modifier_stack_ref.has($"../draw_weapon") == false:
#		$"../draw_weapon".enter()
#		modifier_stack_ref.push_front($"../draw_weapon")
	if debug_state:
		print("Previous State Info: ", previous_animation_name, " ",
			 previous_state_frame_number, " ", previous_state_name)
	match previous_animation_name:
		"idle":
			animations.play("idle_shoot")
		"run":
			animations.play("run_shoot")
			animations.frame = previous_state_frame_number
		"jump":
			animations.play("jump_shoot")
			animations.frame = previous_state_frame_number
		_:
			print("Warning!: Attack state encountered unhandled prev animation: ", previous_animation_name)

func process_frame(delta: float) -> State:
#	if animations.frame == 2:
#		animations.stop()
	if state_timeout.time_left == 0 and animations.animation == "shoot":
		return idle_state
	return null


func process_physics(delta: float) -> State:
	
	if can_fire: # trying to do this in the physics because irrecular behavior
		emit_signal("do_attack")
		can_fire = false
		
	if move_component.wants_jump() and parent.is_on_floor():
		return jump_state
	
	move_actor_as_desired(delta)

	if previous_state_name == "move" and move_component.velocity.x == 0.0:
		return idle_state

	if previous_state_name == "idle" and move_component.velocity.x != 0.0:
		return move_state

	if move_component.get_movement_direction() != 0.0:
		parent.transform.x.x = move_component.get_movement_direction()

#	if move_component.velocity.x != 0.0:
#		return move_state
	
	if !parent.is_on_floor():
		return fall_state

	
	return null

func exit() -> void:
	# force timer reset
	weapon_state_modifier.state_timeout.start()
	push_animation_state_modifier(weapon_state_modifier)
	#$"../draw_weapon".state_timeout.start()
	
	return	

