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)
        {
            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 Server_NodeChanged(Agv.Node clientNode)
        {
            this.Invoke(new Action(() =>
            {
                label8.Text = "从客户端接收:" + clientNode.ToText();
            }
            ));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs 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 { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            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);
            }));
            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 (!status)
                    label13.BackColor = Color.Red;
                else
                    label13.BackColor = Color.Green;
            }
                ));
        }

        private void Client_Received(Agv.Node node)
        {
            this.Invoke(new Action(() =>
            {
                label12.Text = "从服务端端接收:" + node.ToText();
            }
            ));
        }

        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;
            }

        }
    }
}