using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ytLive.Helpers;

/// <summary>
/// Best-effort diagnostic: when a camera won't start, lists other running
/// processes known to hold cameras (OBS, Zoom, Teams, NVIDIA Broadcast, etc.).
/// Windows doesn't expose "which process has this device" via any public API,
/// so this is a suspect list, not a verdict — but it's far better than
/// "maybe in use by another app" with no idea which one.
/// </summary>
public static class CameraConflictProbe
{
    private static readonly HashSet<string> KnownCameraApps = new()
    {
        "obs64", "obs32", "zoom", "teams", "ms-teams", "discord",
        "nvidia broadcast", "skype", "webex", "slack",
        "streamlabs obs", "restream studio", "manycam", "snap camera",
        "logitech capture", "logitune", "facerig", "animaze",
        "camera", "vmix", "xsplit broadcaster", "xsplit gamecaster",
        "droidcam", "ivcam", "epoccam", "camo",
        "chrome", "msedge", "firefox", "brave",
    };

    /// <summary>
    /// Returns the friendly names of known camera apps currently running, or an
    /// empty list if none are found (or the probe itself fails).
    /// </summary>
    public static List<string> GetRunningCameraApps()
    {
        try
        {
            return Process.GetProcesses()
                .Where(p => KnownCameraApps.Contains(p.ProcessName.ToLowerInvariant()))
                .Select(p => p.ProcessName)
                .Distinct()
                .OrderBy(n => n)
                .ToList();
        }
        catch
        {
            return new List<string>();
        }
    }
}
