class_name StateModifier
extends Reference
## State modification
##
## A state modifier doesn't have any direct control of a game object. 
## They are transfered from the enter and exit of other states and 
## influence the movement or animation of their parent states. 
## Rather than going full into paralell state machines I hope that this 
## modifier will be suitable for a basic state machine.
##
## @WIP

enum TYPE {NONE,EXIT_ANIMATION, ANIMATION_SUFFIX, REPLACE_ANIMATION}

var debug_state: bool = false

## These should be the original properties set when the modifier is created.
# property changes can be applied over time but these are the instantiated values.
var modifier_properties: ModifierProperties

## Animation modifier variables
## The name of the animation that could be applied and starting frame
var animation_name: String
var starting_frame: int = 0
##TODO: Animation speed

## Modification Type
## if an animation is specified, the default behavior will be just to override
## the state animation. Or perform a 'Replace Animation'
var modifier_type = TYPE.NONE

var name :String = ''

# Move Speed in Pixels Per Second
# These should only apply if Modification Type Set to None.
# (But I'm not sure if I like that model.)
#var move_speed: float
#var move_speed_modifier: float = 0
#var move_speed_modifier_decay: float = 0

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


# Attempting to use this as an animation suffix that can linger after
# state transition like 'run-fire' to 'idle-fire' 'jump-fire' etc.
#var animation_suffix: bool = false
#var pre_append_animation: bool = false

# not sure if I can do the array thing from state change. Maybe? 
# disabling this sequencing for now. 
# export(Array, String) var animation_sequence
# var animation_index: int = 0
# var current_animation_sequence: int = 0


#var animation_sequence_timer: Timer

var state_timeout: Timer
var timeout_seconds: float = 0.0

# Meant to be called on timeout or other counters this modifier may have.
# Should be updated whenever modifier owner transfers
var state_call_function: FuncRef

## Can't do it this way.
#func copy_modifier() -> StateModifier:
#	var new_modifier = StateModifier.new()
#	new_modifier.ready(animation_name, modifier_type, timeout_seconds)
#
#	return StateModifier.new()
#
func merge_modifier(merging_mod: ModifierProperties):
	if merging_mod.modifier_type != TYPE.NONE:
		print("Warning Merging modifier types that aren't set to 'none' is not supported!")
		return
	# Can't support timeouts or animation stuff right now.
	#timeout_seconds = merging_mod.timeout_seconds
#	move_speed = merging_mod.move_speed
#	gravity = merging_mod.gravity
#	move_speed_modifier = merging_mod.move_speed_modifier
#	move_speed_modifier_decay = merging_mod.move_speed_modifier_decay
	

func get_modifier_properties() -> ModifierProperties:
	#( p_move_speed:float, p_gravity:int , p_move_acceleration , p_move_speed_modifier:float,
	#	 p_move_modifier_move_acceleration:float, p_jerk_factor: float):
	var mp = ModifierProperties.new( modifier_properties.move_speed,
									modifier_properties.gravity, 
									modifier_properties.move_acceleration,
									modifier_properties.move_speed_modifier,
									modifier_properties.move_modifier_move_acceleration,
									modifier_properties.jerk_factor)
	mp.directional_modifier = modifier_properties.directional_modifier
	return mp

func ready(modifier_name:String, anim_name:String = '', type = TYPE.NONE, timeout : float = 0, parent:Node = null, function_name : String = "") -> Timer:
	name = modifier_name 
	# If somebody forgot to specify the type
	if anim_name != '' and type == TYPE.NONE:
		modifier_type = TYPE.REPLACE_ANIMATION
	animation_name = anim_name
	modifier_type = type
#	if parent != null and function_name != "":
#		state_call_function = funcref(parent, function_name)

	modifier_properties = ModifierProperties.new(0,0,0,0,0,0)

	if timeout > 0:
		state_timeout = Timer.new()
		state_timeout.wait_time = timeout
		state_timeout.one_shot = true
		state_timeout.autostart = false
		return state_timeout
	#add_child(state_timeout)
	#ModifierProperties.new()
	return null


## Transfer and callback related methods. These sort of worked but I didn't end up using them
## They're an interesting idea though.
func transfer_owner(parent:Node):
	print("Modifier owner will now be: ", parent.name)
	state_call_function = funcref(parent, 'update_animation')

func _on_Timer_timeout():
	#print("Oh Crap! I can't beleive it worked")
	if state_call_function:
		if state_call_function.is_valid():
			state_call_function.call_func()
		else:
			print("Warning! Modifier timed out with no callback to alert.")
	else:
		print("Warning! no function applies.")
