class_name MovementParameters
extends Reference

# Allow floats from -10 to 20 and snap the value to multiples of 0.2.

export(PoolVector2Array) var _speed_start = PoolVector2Array([Vector2(0,0),Vector2(0,0),Vector2(0,0)])
export(PoolVector2Array) var _speed_end = PoolVector2Array([Vector2(0,0),Vector2(0,0),Vector2(0,0)])
export(PoolVector2Array) var _acceleration = PoolVector2Array([Vector2(0,0),Vector2(0,0),Vector2(0,0)])


## Vector based ones for x and y
#export var base_move_speed :Vector2
#export var base_move_acceleration :Vector2
#export var move_speed_modifier :Vector2
#export var move_speed_modifier_acceleration :Vector2
export var jerk_factor :Vector2

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

## Can be set to help with debugging the velocity controller
export var debug_name :String

# export var base_h_move_speed :float
# export var base_h_move_acceleration :float
# export var h_move_speed_modifier :float
# export var h_move_modifier_move_acceleration :float

# export var base_v_move_speed :float
# export var base_v_move_acceleration :float
# export var v_move_speed_modifier :float
# export var v_move_modifier_move_acceleration :float

func get_speed_start(direction :int) -> Vector2:
	return _speed_start[direction]

func set_speed_start(direction :int, value :Vector2) -> void:
	_speed_start[direction] = value

func get_speed_end(direction :int) -> Vector2:
	return _speed_end[direction]

func set_speed_end(direction :int, value :Vector2) -> void:
	_speed_end[direction] = value

func get_acceleration(direction :int) -> Vector2:
	return _acceleration[direction]

func set_acceleration(direction :int, value :Vector2) -> void:
	_acceleration[direction] = value


func modify(_movement_parameters :MovementParameters):
	pass

func apply_multiple_modifiers( _modifiers: Array, _movement_direction :Vector2):
	for m in _modifiers:
		if m is StateModifierMovement:
			apply_state_modifier(m, _movement_direction)

func apply_state_modifier(_modifier :StateModifierMovement, _movement_direction :Vector2):
	#_modifier.direction == _modifier.APPLIED_DIRECTIONS.LEFT_OR_UP
	match _modifier.modifier_type:
		StateModifierMovement.SUB_TYPE.NEGATIVE_CONSTRAINT:
#			var adjusted_move_speed = base_move_speed.x + _modifier.horizontal_speed
#			## We crossed the zero from the base
#			if sign(adjusted_move_speed) != sign(base_move_speed.x):
#				base_move_speed.x = 0.0
#			else:
#				base_move_speed.x = adjusted_move_speed
			
			var speed :Vector2 = get_speed_start(_modifier.direction)
			var adjusted_move_speed = get_speed_start(_modifier.direction) + _modifier.speed_start
			if sign(adjusted_move_speed.x) != sign(speed.x):
				speed.x = 0.0
			else:
				speed.x = adjusted_move_speed.x
			if sign(adjusted_move_speed.y) != sign(speed.y):
				speed.y = 0.0
			else:
				speed.y = adjusted_move_speed.y
			set_speed_start(_modifier.direction, speed)
			
			speed = get_speed_end(_modifier.direction)
			adjusted_move_speed = get_speed_end(_modifier.direction) + _modifier.speed_end
			if sign(adjusted_move_speed.x) != sign(speed.x):
				speed.x = 0.0
			else:
				speed.x = adjusted_move_speed.x
			if sign(adjusted_move_speed.y) != sign(speed.y):
				speed.y = 0.0
			else:
				speed.y = adjusted_move_speed.y
			set_speed_end(_modifier.direction, speed)
			
			
#			var adjusted_accel = base_move_acceleration.x + _modifier.horizontal_acceleration
#			if sign(adjusted_accel) != sign(base_move_acceleration.x):
#				base_move_acceleration.x = 0.0
#			else:
#				base_move_acceleration.x = adjusted_accel
			
			var accel :Vector2 = get_acceleration(_modifier.direction)
			var adjusted_accel = get_acceleration(_modifier.direction) + _modifier.acceleration
			if sign(adjusted_accel.x) != sign(accel.x):
				accel.x = 0.0
			else:
				accel.x = adjusted_accel.x
			if sign(adjusted_accel.y) != sign(accel.y):
				accel.y = 0.0
			else:
				accel.y = adjusted_accel.y
			set_acceleration(_modifier.direction, accel)
			
			if (true): #DEGUG
				var foo = 2+2
			
			##TODO: Um, modifiers are a little weirder for this
			#move_speed_modifier.x += _modifier.horizontal_speed_offset
#			var speed_differance = base_move_speed.x + (move_speed_modifier.x + _modifier.horizontal_speed_offset)
#			if sign(speed_differance) != sign(base_move_speed.x) and base_move_speed.x != 0:
#				## Add the inverse of the modifier instead
#				move_speed_modifier.x += speed_differance * - 1
#			else:
#				move_speed_modifier.x += _modifier.horizontal_speed_offset
#
#			move_speed_modifier_acceleration.x += _modifier.horizontal_speed_offset_acceleration
#			move_speed_modifier.y += _modifier.vertical_speed_offset
#			move_speed_modifier_acceleration.y += _modifier.vertical_speed_offset_acceleration
		_:
#			base_move_speed.x += _modifier.horizontal_speed
#			base_move_acceleration.x += _modifier.horizontal_acceleration
#			move_speed_modifier.x += _modifier.horizontal_speed_offset
#			move_speed_modifier_acceleration.x += _modifier.horizontal_speed_offset_acceleration
#
#			base_move_speed.y += _modifier.vertical_speed
#			base_move_acceleration.y += _modifier.vertical_acceleration
#			move_speed_modifier.y += _modifier.vertical_speed_offset
#			move_speed_modifier_acceleration.y += _modifier.vertical_speed_offset_acceleration
			
			set_speed_start(
				_modifier.direction, 
				(get_speed_start(_modifier.direction) + _modifier.speed_start)
			)
			set_speed_end(
				_modifier.direction, 
				(get_speed_end(_modifier.direction) + _modifier.speed_end)
			)
			set_acceleration(
				_modifier.direction, 
				(get_acceleration(_modifier.direction) + _modifier.acceleration)
			)

			if (true): #DEGUG
				var foo = 2+2
			
#	UiManager.debug_text =	( #"H_Speed x Dir: " + str(h_speed) + str('duh') +
#		"BaseSpeed  Fm:{0} To:{1}".format({"0":"%5.2f" % base_move_speed.x, "1":"%5.2f" % base_move_speed.y}) + 
#		"\nSpeedMod   Fm:{0} To:{1}".format({"0":"%5.2f" % move_speed_modifier.x, "1":"%5.2f" % move_speed_modifier.y}) + 
#		"\nMoveAccel: {0}, {1}".format({"0":"%4.1f" % base_move_acceleration.x, "1":"%4.1f" % base_move_acceleration.y}) + 
#		"\nType: " + str(_modifier.modifier_type)
#	)

	
	
# var movement_params :Dictionary = {
#     "_base_h_move_speed" : _state.horizontal_speed,
#     "_base_h_move_acceleration" : _state.horizontal_acceleration,
#     "_base_h_move_speed_modifier" : _state.horizontal_speed_offset,
#     "_base_h_move_modifier_move_acceleration" : _state.horizontal_speed_offset_acceleration,
#     "_jerk_factor" : _state.jerk_factor,
#     "_gravity" : _state.gravity
# }
