using Remontor.History;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Remontor.Connector
{
    internal static class SConnector
    {
        public delegate void UpdateConnect(List<Connect> connects);
        public static event UpdateConnect Update;
        public delegate void UpdateModeMon(IModeMon Mode);
        public static event UpdateModeMon UpdateMode;
        private static List<Connect> Connects = new List<Connect>();
        public static Control Panel;
        public static Form1 FormMain;
        public static Connect ActionConnect  =  null;
        public static void NewConnect(IComp comp) 
        {
            Connect con = new Connect(comp);
            
            Connects.Add(con);
            Update(Connects);
            con.ConnectSCCM(Panel);
            Activ(con);
            HistoryList.Add(con);
        }

        public static void NewConnectIP(IComp comp)
        {
            Connect con = new Connect(comp, comp.GetIP());

            Connects.Add(con);
            Update(Connects);
            con.ConnectSCCM(Panel);
            Activ(con);
            HistoryList.Add(con);
        }


        public static void Activ(Connect actConnect)
        {
            ActionConnect = actConnect;
            
            foreach (var connect in Connects)
            {
                if (connect != actConnect) connect.Activ(false);
            }
            actConnect.Activ(true);
            UpdateMode(actConnect.GetModeMon());

        }
        public static void DeleteConnect(Connect Connect)
        { 
            
            Connects.Remove(Connect);
            Update(Connects);
            if (Connect != ActionConnect) Activ(ActionConnect);
            UpdateMode(ActionConnect.GetModeMon());
        }

        public static void CloseAllSession()
        {
            Connect[] buffConnects = new Connect[Connects.Count];
            Connects.CopyTo(buffConnects);
            foreach(Connect connect in buffConnects)
            {
                DeleteConnect(connect);
            }

        }

        public static void SetModeMon(IModeMon Mode)
        {
            if(ActionConnect != null)
            {
                UpdateMode(ActionConnect.SetModeMon(Mode));
            }  
        }

        public static void ResizeActiv()
        {
            if(ActionConnect != null)
            {
                ActionConnect.Resize();
            }  
        }

        public static int ActivConnect()
        {
           return Connects.Count();
        }
    }
}
