class_name Actor 
extends KinematicBody2D

#var velocity = Vector2(0,0)
#var direction = Vector2(-1,0)

export(String, "Player", "Enemy", "NPC") var actor_type
#export var sprite_facing_right: bool = true
export(Array, Resource) var SoundEffects 
export var debug_actor: bool = false


onready var movement_animations: AnimatedSprite = $AnimatedSprite

onready var sound_effect_player: AudioStreamPlayer = $SE_Player

onready var movement_component: MovementComponent = $movement_component

onready var movement_state_machine: StateMachineAnimatedActor = $movement_state_machine

var sound_effects_dict: Dictionary

##TODO:
# Can't seem to implement the ready and process node functions
# without causing a double run somehow. Not sure why. 
# not a huge deal because this is mostly to help with 
# interfaces for the state machines.

func _ready() -> void:
	movement_state_machine.init_animated_actor(self, movement_animations, movement_component)
	movement_component.attack_function = funcref(self, "attack")
#	movement_component.player_number = player_number

	# Experimenting with sound based resources I did this and it worked
	#sound_effect_player.stream = sound_effects.sounds["pew"]
	#print(sounds.sounds["pew"])
	#sound_effects.play()
	
	# Trying something new with a custom resourse.
	for i in SoundEffects:
		sound_effects_dict[i.animation_name + str(i.frame_number)] = i.sound
	print (sound_effects_dict)	

func _unhandled_input(event: InputEvent) -> void:
	if actor_type == "Player":
		movement_component.process_input(event)
	movement_state_machine.process_input(event)

func _physics_process(delta: float) -> void:
	movement_component.process_physics(delta)
	movement_state_machine.process_physics(delta)

func _process(delta: float) -> void:
	if actor_type == "NPC" or actor_type == "Enemy":
		movement_component.process(delta)
	movement_state_machine.process_frame(delta)
#	PlayerInfo.player_position = global_position
	play_sound_frame(movement_animations.animation , movement_animations.frame)


var last_sound_frame_animation: String = ''
var last_sound_frame: int = -1
func play_sound_frame(animation: String, frame: int ):
	##DEBUG:
	#if animation == 'run' and frame == 6:
	#	print("Do the thign! (", animation + str(frame), ")", " (", last_sound_frame_animation + str(last_sound_frame), ")")
	if animation != last_sound_frame_animation or frame != last_sound_frame:
		last_sound_frame = frame
		last_sound_frame_animation = animation
		var sound_index = animation + str(frame)
		if sound_effects_dict.has(sound_index):
			sound_effect_player.stream = sound_effects_dict[sound_index]
			sound_effect_player.play()
			print(self.name, " Playing SE: ", sound_index)

func attack():
	#print(self.name, ": It's not inheritence, it's funcrefs")
	if movement_state_machine.current_state.name != 'attack':
		if($movement_state_machine/attack):
			movement_state_machine.change_state($movement_state_machine/attack)
		if ($Hitbox_Component/CollisionShape2D):
			#print(self.name, ": Found a hitbox")
			var hit_box = $Hitbox_Component
			hit_box.set_hitbox(true)
