extends AudioStreamPlayer

const MENU_SCENES: Dictionary = {
	"res://scenes/UI/start_screen.tscn": true,
	"res://scenes/UI/Settings.tscn": true,
	"res://scenes/UI/login_screen.tscn": true,
}

const CONFIG_PATH := "user://settings.cfg"
const CONFIG_SECTION := "audio"
const CONFIG_KEY_MUSIC_VOLUME := "menu_music_volume"
const CONFIG_KEY_MUSIC_MUTED := "menu_music_muted"
const DEFAULT_VOLUME := 0.7

var _last_scene: Node = null
var _user_volume_linear: float = DEFAULT_VOLUME
var _is_muted: bool = false

func _ready() -> void:
	set_process(true)
	_load_settings()
	_apply_volume()
	_update_playback(get_tree().current_scene)

func _process(_delta: float) -> void:
	var current := get_tree().current_scene
	if current != _last_scene:
		_update_playback(current)
	elif _should_play_scene(current) and not playing and not _is_muted and _user_volume_linear > 0.0:
		play()

func _update_playback(scene: Node) -> void:
	if scene == null:
		_last_scene = null
		return
	_last_scene = scene
	if _should_play_scene(scene):
		if not playing:
			play()
	elif playing:
		stop()

func _should_play_scene(scene: Node) -> bool:
	if scene == null:
		return false
	var scene_path: String = scene.get_scene_file_path()
	if scene_path.is_empty():
		return false
	return MENU_SCENES.has(scene_path)

func set_user_volume(value: float) -> void:
	var clamped_value: float = clamp(value, 0.0, 1.0)
	if is_equal_approx(_user_volume_linear, clamped_value):
		return
	_user_volume_linear = clamped_value
	_apply_volume()
	_save_settings()

func get_user_volume() -> float:
	return _user_volume_linear

func set_user_muted(muted: bool) -> void:
	var new_muted: bool = muted
	if _is_muted == new_muted:
		return
	_is_muted = new_muted
	_apply_volume()
	_save_settings()

func is_user_muted() -> bool:
	return _is_muted

func _apply_volume() -> void:
	if _is_muted or _user_volume_linear <= 0.0:
		volume_db = -80.0
	else:
		volume_db = linear_to_db(_user_volume_linear)

func _load_settings() -> void:
	var config: ConfigFile = ConfigFile.new()
	var err: int = config.load(CONFIG_PATH)
	if err == OK:
		var stored_volume: float = float(config.get_value(CONFIG_SECTION, CONFIG_KEY_MUSIC_VOLUME, DEFAULT_VOLUME))
		_user_volume_linear = clamp(stored_volume, 0.0, 1.0)
		var stored_muted: bool = bool(config.get_value(CONFIG_SECTION, CONFIG_KEY_MUSIC_MUTED, false))
		_is_muted = stored_muted
	elif err == ERR_DOES_NOT_EXIST:
		_user_volume_linear = DEFAULT_VOLUME
		_is_muted = false
	else:
		push_warning("Failed to load settings.cfg: %s" % err)
		_user_volume_linear = DEFAULT_VOLUME
		_is_muted = false

func _save_settings() -> void:
	var config: ConfigFile = ConfigFile.new()
	var err: int = config.load(CONFIG_PATH)
	if err != OK and err != ERR_DOES_NOT_EXIST:
		push_warning("Failed to load settings.cfg before saving: %s" % err)
		config = ConfigFile.new()
	elif err != OK:
		config = ConfigFile.new()
	config.set_value(CONFIG_SECTION, CONFIG_KEY_MUSIC_VOLUME, _user_volume_linear)
	config.set_value(CONFIG_SECTION, CONFIG_KEY_MUSIC_MUTED, _is_muted)
	var save_err: int = config.save(CONFIG_PATH)
	if save_err != OK:
		push_warning("Failed to save settings.cfg: %s" % save_err)
