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 int x,y, Width, Height;
        public static FormWindowState WindowState = FormWindowState.Normal;
        public static void NewConnect(IComp comp)
        {
            Connect con = new Connect(comp);
            if (Connects.Count == 0)
            {
                Width = FormMain.Width;
                Height = FormMain.Height;
                x = FormMain.Location.X;
                y = FormMain.Location.Y;
                WindowState = FormMain.WindowState;
            }
            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());
            if (Connects.Count == 0)
            {
                Width = FormMain.Width;
                Height = FormMain.Height;
                x = FormMain.Location.X;
                y = FormMain.Location.Y;
                WindowState = FormMain.WindowState;
            }
            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);
            }
            if (actConnect != null)
            {
                actConnect.Activ(true);
                UpdateMode(actConnect.GetModeMon());
            }
        }
        public static void DeleteConnect(Connect Connect)
        {
            int index = Connects.IndexOf(Connect);
            Connects.Remove(Connect);
            Update(Connects);
            if (Connect != ActionConnect)
            {
                Activ(ActionConnect);
            }
            else
            {
               if(Connects.Count > 0)
                {
                    if (index == Connects.Count)
                    {
                        index--;
                    }
                    Activ(Connects[index]);
                }
                else
                {
                    ActionConnect = null;
                } 
            }
            if (ActionConnect != null) 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 void StateChangeActiv()
        {
            if (ActionConnect != null)
            {
                ActionConnect.StateChange();
            }
        }

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