using Remontor.Connector;
using Remontor.History;
using Remontor.Picter;
using Remontor.Pinger;
using Remontor.Seacher;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Remontor
{
    internal partial class SessionItemControl : UserControl
    {
        private bool PreActive = false;
        private IComp Comp;
        public delegate void SelectDelegat(SessionItemControl sessionItem);
        event SelectDelegat Select;
        private int Index;
        public ISeacherResult seacherResult;
        public SessionItemControl()
        {
            InitializeComponent();
        }

        public SessionItemControl(ISeacherResult seacherResult, SelectDelegat UpdateAct, int index)
        {
            InitializeComponent();
            this.seacherResult = seacherResult; 
            if (seacherResult is NoResult)
            {
                NoResult result = (NoResult) seacherResult;
                this.Dock = DockStyle.Top;
                LbName.Location = this.Location;               
                LbName.Text = result.toString();
               
                PbPerson.Visible = false;
                LbData.Visible = false;
                LbNetName.Visible = false;
                LbNetNameStatic.Visible = false;
                PPingStat.Visible = false;
                LbLogin.Visible = false;
                LbLoginStatic.Visible = false;


            }
            else if(seacherResult is HistoryResult)
            {
                HistoryResult result = (HistoryResult) seacherResult;
                Comp = result.GetComp();
                LbName.Text = Comp.GetDescription();
                LbLogin.Text = Comp.GetName();
                LbNetName.Text = Comp.GetNetName();
                LbData.Text = "last connect:" + result.GetLastConnect();
                Select += UpdateAct;
                Index = index;

                SPicManager.PictureSet(PbPerson, Comp);
                

            }
            else if (seacherResult is SCCMResult)
            {
                SCCMResult result = (SCCMResult)seacherResult;
                Comp = result.GetComp();
                LbName.Text = Comp.GetDescription();
                LbNetName.Text = Comp.GetNetName();
                LbLogin.Text = Comp.GetName();
                LbData.Text = "last login:" + result.GetLastConnect();
                Select += UpdateAct;
                Index = index;

                SPicManager.PictureSet(PbPerson, Comp);

            }
        }
        public void SelectPreActiv(SessionItemControl control)
        {
            if (control == this)
            {
                PreActive = true;
                this.BackColor = SystemColors.Control;
                // this.BackColor = SystemColors.GradientInactiveCaption;
            }
            else
            {
                PreActive = false;
                this.BackColor = SystemColors.Window;
            }
        }

        public int SelectActiv(SessionItemControl control)
        {
            if (control == this)
            {
                PreActive = true;
               // this.BackColor = SystemColors.Control;
                this.BackColor = SystemColors.GradientInactiveCaption;
            }
            else
            {
                PreActive = false;
                this.BackColor = SystemColors.Window;
            }
            return Index;
        }

        private void SessionItemControl_KeyDown(object sender, KeyEventArgs e)
        {
            
            System.Console.WriteLine("click");
        }

        private void SessionItemControl_Click(object sender, EventArgs e)
        {
            
           
               

        }

        private void SessionItemControl_Load(object sender, EventArgs e)
        {
            if (seacherResult is HistoryResult)
            {
                HistoryResult result = (HistoryResult)seacherResult;
                PingManager.NewTaskPing(PPingStat, result.GetComp());
            }else if (seacherResult is SCCMResult)
            {
                SCCMResult result = (SCCMResult)seacherResult;
                PingManager.NewTaskPing(PPingStat, result.GetComp());
            }
                
        }

        private void SessionItemControl_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                if (!(seacherResult is NoResult))
                {
                    Select(this);
                }
            }else if(e.Button == MouseButtons.Right)
            {
                if(Comp != null)
                {
                    this.BackColor = SystemColors.ActiveCaption;
                    Control control = (Control)sender;
                    SConnector.FormMain.MenuStripCommand.ShowMenu(Comp, control.PointToScreen(e.Location), CloseMenu);
                }
                
            }
            


        }
        private void CloseMenu()
            {
                this.BackColor = SystemColors.Window;
            }
    }
}
