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)])

## MP accelleration is reflective of minimum maximum and adjusted accel
## It can be accessed directly now, doesn't need the direction
export(Vector2) var _acceleration = Vector2(0,0)
const BASE_ACCELERATION = 0
const MAX_ACCELERATION = 1
const MIN_ACCELERATION = 2 # or -1

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

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() -> Vector2:
	return _acceleration

## Sort of the conditional clamp of acceleration parameters
func set_acceleration(value :Vector2, min_value :Vector2, max_value :Vector2) -> void:
	var adjusted_accel = value
	if adjusted_accel.x < min_value.x:
		adjusted_accel.x = min_value.x
	if adjusted_accel.y < min_value.y:
		adjusted_accel.y = min_value.y
	
	if max_value.x != 0:
		if adjusted_accel.x > max_value.x:
			adjusted_accel.x = max_value.x
	if max_value.y != 0:
		if adjusted_accel.y > max_value.y:
			adjusted_accel.y = max_value.y

	_acceleration = adjusted_accel


func modify(_movement_parameters :MovementParameters):
	pass

func apply_multiple_modifiers( _modifiers: Array, _movement_direction :Vector2):
	for m in _modifiers:
		if m is StateModifierMovement:
			##TODO: Check sign of direction here or maybe further down?
			# if m.direction != sign(_movement_direction):
			# 	pass
			apply_state_modifier(m, _movement_direction)

func apply_state_modifier(_modifier :StateModifierMovement, _movement_direction :Vector2):
	##TODO: probably math the min and max as well somehow
	## Accel processing is the same regardless of direction now
	set_acceleration( ( _acceleration + _modifier.acceleration), 
						_modifier.min_acceleration, 
						_modifier.max_acceleration )

	match _modifier.processing_mode:
		StateModifierMovement.SUB_TYPE.NEGATIVE_CONSTRAINT:
			
			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)
			
			
			if (true): #DEGUG
				var foo = 2+2
			

		_:
			
			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)
			)

			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)
#	)

