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 AsaPL.AgvClient client;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++)
                comboBox1.Items.Add((AsaPL.ClientAction)i);
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.Items.Add(AsaPL.ClientAction.MayEnter);
            comboBox3.Items.Add(AsaPL.ClientAction.MayLeave);
            comboBox3.SelectedIndex = 0;
            client = new AsaPL.AgvClient("10.85.199.1");
            client.ReadyEnter += AGV_ReadyEnter;
            client.ReadyLeave += AGV_ReadyLeave;
            client.SetStatus("E1", "", AsaPL.ClientAction.None);
            client.SetStatus("E2", "", AsaPL.ClientAction.None);
            client.SetStatus("E3", "", AsaPL.ClientAction.None);
            client.SetStatus("E4", "", AsaPL.ClientAction.None);
            client.SetStatus("E5", "", AsaPL.ClientAction.None);
            client.SetStatus("E6", "", AsaPL.ClientAction.None);
            client.SetStatus("E8", "", AsaPL.ClientAction.None);
            client.SetStatus("E9", "", AsaPL.ClientAction.None);
            client.SetStatus("E10", "", AsaPL.ClientAction.None);
            client.SetStatus("E11", "", AsaPL.ClientAction.None);
            client.SetStatus("E12", "", AsaPL.ClientAction.None);
            client.SetStatus("E14", "", AsaPL.ClientAction.None);
            client.SetStatus("E15", "", AsaPL.ClientAction.None);
            client.SetStatus("E16", "", AsaPL.ClientAction.None);
            client.SetStatus("E21", "", AsaPL.ClientAction.None);
            client.SetStatus("E22", "", AsaPL.ClientAction.None);
            client.SetStatus("G10", "", AsaPL.ClientAction.None);
            client.SetStatus("G21", "", AsaPL.ClientAction.None);
            client.SetStatus("G22", "", AsaPL.ClientAction.None);
            client.Connect();
        }


        private void AGV_ReadyEnter(string nodeName)
        {
            if (checkBox1.Checked)
            {
                if (nodeName.StartsWith("A") || nodeName.EndsWith("A"))
                    return;
                label4.Text = "收到" + nodeName + "ReadyEnter,节点状态变为MayEnter";
                client.SetStatus(nodeName, "", AsaPL.ClientAction.MayEnter);

                System.Threading.Thread.Sleep(10000);
                client.SetStatus(nodeName, "", AsaPL.ClientAction.None);
                label4.Text = "设置" + nodeName + " None";
            }

        }
        private void AGV_ReadyLeave(string nodeName)
        {
            if (checkBox1.Checked)
            {
                if (nodeName.StartsWith("A") || nodeName.EndsWith("A"))
                    return;
                label4.Text = "收到" + nodeName + "ReadyLeave,节点状态变为MayLeave";
                client.SetStatus(nodeName, "", AsaPL.ClientAction.MayLeave);
                System.Threading.Thread.Sleep(10000);
                client.SetStatus(nodeName, "", AsaPL.ClientAction.None);
                label4.Text = "设置" + nodeName + " None";
            }

        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            client.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task task = new Task(new Action(() => {
                client.SetStatus(comboBox2.SelectedItem.ToString(), textBox1.Text, (AsaPL.ClientAction)comboBox1.SelectedIndex);
                System.Threading.Thread.Sleep(10000);
                client.SetStatus(comboBox2.SelectedItem.ToString(), "", AsaPL.ClientAction.None);
                label4.Text = "设置" + comboBox2.SelectedItem.ToString() + " None";
            }));
            task.Start();

        }
        private void SetLineState(object sender, EventArgs e)
        {
            Task task = new Task(new Action(()=>{
                if (comboBox3.SelectedItem == null)
                    return;
                string name = ((Button)sender).Text;
                client.SetStatus(name, "", (AsaPL.ClientAction)Enum.Parse(typeof(AsaPL.ClientAction), comboBox3.SelectedItem.ToString()));
                label5.Text = name + " " + comboBox3.SelectedItem.ToString();

                System.Threading.Thread.Sleep(10000);
                client.SetStatus(name, "", AsaPL.ClientAction.None);
                label5.Text = "设置" + name + " None";
            }));
            task.Start();

        }

    }
}