using Godot;
using Newlon;
using System;

public partial class SettingsSerializer : Node
{
	const string CFG_PATH = "user://config.cfg";
	public override void _EnterTree()
	{
		GetTree().AutoAcceptQuit = false;
		if (FileAccess.FileExists(CFG_PATH) == false) return;

		var access = FileAccess.Open(CFG_PATH, FileAccess.ModeFlags.Read);

		Utility.SFX = float.Parse(access.GetLine().Split(" ")[1]);
		Utility.Music = float.Parse(access.GetLine().Split(" ")[1]);
		Utility.Splash = bool.Parse(access.GetLine().Split(" ")[1]);

		AudioServer.SetBusVolumeDb(0, Mathf.LinearToDb((float)Utility.SFX));
		AudioServer.SetBusVolumeDb(1, Mathf.LinearToDb((float)Utility.Music));

		access.Close();
	}
	public override void _ExitTree()
	{
		var access = FileAccess.Open(CFG_PATH, FileAccess.ModeFlags.Write);
		access.Resize(0);
		access.StoreString(string.Format("SFX {0}\nMusic {1}\nSplash {2}\n", Utility.SFX,Utility.Music,Utility.Splash));

		access.Close();
	}

	public override void _Notification(int what)
	{
		if (what == NotificationWMCloseRequest)
		{
			_ExitTree();
			GetTree().Quit();
		}
    }


}
