using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace IOB_WIN_FACADE
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string sep = "----------------------------";
            Console.WriteLine(sep);
            Console.WriteLine("- IOB-WIN-FACADE");
            Console.WriteLine(sep);
            Console.WriteLine("Implementazione segnaposto di applicazioni IOB-WIN-*, da aggiornare con EgwAppControlCenter");
            Console.WriteLine(); 
            Console.WriteLine();
            int idx = 0;
            while (true)
            {
                var testPing = testPingMachine;
                Console.WriteLine($"Ping localhost... {testPing} | ctrl-c to close");
                Task.Delay(1000).Wait();
                idx++;
                if (idx >= 20)
                {
                    Console.WriteLine();
                    Console.WriteLine(sep);
                    Console.WriteLine("- IOB-WIN-FACADE");
                    Console.WriteLine(sep);
                    Console.WriteLine();
                    idx = 0;
                }
            }
        }

        /// <summary>
        /// test ping all'indirizzo PLC/CNC impostato nei parametri
        /// </summary>
        /// <returns></returns>
        protected static IPStatus testPingMachine
        {
            get
            {
                IPStatus answ = IPStatus.Unknown;
                // se disabilitato salto...

                IPAddress address;
                PingReply reply;
                using (Ping pingSender = new Ping())
                {
                    address = IPAddress.Loopback;
                    int pingMsTimeout = 500;
                    IPAddress.TryParse("localhost", out address);
                    try
                    {
                        // se != null --> uso address...
                        if (address != null)
                        {
                            reply = pingSender.Send(address, pingMsTimeout);
                        }
                        else
                        {
                            reply = pingSender.Send(IPAddress.Loopback, pingMsTimeout);
                        }
                    }
                    catch
                    {
                        reply = pingSender.Send(IPAddress.Loopback, pingMsTimeout);
                    }
                    answ = reply.Status;

                }
                return answ;
            }
        }
    }
}
