using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Text;

namespace DragonVideoPlayer
{
    public class ping_m3u8
    {
        public static async Task Main()
        {
            string[] servers =
            {
            "akariko-bck1.sankuria.sbs",
         
   
            "tbs5.mov3.co",
            "58.82.168.138"

        };

            var tasks = servers.Select(async server =>
            {
                using Ping ping = new Ping();

                try
                {
                    var reply = await ping.SendPingAsync(server, 3000);
                    return (Server: server,
                            Ping: reply.Status == IPStatus.Success ? reply.RoundtripTime : long.MaxValue);
                }
                catch
                {
                    return (Server: server, Ping: long.MaxValue);
                }
            });

            var result = (await Task.WhenAll(tasks))
                .OrderBy(x => x.Ping)
                .ToList();

            string path = "ping_ranking.txt";

            using StreamWriter sw = new StreamWriter(path, false);

            sw.WriteLine("TVのサーバーの反応速度のPingランキング");
            sw.WriteLine("-----------------------------");

            int rank = 1;

            foreach (var item in result)
            {
                if (item.Ping == long.MaxValue)
                    sw.WriteLine($"{rank,2}. {item.Server,-25} Timeout");
                else
                    sw.WriteLine($"{rank,2}. {item.Server,-25} {item.Ping,4} ms");

                rank++;
            }

            sw.Flush();

            Process.Start(new ProcessStartInfo(path)
            {
                UseShellExecute = true
            });
        }
    }


}
