using Godot;

namespace Mr.BrickAdventures.Autoloads;

public partial class ConfigFileHandler : Node
{
    public static ConfigFileHandler Instance { get; private set; }

    private ConfigFile _settingsConfig = new();
    public const string SettingsPath = "user://settings.ini";

    public ConfigFile SettingsConfig => _settingsConfig;

    public override void _Ready()
    {
        Instance = this;
        if (!FileAccess.FileExists(SettingsPath))
        {
            var err = _settingsConfig.Save(SettingsPath);
            if (err != Error.Ok)
                GD.PushError($"Failed to create settings file at {SettingsPath}: {err}");
        }
        else
        {
            var err = _settingsConfig.Load(SettingsPath);
            if (err != Error.Ok)
                GD.PushError($"Failed to load settings file at {SettingsPath}: {err}");
        }
    }

    public override void _ExitTree()
    {
        if (Instance == this) Instance = null;
    }
}