using Insight.Agent.Extensions;
using Insight.Agent.Messages;
using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;

namespace Insight.Agent.Services
{
    public partial class CollectorService
    {
        public OperationSystem? GetOperatingSystem()
        {
            Logger.LogTrace("GetOperatingSystem");

            var os = new OperationSystem();

            // get uptime
            var output = string.Empty;

            // read file
            using var stream = File.OpenText(@"/proc/uptime");
            output = stream.ReadToEnd();

            // clean output
            var clean = Regex
                .Replace(output
                .Trim()
                .Replace("\t", " "), @"[ ]{2,}", " ");

            var elements = clean.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);

            // assign values
            //os.Uptime = DateTime.Now - TimeSpan.FromSeconds(double.Parse(elements.ElementAt(0)));

            // request data with process
            output = "hostnamectl".Bash();

            // linebreak list conversion
            var lines = new List<string>(output
                .Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
                .Select(l =>
                {
                    return Regex
                    .Replace(l
                    .Trim()
                    .Replace("\t", " "), @"[ ]{2,}", " ");
                })
                .ToList();

            // assign values
            os.Virtual = lines
                .Any(l => l
                .StartsWith("Virtualization:")) && !string.IsNullOrEmpty(lines
                .Where(l => l
                .StartsWith("Virtualization:"))
                .First()
                .Split("Virtualization:")[1]
                .Trim());

            // OS
            // request data with process
            output = "hostnamectl".Bash();

            // linebreak list conversion
            lines = new List<string>(output
                .Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
                .Select(l =>
                {
                    return Regex
                    .Replace(l
                    .Trim()
                    .Replace("\t", " "), @"[ ]{2,}", " ");
                })
                .ToList();

            // assign values
            os.Name = lines
                .Any(l => l
                .StartsWith("Operating System:")) ? lines
                .Where(l => l
                .StartsWith("Operating System:"))
                .First()
                .Split("Operating System:")[1]
                .Trim() : string.Empty;

            os.Version = lines
                .Any(l => l
                .StartsWith("Kernel:")) ? lines
                .Where(l => l
                .StartsWith("Kernel:"))
                .First()
                .Split("Kernel:")[1]
                .Trim() : string.Empty;

            var architecture = lines
                .Any(l => l
                .StartsWith("Architecture:")) ? lines
                .Where(l => l
                .StartsWith("Architecture:"))
                .First()
                .Split("Architecture:")[1]
                .Trim() : string.Empty;

            return os;
        }
    }
}