using System.Runtime.Versioning;
using System.ServiceProcess;
using System.Text;

namespace Insight.Agent
{
    internal class Helpers
    {
        internal static string? EscapeWql(string text)
        {
            if (text == null) return null;

            var sb = new StringBuilder(text.Length);
            foreach (var c in text)
            {
                if (c == '\\' || c == '\'' || c == '"')
                {
                    sb.Append('\\');
                }
                sb.Append(c);
            }
            return sb.ToString();
        }

        [SupportedOSPlatform("windows")]
        internal static bool ForceWinRmStart()
        {
            var winRm = ServiceController
                .GetServices()
                .First(x => x.ServiceName
                .ToLower()
                .Equals("winrm", StringComparison.Ordinal));

            if (winRm.Status is not ServiceControllerStatus.Running)
            {
                winRm.Start();
                winRm.WaitForStatus(ServiceControllerStatus.Running);
            }

            if (winRm.Status != ServiceControllerStatus.Running) return false;
            return true;
        }
    }
}