using AGVLib;
using Common;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace AgvComTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] server = AppConfigSetting.GetStrVal("server", "127.0.0.1:12000").Split(':');
            if (server != null && server.Length == 2)
            {
                txtIP.Text = server[0];
                txtPort.Text = server[1];
            }
            client = new Client();
            client.ConnectedEvent += Client_ConnectedEvent;
            client.ReceivedEvent += Client_ReceivedEvent;
            comboBox1.Items.AddRange(Enum.GetNames(typeof(NodeStatus)));
            comboBox2.Items.AddRange(Enum.GetNames(typeof(NodeLevel)));
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            button1.Enabled = true;
            button2.Enabled = false;

        }

        private void Client_ReceivedEvent(Node node)
        {
            this.Invoke(new Action(()=>
            {
                try
                {
                    txtJSON.Text = "";
                    txtBytes.Text = "";
                    groupBox4.Text = $"字节码";
                    string json = JsonHelper.SerializeObject(node);
                    textBox1.Text = node.name;
                    txtNodeId.Text = node.id.ToString();
                    checkBox1.Checked = node.shielded;
                    txtShelfId.Text = node.shelf_id;
                    txtRemark.Text = node.remark;
                    comboBox1.SelectedIndex = (int)node.status;
                    comboBox2.SelectedIndex = (int)node.level;
                    byte[] bytes = AGVLib.Common.Encode(node);
                    txtBytes.Text = StringHelper.ToHexString(bytes);
                    txtJSON.Text = json;
                    groupBox4.Text = $"字节码-长度为{bytes.Length}";
                }
                catch
                {
                    txtJSON.Text = "收到的数据解码失败";
                }
            }));

        }

        private void Client_ConnectedEvent(bool status)
        {
            label8.Invoke(new Action(()=> { label8.BackColor = status ? Color.Green : Color.Red; }));
        }

        private Client client;

        private void button1_Click(object sender, EventArgs e)
        {
            client.Connect(txtIP.Text, int.Parse(txtPort.Text));
            button1.Enabled = false;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            client.Close();
            button1.Enabled = true;
            button2.Enabled = false;
        }
        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                txtBytes.Text = "";
                txtJSON.Text = "";
                groupBox4.Text = $"字节码";
                int status = comboBox1.SelectedIndex;
                int level = comboBox2.SelectedIndex;
                Node node = new Node()
                {
                    name = textBox1.Text,
                    id = int.Parse(txtNodeId.Text),
                    shielded = checkBox1.Checked,
                    shelf_id = txtShelfId.Text,
                    remark = txtRemark.Text,
                    status = (NodeStatus)comboBox1.SelectedIndex,
                    level = (NodeLevel)comboBox2.SelectedIndex
                };
                byte[] bytes = AGVLib.Common.Encode(node);
                string json = JsonHelper.SerializeObject(node);
                txtBytes.Text = StringHelper.ToHexString(bytes);
                groupBox4.Text = $"字节码-长度为{bytes.Length}";
                txtJSON.Text = json;
            }
            catch
            {
                txtBytes.Text = "编码失败";
                txtJSON.Text = "编码失败";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBytes.Text)) return;
            try
            {
                txtJSON.Text = "";
                groupBox4.Text = $"字节码";
                byte[] bytes = StringHelper.StrToHexByte(txtBytes.Text);
                Node node = AGVLib.Common.Decode(bytes);
                string json = JsonHelper.SerializeObject(node);
                txtJSON.Text = json;
                textBox1.Text = node.name;
                txtNodeId.Text = node.id.ToString();
                checkBox1.Checked = node.shielded;
                txtShelfId.Text = node.shelf_id;
                txtRemark.Text = node.remark;
                comboBox1.SelectedIndex = (int)node.status;
                comboBox2.SelectedIndex = (int)node.level;
                groupBox4.Text = $"字节码-长度为{bytes.Length}";
            }
            catch
            {
                txtJSON.Text = "解码失败";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                txtBytes.Text = "";
                txtJSON.Text = "";
                groupBox4.Text = $"字节码";
                int status = comboBox1.SelectedIndex;
                int level = comboBox2.SelectedIndex;
                Node node = new Node()
                {
                    name = textBox1.Text,
                    id = int.Parse(txtNodeId.Text),
                    shelf_id = txtShelfId.Text,
                    remark = txtRemark.Text,
                    status = (NodeStatus)comboBox1.SelectedIndex,
                    level = (NodeLevel)comboBox2.SelectedIndex
                };
                client.Shielded = checkBox1.Checked;
                byte[] bytes = AGVLib.Common.Encode(node);
                string json = JsonHelper.SerializeObject(node);
                txtBytes.Text = StringHelper.ToHexString(bytes);
                txtJSON.Text = json;
                client.SetStatus(node);
                groupBox4.Text = $"字节码-长度为{bytes.Length}";
            }
            catch
            {
                txtBytes.Text = "发送失败";
                txtJSON.Text = "发送失败";
            }
        }

        private void txtPort_TextChanged(object sender, EventArgs e)
        {
            try
            {
                AppConfigSetting.SetStrVal("server",$"{txtIP.Text}:{txtPort.Text}");
            }
            catch { }

        }
    }
}