extends Node

var bgm_dict : Dictionary = {}
var sfx_dict : Dictionary = {}

var sfx_bubble_wrap_pop_0 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_0.ogg")
var sfx_bubble_wrap_pop_1 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_1.ogg")
var sfx_bubble_wrap_pop_2 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_2.ogg")
var sfx_bubble_wrap_pop_3 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_3.ogg")
var sfx_bubble_wrap_pop_4 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_4.ogg")
var sfx_bubble_wrap_pop_5 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_5.ogg")
var sfx_bubble_wrap_pop_6 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_6.ogg")
var sfx_bubble_wrap_pop_7 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_7.ogg")
var sfx_bubble_wrap_pop_8 : AudioStream = preload("res://utilities/audio/bubble_wrap_pop_8.ogg")

var sfx_bubble_wrap_step_0 : AudioStream = preload("res://utilities/audio/bubble_wrap_step_0.ogg")
var sfx_bubble_wrap_step_1 : AudioStream = preload("res://utilities/audio/bubble_wrap_step_1.ogg")

var sfx_bubble_wrap_roll : AudioStream = preload("res://utilities/audio/bubble_wrap_roll.ogg")

var sfx_ui_click : AudioStream = sfx_bubble_wrap_pop_3
var sfx_mouse_click : AudioStream = sfx_bubble_wrap_pop_8

@onready var bgm_player: AudioStreamPlayer = $BGMPlayer

func _ready() -> void:
	# add audio streams to a dictionary
	sfx_dict[GameConstants.SFX_POP_0] = sfx_bubble_wrap_pop_0
	sfx_dict[GameConstants.SFX_POP_1] = sfx_bubble_wrap_pop_1
	sfx_dict[GameConstants.SFX_POP_2] = sfx_bubble_wrap_pop_2
	sfx_dict[GameConstants.SFX_POP_3] = sfx_bubble_wrap_pop_3
	sfx_dict[GameConstants.SFX_POP_4] = sfx_bubble_wrap_pop_4
	sfx_dict[GameConstants.SFX_POP_5] = sfx_bubble_wrap_pop_5
	sfx_dict[GameConstants.SFX_POP_6] = sfx_bubble_wrap_pop_6
	sfx_dict[GameConstants.SFX_POP_6] = sfx_bubble_wrap_pop_6
	sfx_dict[GameConstants.SFX_POP_7] = sfx_bubble_wrap_pop_7
	sfx_dict[GameConstants.SFX_POP_8] = sfx_bubble_wrap_pop_8
	
	sfx_dict[GameConstants.SFX_STEP_0] = sfx_bubble_wrap_step_0
	sfx_dict[GameConstants.SFX_STEP_1] = sfx_bubble_wrap_step_1
	
	sfx_dict[GameConstants.SFX_ROLL] = sfx_bubble_wrap_roll
	
	sfx_dict[GameConstants.SFX_UI_CLICK] = sfx_ui_click
	sfx_dict[GameConstants.SFX_MOUSE_CLICK] = sfx_mouse_click


func play_sfx(sfx_name : String) -> void:
	var stream : AudioStream = sfx_dict[sfx_name]
	
	if not stream:
		return
	
	var asp := AudioStreamPlayer.new()
	asp.stream = stream
	
	add_child(asp)
	
	asp.play()
	
	await asp.finished
	asp.queue_free()


func play_bgm(bgm_name : String) -> void:
	var stream : AudioStream = bgm_dict[bgm_name]
	
	if not stream:
		return

	bgm_player.stop()
	bgm_player.stream = stream
	bgm_player.play()


func stop_bgm() -> void:
	bgm_player.stop()
