## Resource for storing BPM data and loading into a SyncTrack.
class_name Tempo extends Resource

## Defines how the BPM changes throughout its length.
enum BPM_TYPE {
	HOLD = 0, ## BPM stays constant through the whole length.
	LINEAR = 1 ## BPM linearly interpolates to the next BPM.
}

# The data for each BPM is stored in the same index
# across the different arrays.
@export var bpms: Array[float] = []
@export var lengths: Array[float] = []
@export var types: Array[BPM_TYPE] = []


## Get the number of valid BPM changes.
func size() -> int:
	return min(bpms.size(), lengths.size() + 1)


## Ensure the arrays obey the following properties:
## all arrays are the same size (every BPM has a length and type),
## last element of BPM Length Beats is -1 (since the
## corresponding BPM is assumed to last the rest of the chart).
## Extra BPM or BPM Length values are truncated.
## If BPM Types must be resized greater, assume every BPM after is
## BPM_TYPE.HOLD.
func normalize() -> void:
	var target_size := size()
	bpms.resize(target_size)
	lengths.resize(target_size)
	types.resize(target_size)
	
	if target_size > 0:
		_make_bpm_positive()
		_make_lengths_positive()
		lengths[-1] = -1
		types[-1] = BPM_TYPE.HOLD


#----- Static Constructors -----#

static func create_from_arrays(
		p_bpms: Array[float],
		p_lengths: Array[float],
		p_types: Array[BPM_TYPE]
	) -> Tempo:
	var result: Tempo = Tempo.new()
	result.bpms = p_bpms
	result.lengths = p_lengths
	result.types = p_types
	result.normalize()
	return result

static func create_from_nodes(_p_nodes: Array[Variant]) -> Tempo:
	var result: Tempo = Tempo.new()
	# Parse array and add data to result...
	# Might not be used?
	return result
#----- ------------- -----#


## Makes sure every BPM entry is positive.
## Negative BPMs are inverted.
func _make_bpm_positive() -> void:
	for i: int in range(bpms.size()):
		bpms[i] = abs(bpms[i])


## Makes sure every length entry is positive.
## Negative lengths are inverted.
func _make_lengths_positive() -> void:
	for i: int in range(lengths.size()):
		lengths[i] = abs(lengths[i])
