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 AgvClientTest
{
    public partial class Form1 : Form
    {
        private Agv.Server server;
        private Agv.AgvClient client;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Agv.Common.EnDecodeSingleNode = false;
            server = new Agv.Server();
            server.NodeChanged += Server_NodeChanged;
            server.NodeOnline += Server_NodeOnline;
            comboBox1.Items.AddRange(Enum.GetNames(typeof(Agv.ClientAction)));
            comboBox2.Items.AddRange(Enum.GetNames(typeof(Agv.ClientLevel)));
            comboBox3.Items.AddRange(Enum.GetNames(typeof(Agv.ClientType)));
            comboBox4.Items.AddRange(Enum.GetNames(typeof(Agv.ClientShelf)));
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
            comboBox4.SelectedIndex = 0;
            client = new Agv.AgvClient();
            client.Received += Client_Received;
            client.Connected += Client_Connected;
        }

        private void Client_Log(string s)
        {
            Agv.log.info(s);
        }

        private void Server_NodeChanged(Agv.Node clientNode)
        {
            this.Invoke(new Action(() =>
            {
                listBox1.Items.Add(clientNode.ToText());
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
            }
            ));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //comboBox1.Items.AddRange(Enum.GetNames(typeof(Agv.ClientAction)));
            //comboBox2.Items.AddRange(Enum.GetNames(typeof(Agv.ClientLevel)));
            //comboBox3.Items.AddRange(Enum.GetNames(typeof(Agv.ClientType)));
            //comboBox4.Items.AddRange(Enum.GetNames(typeof(Agv.ClientShelf)));
            int i1 = comboBox1.SelectedIndex;
            int i2 = comboBox2.SelectedIndex;
            int i3 = comboBox4.SelectedIndex;
            int i4 = comboBox3.SelectedIndex;
            Task task = new Task(new Action(() =>
            {

                //client.SetStatus(textBox1.Text,textBox3.Text,textBox2.Text, (Agv.ClientAction)i1,
                //    (Agv.ClientLevel)i2,(Agv.ClientShelf)i3,
                //    (Agv.ClientType)i4);
                client.SetStatus(textBox1.Text, textBox3.Text, textBox2.Text, (Agv.ClientAction)i1, (Agv.ClientLevel)i2, (Agv.ClientShelf)i3, (Agv.ClientType)i4);//, (Agv.ClientShelf)i3
                //for (int i = 0; i < 100; i++)
                //{
                //    client.SetStatus(string.Format("A{0}",i), textBox3.Text, textBox2.Text, (Agv.ClientAction)i1, (Agv.ClientLevel)i2, (Agv.ClientShelf)i3, (Agv.ClientType)i4);
                //}
            }));
            task.Start();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            server.Start(textBox4.Text, int.Parse(textBox5.Text));
            this.Invoke(new Action(() =>
            {
                label11.BackColor = Color.Green;

            }
));
        }

        private void Server_NodeOnline(string nodeName, bool online)
        {
            this.Invoke(new Action(() =>
            {
                label11.Text = nodeName;
                if (online)
                    label11.BackColor = Color.Green;
                else
                    label11.BackColor = Color.Red;
            }
            ));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            server.Stop();
            this.Invoke(new Action(() =>
            {
                label11.BackColor = Color.Red;

            }
));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            client.Connect(textBox4.Text, int.Parse(textBox5.Text));
        }

        private void Client_Connected(bool status)
        {
            this.Invoke(new Action(() =>
            {
                if (label3 == null)
                    return;
                if (!status)
                    label13.BackColor = Color.Red;
                else
                    label13.BackColor = Color.Green;
            }
                ));
        }

        private void Client_Received(Agv.Node node)
        {
            this.Invoke(new Action(() =>
            {
                listBox2.Items.Add(node.ToText());
                listBox2.SelectedIndex = listBox2.Items.Count - 1;
            }
            ));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            client.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int i1 = comboBox1.SelectedIndex;
            server.SendToClient(textBox1.Text, textBox2.Text, (Agv.ClientAction)i1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (client.CancelState)
            {
                client.CancelState = false;
                button7.BackColor = Color.Green;
            }
            else
            {
                client.CancelState = true;
                button7.BackColor = Color.Red;
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox6.Text = "";
            try
            {
                if (Agv.Common.EnDecodeSingleNode)
                {
                    int i1 = comboBox1.SelectedIndex;
                    int i2 = comboBox2.SelectedIndex;
                    int i3 = comboBox4.SelectedIndex;
                    int i4 = comboBox3.SelectedIndex;
                    Agv.Node node = new Agv.Node()
                    {
                        Name = textBox1.Text,
                        Mark = textBox3.Text,
                        RFID = textBox2.Text,
                        Action = (Agv.ClientAction)i1,
                        Level = (Agv.ClientLevel)i2,
                        Shelf = (Agv.ClientShelf)i3,
                        Type = (Agv.ClientType)i4
                    };
                    byte[] buff = Agv.Common.Encode(node);
                    textBox6.Text = Agv.Common.HexBuffWithoutSpace(buff);
                }
                else
                {
                    byte[] buff = Agv.Common.Encode(client.Nodes);
                    textBox6.Text = Agv.Common.HexBuffWithoutSpace(buff);
                }

            }
            catch
            {
                textBox6.Text = "";
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //try
            //{
            //    if (server != null)
            //    {
            //        server.NodeChanged -= Server_NodeChanged;
            //        server.NodeOnline -= Server_NodeOnline;
            //        server.Stop();
            //        server = null;
            //    }
            //    if (client != null)
            //    {
            //        client.Received -= Client_Received;
            //        client.Connected -= Client_Connected;
            //        client.Close();
            //        client = null;
            //    }
            //}
            //catch { }         
        }
    }
}