#@tool
#@icon(icon_path: String)
class_name RhythmManager
extends Node2D
## Documentation comments

signal song_started()
## enum
## const

@export var target_audio: AudioStreamPlayer2D
@export var tempo: int = 120
@export var beats_per_measure: int = 4

var beats_elapsed: int = 0
var beat_time_start: float = 0.0
var beat_time_elapsed: float = 0.0
var seconds_per_beat: float
var useconds_per_beat: float
var song_time_elapsed: float = 0.0
var total_time_elapsed: float = 0.0
var is_running: bool = false
var start_time: float
var end_time: float
var ready_time: float
var click_track_stream: AudioStream
#var delay_time: float

@onready var beat_bar: ProgressBar = %BeatBar
#@onready var click_track: AudioStreamPlayer2D = %ClickTrack
@onready var beat_time_value: Label = %BeatTimeValue
@onready var volume_bar_left: ProgressBar = %VolumeBarLeft
@onready var volume_bar_right: ProgressBar = %VolumeBarRight
@onready var volume_value_left: Label = %VolumeValueLeft
@onready var volume_value_right: Label = %VolumeValueRight

## OVERRIDES

func _ready() -> void:
	#click_track_stream = click_track.stream
	ready_time = Time.get_ticks_usec()
	print("Ready time: %s" % ready_time)
	print("Buses at index 0: %s" % AudioServer.get_bus_name(0))
	print("Channels on Bus 0: %s" % AudioServer.get_bus_channels(0))
	#var time_to_next_mix: float = AudioServer.get_time_to_next_mix() 
	#var output_latency: float = AudioServer.get_output_latency()
	#var mix_rate: float = AudioServer.get_mix_rate()
	#delay_time = time_to_next_mix + output_latency
	seconds_per_beat = 60.0/float(tempo)
	useconds_per_beat = seconds_per_beat * 1000000.0
	beat_bar.max_value = useconds_per_beat

func _process(_delta: float) -> void:
	total_time_elapsed = Time.get_ticks_usec()
	if is_running:
		volume_bar_left.value = AudioServer.get_bus_peak_volume_left_db(0, 0)
		volume_bar_right.value = AudioServer.get_bus_peak_volume_right_db(0, 0)
		volume_value_left.text = str(snappedf(AudioServer.get_bus_peak_volume_left_db(0, 0), 0.1))
		volume_value_right.text = str(snappedf(AudioServer.get_bus_peak_volume_right_db(0, 0), 0.1))
		song_time_elapsed = total_time_elapsed - start_time
		#print("song time elapsed: %s" % song_time_elapsed)
		beat_time_elapsed = song_time_elapsed - beat_time_start
		beat_time_value.text = str(beat_time_elapsed) + " usec"
		beat_bar.value = beat_time_elapsed
		#print("beat time elapsed: %s" % beat_time_elapsed)
		#fmod = fmod(total_time_elapsed, useconds_per_beat)
		#print(fmod)
		if beat_time_elapsed >= useconds_per_beat:
			#print("This beat took %s useconds" % beat_time_elapsed)
			beats_elapsed += 1
			beat_time_elapsed = 0.0
			beat_time_start = song_time_elapsed

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("start"):
		if is_running == false:
			start_time = Time.get_ticks_usec()
			print("start time: %s" % start_time)
			is_running = true
			target_audio.play()
		else:
			target_audio.stop()
			is_running = false
			end_time = Time.get_ticks_usec()
			print("end_time: %s" % end_time)
		song_started.emit()

#func _physics_process(delta: float) -> void:
	#pass

## CORE

## PRIVATE/HELPER

## RECEIVERS

## SETTERS/GETTERS
