using Microsoft.Win32;

internal static class Utils
{
    public static string? GetGamePath()
    {
        string? path;
        path = TryGetXboxPath();
        return path;

        /*
         * to-do:
         *  implement steam and epic
         */
    }

    private static string? TryGetXboxPath()
    {
        const string keyPath = @"Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\Repository\Packages";
        using RegistryKey? key = Registry.CurrentUser.OpenSubKey(keyPath);

        if (key == null) return null;

        string? match = key.GetSubKeyNames().FirstOrDefault(n => n.StartsWith(Constants.PackageName, StringComparison.OrdinalIgnoreCase));
        if (match == null) return null;

        using RegistryKey? subKey = key.OpenSubKey(match);
        if (subKey == null) return null;

        object? pkgRootFolder = subKey.GetValue("PackageRootFolder");
        if (pkgRootFolder == null) return null;

        return pkgRootFolder.ToString();
    }
}
