using Avalonia;

namespace Lumi.Companion.App;

internal static class Program
{
    internal static SingleInstanceCoordinator? InstanceCoordinator { get; private set; }
    internal static bool LaunchInBackground { get; private set; }

    [STAThread]
    public static int Main(string[] args)
    {
        if (!OperatingSystem.IsWindows())
        {
            Console.Error.WriteLine("The first Lumi Companion release supports Windows x64 only.");
            return 2;
        }

        if (UpdateApplier.IsApplyRequest(args)) return UpdateApplier.Apply(args);
        if (ObsBridgeManager.IsDiagnosticRequest(args)) return ObsBridgeManager.RunDiagnostic(args);
        if (ObsBridgeManager.IsMaintenanceRequest(args)) return ObsBridgeManager.RunMaintenance(args);

        LaunchInBackground = args.Contains("--background", StringComparer.OrdinalIgnoreCase);
        InstanceCoordinator = new SingleInstanceCoordinator();
        if (!InstanceCoordinator.IsPrimary)
        {
            InstanceCoordinator.SignalPrimary();
            InstanceCoordinator.Dispose();
            return 0;
        }

        try
        {
            return BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
        }
        finally
        {
            InstanceCoordinator.Dispose();
        }
    }

    public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure<App>()
        .UsePlatformDetect()
        .WithInterFont()
        .LogToTrace();
}
