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: StateAnimatedActor = get_node(jump_node)
onready var idle_state: StateAnimatedActor = get_node(idle_node)
onready var fall_state: StateAnimatedActor = get_node(fall_node)
onready var move_state: StateAnimatedActor = get_node(move_node)
#onready var weapon_state_modifier: StateModifier = get_node(draw_weapon_node)


var can_fire = true
signal do_attack()

var draw_weapon_modifier: StateModifier

func _ready():
	state_timeout.connect("timeout", self, "disable_cooldown")
	draw_weapon_modifier = StateModifier.new()
	var mod_timer :Timer = draw_weapon_modifier.ready( "draw_weapon", "_shoot" , draw_weapon_modifier.TYPE.ANIMATION_SUFFIX, 2.0)
	add_child(mod_timer)


func disable_cooldown():
	if debug_state:
		print("Attack state cooldown timeout. Can attack again.")
	state_ready = true

func enter() -> void:
	if modifier:
		if modifier.get_instance_id() == draw_weapon_modifier.get_instance_id():
			#print ("They the same cannot apply modifier: ", draw_weapon_modifier.get_instance_id())
			modifier = null
	.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")

	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")
			if debug_state:
				print("no need to change idle animation")
		"run":
			animations.play("run_shoot")
			animations.frame = previous_state_frame_number
		"jump":
			animations.play("jump_shoot")
			animations.frame = previous_state_frame_number
			if debug_state:
				print("Starting Animation: jump_shoot at frame ", animations.animation ,  animations.frame)
		_:
			print("Warning!: Attack state encountered unhandled prev animation: ", previous_animation_name)
	
	# An example where the modifier is left to alert on a timeout
	#mod_timer.connect("timeout", draw_weapon_modifier, "_on_Timer_timeout")
	# An example where the current node subscribes
	#mod_timer.connect("timeout", self, "_on_Timer_timeout")

	state_timeout.start()
	draw_weapon_modifier.state_timeout.start()
	state_ready = false
	animation_finished = false

func _on_Timer_timeout():
	print("Modifier Timeout from attack! Forcing animation recall")
	#animations.stop()

func process_frame(delta: float) -> State:
	if animation_finished:
		draw_weapon_modifier.state_timeout.start()
		modifier = draw_weapon_modifier
		return idle_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():
		draw_weapon_modifier.state_timeout.start()
		jump_state.modifier = draw_weapon_modifier
		return jump_state
	
	move_actor_as_desired(delta)

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

	if move_component.velocity.x != 0.0:
		draw_weapon_modifier.state_timeout.start()
		move_state.modifier = draw_weapon_modifier
		#draw_weapon_modifier.transfer_owner(move_state)
		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():
		draw_weapon_modifier.state_timeout.start()
		fall_state.modifier = draw_weapon_modifier
		return fall_state

	
	return null

