extends Node

var _current = 0

@onready var _tracks = [ $Track1, $Track2 ]
@onready var _animation_player = $AnimationPlayer

func play_music(audio: AudioStream, crossfade = true):
	if crossfade:
		_current += 1
		_current %= 2
		_tracks[_current].stream = audio
		_tracks[_current].play()
		_animation_player.play("fade_to{0}".format([_current+1]))
	else:
		_tracks[_current].stream = audio
		_tracks[_current].play()
	
func _on_game_pause_toggled(paused: bool):
	_tracks[_current].volume_db = -10.0 if paused else 0.0

func _on_cutscene_issued_command(command: CutsceneCommand, manager: CutsceneManager):
	if command is CueBGMCommand:
		play_music(command.music, command.crossfade)
		manager.step()


func _on_animation_player_animation_finished(anim_name: StringName):
	if anim_name == &"fade_to1":
		_tracks[1].stop()
	else:
		_tracks[0].stop()
