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

namespace Remontor.Finder
{
    internal partial class Finder : Form
    {
        private SessionItemControl SelectedItem;
        private bool StopRefreshSeacher = false;
        private int PreSelected = -1;
        public Finder()
        {
            InitializeComponent();
            SConnector.FormMain.MenuStripCommand.actForm = this;
            SessionItemControl itemControl = new SessionItemControl(new NoResult("Введите запрос"), UpdateActiv,0);
            SeaherPanel.Controls.Add(itemControl);
        }

        private void ConnectBtn_Click(object sender, EventArgs e)
        {
            
            if(SelectedItem != null)
            {
              
                SConnector.NewConnect(SelectedItem.seacherResult.Result());
            }
            else
            {
                if (CompNameLB.Text.Length > 0)
                {
                    SConnector.NewConnect(new CompId(CompNameLB.Text));
                }
                else
                {
                    MessageBox.Show("Введите имя компьютера для подключения");
                }
                    
            }
            this.Close();
        }

        private void CompNameLB_TextChanged(object sender, EventArgs e)
        {
            if (sender is TextBox && !StopRefreshSeacher)
            {
                TextBox textBox = (TextBox)sender;
                SSeaher.seaherMetod.Change(cb_comp_ResultUpdate, textBox.Text);
               // CompSelected = null;
               // ListComp.Visible = true;
            }
        }

        public void cb_comp_ResultUpdate(List<ISeacherResult> Items)
        {
            PreSelected = -1;
            SelectedItem = null;
            SeaherPanel.SuspendLayout();
            SeaherPanel.Controls.Clear();
            int countItem = 0; 
            
            foreach (ISeacherResult item in Items)
            {
                SessionItemControl itemControl = new SessionItemControl(item, UpdateActiv, countItem++);
                SeaherPanel.Controls.Add(itemControl);
                
            }
            SeaherPanel.ResumeLayout();



        }
     
        private void UpdateActiv(SessionItemControl sessionItem)
        {
            if(SelectedItem == sessionItem)
            {
                SConnector.NewConnect(SelectedItem.seacherResult.Result());
                this.Close();
                return;
            }
            if(SelectedItem != null) SelectedItem.SelectActiv(null);
            if(PreSelected > -1)((SessionItemControl)SeaherPanel.Controls[PreSelected]).SelectPreActiv(null);
            SelectedItem = sessionItem;
            PreSelected = sessionItem.SelectActiv(sessionItem);
            StopRefreshSeacher = true;
            CompNameLB.Focus();
            CompNameLB.Text = sessionItem.seacherResult.ResultString();
            CompNameLB.Select(CompNameLB.Text.Length, 0);
            StopRefreshSeacher = false;
        }

        private void CompNameLB_KeyDown(object sender, KeyEventArgs e)
        {
            if (SeaherPanel.Controls[0] is SessionItemControl)
            {

                switch (e.KeyValue)
                {
                    case 40:
                        if (!(((SessionItemControl)SeaherPanel.Controls[0]).seacherResult is NoResult))
                        {
                            if (PreSelected < SeaherPanel.Controls.Count - 1)
                            {
                                SelectedItem = null;
                                if (PreSelected > -1) ((SessionItemControl)SeaherPanel.Controls[PreSelected]).SelectPreActiv(null);
                                ++PreSelected;
                                SeaherPanel.ScrollControlIntoView(SeaherPanel.Controls[PreSelected]);
                                ((SessionItemControl)SeaherPanel.Controls[PreSelected]).SelectPreActiv((SessionItemControl)SeaherPanel.Controls[PreSelected]);
                            }
                            e.SuppressKeyPress = true;

                        }
                        break;

                    case 38:
                        if (!(((SessionItemControl)SeaherPanel.Controls[0]).seacherResult is NoResult))
                        {
                            if (PreSelected > 0)
                            {
                                SelectedItem = null;
                                if (PreSelected > -1) ((SessionItemControl)SeaherPanel.Controls[PreSelected]).SelectPreActiv(null);
                                --PreSelected;
                                SeaherPanel.ScrollControlIntoView(SeaherPanel.Controls[PreSelected]);
                                ((SessionItemControl)SeaherPanel.Controls[PreSelected]).SelectPreActiv((SessionItemControl)SeaherPanel.Controls[PreSelected]);
                            }

                            e.SuppressKeyPress = true;
                            //ListComp.Refresh();
                        }
                        break;
                    case 13:
                        if (!(((SessionItemControl)SeaherPanel.Controls[0]).seacherResult is NoResult))
                        {
                            if (PreSelected > -1)
                            {
                                if ((SessionItemControl)SeaherPanel.Controls[PreSelected] == SelectedItem)
                                {

                                    //bt_reset_Click(null, null);
                                    SConnector.NewConnect(SelectedItem.seacherResult.Result());
                                    this.Close();
                                    break;
                                }
                                else if (PreSelected >= 0)
                                {
                                    if (SelectedItem != null) SelectedItem.SelectActiv(null);
                                    SelectedItem = (SessionItemControl)SeaherPanel.Controls[PreSelected];
                                    ((SessionItemControl)SeaherPanel.Controls[PreSelected]).SelectActiv((SessionItemControl)SeaherPanel.Controls[PreSelected]);
                                    StopRefreshSeacher = true;
                                    CompNameLB.Focus();
                                    CompNameLB.Text = ((SessionItemControl)SeaherPanel.Controls[PreSelected]).seacherResult.ResultString();
                                    CompNameLB.Select(CompNameLB.Text.Length, 0);
                                    StopRefreshSeacher = false;

                                    e.SuppressKeyPress = true;
                                    break;

                                }
                            }


                        }
                        if (PreSelected == -1)
                        {
                            if (CompNameLB.Text.Length > 0)
                            {
                                SConnector.NewConnect(new CompId(CompNameLB.Text));
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("Введите имя компьютера для подключения");
                            }
                        }
                        break;
                
                
            }
        }

    }

    private void Finder_FormClosed(object sender, FormClosedEventArgs e)
        {
            SConnector.FormMain.MenuStripCommand.actForm = null;
        }
    }
}
