using System.Runtime.InteropServices;
// ReSharper disable CollectionNeverUpdated.Global

namespace Млаумчерб.Клиент.классы;

public static class Буржуазия
{
    public static bool CheckOs(Os os) =>
        os.name switch
        {
            null => true,
            "osx" => OperatingSystem.IsWindows(),
            "linux" => OperatingSystem.IsLinux(),
            "windows" => OperatingSystem.IsWindows(),
            _ => throw new ArgumentOutOfRangeException(os.name)
        }
        && os.arch switch
        {
            null => true,
            "x86" => RuntimeInformation.OSArchitecture == Architecture.X86,
            "x64" => RuntimeInformation.OSArchitecture == Architecture.X64,
            "arm64" => RuntimeInformation.OSArchitecture == Architecture.Arm64,
            _ => false
        };

    public static bool CheckRules(ICollection<Rule> rules, ICollection<string> enabled_features)
    {
        bool allowed = false;
        foreach (var r in rules)
        {
            if (r.os == null || CheckOs(r.os))
            {
                if (r.features != null)
                {
                    foreach (var feature in enabled_features)
                    {
                        if(r.features.TryGetValue(feature, out bool is_enabled))
                        {
                            if (is_enabled)
                            {
                                if (r.action != "allow")
                                    return false;
                            }
                        }
                    }
                }
                
                if (r.action == "allow")
                    allowed = true;
                else return false;
            }
        }

        return allowed;
    } 
}
