class_name AudioManager
extends Node

# When the game starts, play background music
func _ready():
	if Global.play_background_music:
		play_background_music()
		
func play_chop_sound():
	if not Global.play_chop_sound:
		return
	## Pick one of the chopping sounds randomly
	var chop_sounds = [
		"res://assets/audio/OGG/SFX/chopping/chop1.ogg",
		"res://assets/audio/OGG/SFX/chopping/chop2.ogg",
		"res://assets/audio/OGG/SFX/chopping/chop3.ogg",
		"res://assets/audio/OGG/SFX/chopping/chop4.ogg"
	]
	var random_index = randi() % chop_sounds.size()
	play_sound_effect(chop_sounds[random_index])

func play_money_sound():
	if not Global.play_money_sound:
		return
	# Using a simple coin sound - you can replace with your own sound file
	play_sound_effect("res://assets/audio/coin.mp3")


func play_sound_effect(sound_path: String):
	var sfx_player = AudioStreamPlayer.new()
	sfx_player.stream = load(sound_path)
	sfx_player.volume_db = -5 # Set volume for sound effects
	add_child(sfx_player)
	sfx_player.play()
	sfx_player.connect("finished", sfx_player.queue_free)

var music_player: AudioStreamPlayer = null

func play_background_music():
	if not Global.play_background_music:
		return
	if music_player:
		return # Already playing
	music_player = AudioStreamPlayer.new()
	music_player.stream = load("res://assets/audio/background_music.ogg")
	music_player.volume_db = -10 # Set volume to a comfortable level
	music_player.autoplay = true
	add_child(music_player)
	music_player.play()

func stop_background_music():
	if music_player:
		music_player.stop()
		music_player.queue_free()
		music_player = null
