extends Node

onready var _current_music_player = $MusicPlayer1
onready var _next_music_player= $MusicPlayer2

var _SILENT = -40.0
var _DESIRED_VOLUME = -15.0
var _DESIRED_SFX_VOLUME = -10.0

var _DESIRED_PAUSE_VOLUME = -25.0

enum Music{
	Intro,
	Abandon,
	PreGame
}
var _music_files := {
	Music.Intro: load("res://assets/Audio/Dungeon - Altus Stratum.ogg"),
	Music.Abandon: load("res://assets/Audio/Dungeon - Abandoned One.ogg"),
	Music.PreGame: load("res://assets/Audio/Scene - Lucid Night.ogg")
}


enum Sfx{
	COIN_COLLECTION,
	ORB_COLLECTION,
	PLAYER_JUMP,
	PLAYER_STEP_1,
	PLAYER_STEP_2,
	PLAYER_LAND,
	PLAYER_DASH,
	PLAYER_ATTACK,
	PLAYER_DIE,
	UI_MOVE,
	UI_SELECT,
	SPRING,
	SPIKE_TRAP,
	VASE_BREAK,
	MASK_FIRE
}

var _sfx_files := {
	Sfx.COIN_COLLECTION : load("res://assets/Audio/SFX/coin.wav"),
	Sfx.ORB_COLLECTION : load("res://assets/Audio/SFX/orb.wav"),
	Sfx.PLAYER_JUMP : load("res://assets/Audio/SFX/player_jump.wav"),
	Sfx.PLAYER_STEP_1 : load("res://assets/Audio/SFX/foot_step_1.wav"),
	Sfx.PLAYER_STEP_2 : load("res://assets/Audio/SFX/foot_step_2.wav"),
	Sfx.PLAYER_LAND : load("res://assets/Audio/SFX/player_land.wav"),
	Sfx.PLAYER_DASH : load("res://assets/Audio/SFX/player_dash.wav"),
	Sfx.PLAYER_ATTACK : load("res://assets/Audio/SFX/player_attack.wav"),
	Sfx.PLAYER_DIE : load("res://assets/Audio/SFX/player_die.wav"),
	Sfx.SPRING : load("res://assets/Audio/SFX/spring.wav"),
	Sfx.SPIKE_TRAP : load("res://assets/Audio/SFX/spike_trap.wav"),
	Sfx.MASK_FIRE : load("res://assets/Audio/SFX/mask_fire.wav"),
	Sfx.VASE_BREAK : load("res://assets/Audio/SFX/vase_break.wav"),
	Sfx.UI_MOVE : load("res://assets/Audio/UI_Sounds/menu_move_sound.wav"),
	Sfx.UI_SELECT : '',
}

var _current_music = null
var _next_music = null

func _ready() -> void:
	_current_music_player.volume_db = _SILENT
	_next_music_player.volume_db = _SILENT
	var children = $SfxPlayers.get_children()
	for child in children:
		var player:AudioStreamPlayer = child
		player.volume_db = _DESIRED_SFX_VOLUME

func play_music(music:int, transition_time:float = .5) -> void:
	_transition_to(music, transition_time)
	pass

func set_volume(duration:float=.2, volume:float = _DESIRED_PAUSE_VOLUME):
	_current_music_player.get_node("Tween").stop_all()
	_next_music_player.get_node("Tween").stop_all()
	_current_music_player.get_node("Tween").interpolate_property(_current_music_player, 'volume_db', _current_music_player.volume_db, volume, duration)	
	_current_music_player.get_node("Tween").start()
	
func play_sfx(sfx:int, priority:int=0):
	var sfx_player = _get_available_sfx_player()
	if sfx_player != null:
		sfx_player.stream = _sfx_files[sfx]
		sfx_player.play()
	pass

func _transition_to(music:int, duration:float):
	if _current_music == null:
		_play_first_music(music, duration)
		return
	_next_music = music
	_next_music_player.stream = _music_files[music]
	_next_music_player.play()
	_current_music_player.get_node("Tween").stop_all()
	_next_music_player.get_node("Tween").stop_all()
	_current_music_player.get_node("Tween").interpolate_property(_current_music_player, 'volume_db', _current_music_player.volume_db, _SILENT, duration)
	_next_music_player.get_node("Tween").interpolate_property(_next_music_player, 'volume_db', _next_music_player.volume_db, _DESIRED_VOLUME, duration)
	_current_music_player.get_node("Tween").start()
	_next_music_player.get_node("Tween").start()
	
func _play_first_music(music:int, duration:float):
	_current_music = music
	_current_music_player.stream = _music_files[music]
	_current_music_player.play()
	_current_music_player.get_node("Tween").interpolate_property(_current_music_player, 'volume_db', _current_music_player.volume_db, _DESIRED_VOLUME, duration)
	_current_music_player.get_node("Tween").start()
	

func _get_available_sfx_player()->AudioStreamPlayer:
	var children = $SfxPlayers.get_children()
	for child in children:
		var player:AudioStreamPlayer = child
		if not player.playing: return player
	return null

func _on_Tween_completed(object: Object, key: NodePath) -> void:
	if object.volume_db == _DESIRED_VOLUME and _next_music != null:
		var tmp = _current_music_player
		_current_music_player = _next_music_player
		_next_music_player = tmp
		_next_music_player.stop()
		_current_music = _next_music
		_next_music = null
	pass # Replace with function body.


func current_music():
	return _current_music