using System.Reflection;
using KioskApp.Portal.Platforms.Attributes;

namespace KioskApp.Portal.Helper;

public static class OsReflectionHelper
{
    public static Type? GetSupportedClassForCurrentOs(Type baseType)
    {
        var currentOs = GetCurrentOs();
        var assembly = Assembly.GetExecutingAssembly();
        
        return assembly.GetTypes()
            .Where(t => baseType.IsAssignableFrom(t) && !t.IsAbstract)
            .FirstOrDefault(t => 
            {
                var attr = t.GetCustomAttribute<SupportedOSAttribute>();
                return attr != null && (attr.SupportedOperatingSystems & currentOs) == currentOs;
            });
    }

    private static SupportedOS GetCurrentOs()
    {
        if (OperatingSystem.IsWindows())
            return SupportedOS.Windows;
        if (OperatingSystem.IsLinux())
            return SupportedOS.Linux;
        if (OperatingSystem.IsMacOS())
            return SupportedOS.MacOS;
        return SupportedOS.None;
    }
}