# credits to: Mina Pêcheux, https://www.youtube.com/watch?v=EUmN135jMdI
extends Node2D

const ZERO_VOLUME = -80
#@export var transition: float

# dungeon stream 0 and 1 are PRESENT NORMAL and PRESENT FIGHT, 2 and 3 are for PAST Normal and fight
@onready var dungeon_music_player: AudioStreamPlayer = $DungeonMusicPlayer
# boss stream 0 is PRESENT, 1 is PAST
@onready var boss_music_player: AudioStreamPlayer = $BossMusicPlayer
@onready var main_menu_music_player: AudioStreamPlayer = $MainMenuMusicPlayer
@onready var safe_space_music_player: AudioStreamPlayer = $SafeSpaceMusicPlayer

var is_in_combat: bool = false
var _enemy_amount_in_combat: int = 0

func _ready() -> void:
	play_main_menu_music()  # start from main menu
	SignalBus.boss_set_anchor.connect(play_boss_music) # switch to boss music if boss
	SignalBus.player_died.connect(play_no_music)  # switch to no music, if died
	SignalBus.start_game_or_respawn.connect(play_dungeon_music)  # switch back to dungeon music, if respawn or start game
	SignalBus.returned_to_main_menu.connect(play_main_menu_music)  # main menu music
	SignalBus.entered_boss_room.connect(play_no_music)  # quick silence before fight starts
	SignalBus.boss_fight_ended.connect(play_safe_space_music)  # boss dead music
	
	# react to situation (past, present, combat)
	adapt_music_style()
	
	# change if time shift
	SignalBus.changed_time.connect(adapt_music_style)
	# change if in combat
	SignalBus.enemy_joined_combat.connect(an_enemy_joined_combat)
	SignalBus.enemy_left_combat.connect(an_enemy_left_combat)
	SignalBus.loaded_room.connect(_reset_enemy_in_combat_amount)

#region enemy combat music
func _reset_enemy_in_combat_amount():
	_enemy_amount_in_combat = 0
	_change_combat_music()
	
func an_enemy_joined_combat():
	_enemy_amount_in_combat += 1
	_change_combat_music()
	
func an_enemy_left_combat():
	_enemy_amount_in_combat += -1
	_change_combat_music()
	
func _change_combat_music():
	is_in_combat = (_enemy_amount_in_combat > 0)
	adapt_music_style()
#endregion

#region switch whole music
func play_no_music():
	dungeon_music_player.stop()
	main_menu_music_player.stop()
	boss_music_player.stop()
	safe_space_music_player.stop()
	
func play_boss_music():
	dungeon_music_player.stop()
	main_menu_music_player.stop()
	safe_space_music_player.stop()
	boss_music_player.play()

func play_dungeon_music():
	boss_music_player.stop()
	main_menu_music_player.stop()
	safe_space_music_player.stop()
	dungeon_music_player.play()
	
func play_main_menu_music():
	boss_music_player.stop()
	dungeon_music_player.stop()
	safe_space_music_player.stop()
	main_menu_music_player.play()
	
func play_safe_space_music():
	boss_music_player.stop()
	dungeon_music_player.stop()
	main_menu_music_player.stop()
	safe_space_music_player.play()
#endregion

# switch music layers depening on situation (past, present, no combat, combat)
func adapt_music_style():
	if Globals.current_time == Utils.TIME.PRESENT:
		# boss
		boss_music_player.stream.set_sync_stream_volume(0, 0) 
		boss_music_player.stream.set_sync_stream_volume(1, ZERO_VOLUME) 
		
		# dungeon
		if is_in_combat:
			dungeon_music_player.stream.set_sync_stream_volume(0, ZERO_VOLUME)  # 0 if track 1 just overlay 
			dungeon_music_player.stream.set_sync_stream_volume(1, 0)  # track 1 (battle) at -0dB (normal)
			dungeon_music_player.stream.set_sync_stream_volume(2, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(3, ZERO_VOLUME)
		else:
			dungeon_music_player.stream.set_sync_stream_volume(0, 0)
			dungeon_music_player.stream.set_sync_stream_volume(1, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(2, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(3, ZERO_VOLUME)
	else:  # past
		# boss
		boss_music_player.stream.set_sync_stream_volume(0, ZERO_VOLUME) 
		boss_music_player.stream.set_sync_stream_volume(1, -5) 
		
		# dungeon
		if is_in_combat:
			dungeon_music_player.stream.set_sync_stream_volume(0, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(1, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(2, ZERO_VOLUME)  # 0 if track 2 just overlay 
			dungeon_music_player.stream.set_sync_stream_volume(3, 0)
		else:
			dungeon_music_player.stream.set_sync_stream_volume(0, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(1, ZERO_VOLUME)
			dungeon_music_player.stream.set_sync_stream_volume(2, 0)
			dungeon_music_player.stream.set_sync_stream_volume(3, ZERO_VOLUME)
