using Flurl;
using Flurl.Http;
using InnovEnergy.Lib.Utils;

namespace InnovEnergy.App.RemoteSupportConsole;

public static class VpnInfo
{
    private const String Address = "http://10.2.0.1";
    private const String Path    = "vpnstatus.txt";

    public static async Task<String?> LookUpIp(params String?[] commonNames)
    {
        $"looking up ip of {String.Join("/", commonNames)} in VPN routing table... ".Write(); 
        
        var raw = await Address
                       .AppendPathSegment(Path)
                       .GetStringAsync();

        var ip = raw
                .Split('\n')
                .Select(l => l.Split(','))
                .Where(l => l.Length > 2 && commonNames.Contains(l[2].Trim()) && l[0] == "ROUTING_TABLE")
                .Select(l => l[1])
                .SingleOrDefault();

        if (String.IsNullOrEmpty(ip))
            "not found".WriteLine(ConsoleColor.Red);
        else
            ip.WriteLine(ConsoleColor.Cyan);

        return ip;
    }


}