using System;
using System.Diagnostics;
using System.IO;

public static class WavePlayer
{
    private static Process _current;

    private static bool IsUnix
    {
        get
        {
            int p = (int)Environment.OSVersion.Platform;
            return p == 4 || p == 6 || p == 128;
        }
    }

    public static void Play(string absolutePath)
    {
        Stop();

        if (!IsUnix)
        {
            try
            {
                var sp = new System.Media.SoundPlayer(absolutePath);
                sp.Play();
            }
            catch (Exception ex) { Console.WriteLine("Playback failed: " + ex.Message); }
            return;
        }

        foreach (string player in new[] { "paplay", "aplay" })
        {
            try
            {
                var psi = new ProcessStartInfo(player, "\"" + absolutePath + "\"")
                {
                    UseShellExecute = false,
                    RedirectStandardError = true
                };
                _current = Process.Start(psi);
                return;
            }
            catch { /* not installed, try the next one */ }
        }

        Console.WriteLine("No audio player found — install pulseaudio-utils or alsa-utils");
    }

    public static void Stop()
    {
        try
        {
            if (_current != null && !_current.HasExited) _current.Kill();
        }
        catch { }
        _current = null;
    }
}