extends MeshInstance

class_name Road

export var end_rotation:Vector3 = Vector3(0,0,0)

export var first_speed_factor:float = 1.0
export var creator_speed_factor:float = 1.0
export var chasers_speed_factor:float = 1.0

export var reset_index:int = 0

export (PoolVector3Array) var speed_constrains = PoolVector3Array()
export (PoolVector3Array) var force_penalties = PoolVector3Array()
export (PoolVector3Array) var torque_penalties = PoolVector3Array()
export (Array,float) var path_penalties = []

var body:StaticBody

var creator:int = -1
var preview:bool = false


func _init():
	if not has_node("StaticBody"):
		body = StaticBody.new()
		body.set_name("StaticBody")
		var shape = CollisionShape.new()
		shape.set_name("CollisionShape")
		shape.shape = mesh.create_trimesh_shape()
		body.add_child(shape)
		body.set_collision_layer_bit(0,1)
		body.set_collision_layer_bit(1,1)
		body.set_collision_layer_bit(2,1)
		body.set_collision_layer_bit(3,1)
		add_child(body)
	else:
		body = get_node("StaticBody")


func get_creator():
	return creator


func set_creator(new_creator):
	creator = int(new_creator)


func set_color(new_color):
	pass # TODO


func get_path():
	return get_node("Path")


func get_curve():
	return get_path().get_curve()


func get_lane_curve(lane:int):
	if has_node("lanes"):
		return get_node("lanes").get_child(lane).get_curve()
	else:
		return get_path().get_curve()


func get_end_rotation():
	return end_rotation


func set_preview(prev):
	preview = prev
	if prev:
		set_material_override(SpatialMaterial.new())
	else:
		set_material_override(null)


func is_preview():
	return preview


func get_next_lane(lane):
	return lane


func penalty_index(distance:float, speed:float):
	for index in range(speed_constrains.size()):
		var constrain = speed_constrains[index]
		if constrain.x <= distance && constrain.y >= distance:
			if constrain.z < 0 && speed < constrain.z * -1 || constrain.z > 0  && speed > constrain.z:
				return index
	return -1


func get_torque_penalty(index:int):
	if index >= 0:
		if torque_penalties.size() < (index + 1):
			return get_torque_penalty(index - 1)
		if get_rotation().length() != 0:
			return torque_penalties[index].rotated(get_rotation().normalized(), get_rotation().length())
		else: 
			return torque_penalties[index]
	return Vector3(0,0,0)


func get_force_penalty(index:int):
	if index >= 0:
		if force_penalties.size() < (index + 1):
			return get_force_penalty(index - 1)
		if get_rotation().length() != 0:
			return force_penalties[index].rotated(get_rotation().normalized(), get_rotation().length())
		else:
			return force_penalties[index]
	
	return Vector3(0,0,0)


func get_path_penalty(index:int):
	if index >= 0:
		if path_penalties.size() < (index + 1):
			return get_path_penalty(index - 1)
			
		return path_penalties[index]
	
	return 0.0


func get_first_speed_factor():
	return first_speed_factor


func get_creator_speed_factor():
	return creator_speed_factor


func get_chasers_speed_factor():
	return chasers_speed_factor