using Remontor.Setting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Remontor.Pinger
{
    internal static class PingManager
    {
        static Dictionary<string, PingResult> ResultPing = new Dictionary<string, PingResult>();
        static List<PingTask> TaskList = new List<PingTask>();
        static int LifeTimePing = 20;
     

        static public void NewTaskPing(Panel panel, IComp comp)
        {

            if (ResultPing.ContainsKey(comp.GetNetName()))
            { 
                if ((DateTime.Now - ResultPing[comp.GetNetName()].timePing).Seconds > LifeTimePing)
                {
                    //  panel.Visible = true;
                    //   panel.BackColor = Color.Gold;
                    ResultPing.Remove(comp.GetNetName());
                    PingTask pingTask = new PingTask(panel, comp);
                    TaskList.Add(pingTask);
                }
                else
                {
                    //panel.Visible = true;
                    if (ResultPing[comp.GetNetName()].Succes)
                    {
                        panel.BackColor = Color.Lime;
                    }
                    else
                    {
                        panel.BackColor = Color.OrangeRed;
                    }
                }

            }
            else
            {
                //panel.Visible = true;
                // panel.BackColor = Color.Gold;
                PingTask pingTask = new PingTask(panel, comp);
                
                //if(PingerHost.ThreadState == ThreadState.Unstarted)
                //{
                //    PingerHost.Start();
                //}else if(PingerHost.ThreadState == ThreadState.Stopped)
                //{
                //    PingerHost = new Thread(PingHost);
                //    PingerHost.Start();
                //}

            }


        }


      

        static public void WriteResult(PingResult pingResult)
        {
            try
            {
                if(ResultPing.ContainsKey(pingResult.NameOrAddress)) ResultPing.Add(pingResult.NameOrAddress, pingResult);
                while (ResultPing.Count > 200)
                {
                    ResultPing.Remove(ResultPing.First().Key);

                }
            }
            catch
            {

            }
            
        }




    }
}
