namespace RioJoy.Tray;

internal static class Program
{
    // Per-session single-instance guard. Two instances would both grab the RIO COM
    // port, so a second launch (e.g. the launcher starting it while an instance is
    // already running) must exit immediately. A named mutex is released by the OS if the owner dies,
    // so a crash never leaves a stale lock.
    private const string SingleInstanceMutex = "RIOJoy.Tray.SingleInstance";

    /// <summary>
    /// Entry point. RIOJoy runs as a background tray application with no main
    /// window: an ApplicationContext owns the NotifyIcon and the runtime, so the
    /// message loop stays alive while the only UI is the tray icon and its menu.
    /// </summary>
    [STAThread]
    private static void Main()
    {
        using var instance = new Mutex(initiallyOwned: true, SingleInstanceMutex, out bool createdNew);
        if (!createdNew)
            return; // another RIOJoy is already running in this session

        // net48 has no source-generated ApplicationConfiguration.Initialize();
        // do the equivalent setup directly.
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new TrayApplicationContext());
    }
}
