using MesWork;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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 DT_YQJF_DC_SLN
{
    public partial class F_RobotMonitor : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public string Curr_RobotKey = "";
        public string Curr_RobotId = "";
        string[] AxisString = new string[] { "x", "y", "z" };
        string[] FilpString = new string[] { "1", "-1" };
        /// <summary>
        /// 
        /// </summary>
        List<JointConfig> jointsCurrent = new List<JointConfig>();
        List<DiyJoint> diyJoint = new List<DiyJoint>();
        
        public F_RobotMonitor()
        {
            InitializeComponent();
            InitComBoxVale();
            foreach (var item in MesWorkForm.dc_Robot.Keys)
            {
                lb_Robot.Items.Add(item);
            }

            if (MesWorkForm.IsDemoMesServer == 1)
            {
                panel_Debug_Btn.Visible = true;
                panel_Debug_Txt.Visible = true;
            }
            else
            {
                panel_Debug_Btn.Visible = false;
                panel_Debug_Txt.Visible = false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lb_Robot_SelectedIndexChanged(object sender, EventArgs e)
        {

            label19_currentId.Text = "";
            Curr_RobotId = "";
            var selectedItem = lb_Robot.SelectedItem;
            if (selectedItem == null)
            {
                return;
            }



            jointsCurrent = new List<JointConfig>();
            Curr_RobotKey = selectedItem.ToString();
            GetRobot(Curr_RobotKey, out string type, out string robotCode, out object robotObj);
            double[] j = null;
            
            switch (type.ToUpper())
            {
                case "ROBOTFANUC":
                    var fanuc = (B_Robot_Fanuc)robotObj;
                    j = fanuc.j;
                    label19_currentId.Text = fanuc.Id;
                    Curr_RobotId = fanuc.Id;
                    break;
                case "ROBOTKUKA":
                    var kuka = (B_Robot_Kuka)robotObj;
                    jointsCurrent = kuka.Joints;
                    label19_currentId.Text = kuka.Id;
                    Curr_RobotId = kuka.Id;
                    j = kuka.j;
                    break;
                case "ROBOTABB":
                    var abb = (B_Robot_Abb)robotObj;
                    jointsCurrent = abb.Joints;
                    label19_currentId.Text = abb.Id;
                    Curr_RobotId = abb.Id;
                    j = abb.j;
                    break; 
                case "ROBOTEPSON":
                    var espon = (B_Robot_Espon)robotObj;
                    jointsCurrent = espon.Joints;
                    label19_currentId.Text = espon.Id;
                    Curr_RobotId = espon.Id;
                    j = espon.j;
                    break; 
                case "CNCEDM":
                    var edm = (B_Cnc_Edm)robotObj;
                    jointsCurrent = edm.Joints;
                    label19_currentId.Text = edm.Id;
                    Curr_RobotId = edm.Id;
                    j = edm.machine_j;
                    break;
                default:
                    break;
            }
            button1.Enabled = true;
            button2save.Enabled = true;

            ShowJoint(j);
            ShowJointDT(jointsCurrent);

            txt_Detail.Text = JObject.Parse(JsonConvert.SerializeObject(robotObj)).ToString();

        }

        private void GetRobot(string robotKey,out string type, out string robotCode,out object robotObj)
        {
            type = "";
            robotCode = "";
            robotObj = null;

            try
            {
                type = robotKey.Split('|')[0];
                robotCode = robotKey.Split('|')[1];
                robotObj = MesWorkForm.dc_Robot[robotKey];
            }
            catch (Exception err)
            {
            }

        }
        private void InitComBoxVale() {

            foreach (string item in AxisString)
            {
                comboBox1_Axis_J1.Items.Add(item);
                comboBox1_Axis_J2.Items.Add(item);
                comboBox1_Axis_J3.Items.Add(item);
                comboBox1_Axis_J4.Items.Add(item);
                comboBox1_Axis_J5.Items.Add(item);
                comboBox1_Axis_J6.Items.Add(item);
            }
            foreach (string item in FilpString)
            {
                comboBox1_Flip_J1.Items.Add(item);
                comboBox1_Flip_J2.Items.Add(item);
                comboBox1_Flip_J3.Items.Add(item);
                comboBox1_Flip_J4.Items.Add(item);
                comboBox1_Flip_J5.Items.Add(item);
                comboBox1_Flip_J6.Items.Add(item);
            }
        }



        private void ShowJoint(double[] j)
        {
            txt_J1.Text = j[0].ToString();
            txt_J2.Text = j[1].ToString();
            txt_J3.Text = j[2].ToString();
            txt_J4.Text = j[3].ToString();
            txt_J5.Text = j[4].ToString();
            txt_J6.Text = j[5].ToString();
            txt_J7.Text = j[6].ToString();
            txt_J8.Text = j[7].ToString();
            txt_J9.Text = j[8].ToString();
            txt_J10.Text = j[9].ToString();
            txt_J11.Text = j[10].ToString();
            txt_J12.Text = j[11].ToString();
        }
        private void ShowJointDT(List<JointConfig> joints)
        {
            if (joints == null)
            {
                return;
            }

            for (int i = 0; i < joints.Count; i++)
            {
                diyJoint.Add(new DiyJoint()
                {
                    Axis = joints[i].Axis,
                    Delta = joints[i].Delta,
                    Flip = joints[i].Flip,
                    ModelCode = joints[i].ModelCode,
                });
            }
            comboBox1_Axis_J1.Text = joints[0].Axis.ToString();
            comboBox1_Axis_J2.Text = joints[1].Axis.ToString();
            comboBox1_Axis_J3.Text = joints[2].Axis.ToString();
            comboBox1_Axis_J4.Text = joints[3].Axis.ToString();
            comboBox1_Axis_J5.Text = joints[4].Axis.ToString();
            comboBox1_Axis_J6.Text = joints[5].Axis.ToString();

            comboBox1_Flip_J1.Text = joints[0].Flip.ToString();
            comboBox1_Flip_J2.Text = joints[1].Flip.ToString();
            comboBox1_Flip_J3.Text = joints[2].Flip.ToString();
            comboBox1_Flip_J4.Text = joints[3].Flip.ToString();
            comboBox1_Flip_J5.Text = joints[4].Flip.ToString();
            comboBox1_Flip_J6.Text = joints[5].Flip.ToString();

            numericUpDown_j1.Text = joints[0].Delta.ToString();
            numericUpDown_j2.Text = joints[1].Delta.ToString();
            numericUpDown_j3.Text = joints[2].Delta.ToString();
            numericUpDown_j4.Text = joints[3].Delta.ToString();
            numericUpDown_j5.Text = joints[4].Delta.ToString();
            numericUpDown_j6.Text = joints[5].Delta.ToString();

        }

        private void btn_Fill_Joint_Click(object sender, EventArgs e)
        {
            txt_Set_J1.Text = txt_J1.Text;
            txt_Set_J2.Text = txt_J2.Text;
            txt_Set_J3.Text = txt_J3.Text;
            txt_Set_J4.Text = txt_J4.Text;
            txt_Set_J5.Text = txt_J5.Text;
            txt_Set_J6.Text = txt_J6.Text;
            txt_Set_J7.Text = txt_J7.Text;
            txt_Set_J8.Text = txt_J8.Text;
            txt_Set_J9.Text = txt_J9.Text;
            txt_Set_J10.Text = txt_J10.Text;
            txt_Set_J11.Text = txt_J11.Text;
            txt_Set_J12.Text = txt_J12.Text;
        }
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            var jSet = new double[12];
            jSet[0] = Convert.ToDouble(txt_Set_J1.Text);
            jSet[1] = Convert.ToDouble(txt_Set_J2.Text);
            jSet[2] = Convert.ToDouble(txt_Set_J3.Text);
            jSet[3] = Convert.ToDouble(txt_Set_J4.Text);
            jSet[4] = Convert.ToDouble(txt_Set_J5.Text);
            jSet[5] = Convert.ToDouble(txt_Set_J6.Text);
            jSet[6] = Convert.ToDouble(txt_Set_J7.Text);
            jSet[7] = Convert.ToDouble(txt_Set_J8.Text);
            jSet[8] = Convert.ToDouble(txt_Set_J9.Text);
            jSet[9] = Convert.ToDouble(txt_Set_J10.Text);
            jSet[10] = Convert.ToDouble(txt_Set_J11.Text);
            jSet[11] = Convert.ToDouble(txt_Set_J12.Text);

            GetRobot(Curr_RobotKey, out string type, out string robotCode, out object robotObj);
            switch (type)
            {
                case "ROBOTFANUC":
                    var fanuc = (B_Robot_Fanuc)robotObj;
                    fanuc.jDemo = jSet;
                    break;
                case "ROBOTKUKA":
                    var kuka = (B_Robot_Kuka)robotObj;
                    kuka.jDemo = jSet;
                    break;
                case "ROBOTABB":
                    var abb = (B_Robot_Abb)robotObj;
                    abb.jDemo = jSet;
                    break;  
                case "ROBOTEPSON":
                    var espon = (B_Robot_Espon)robotObj;
                    espon.jDemo = jSet;
                    break;
                default:
                    break;
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(Curr_RobotId == "")
            {
                MessageBox.Show("请选择机器人");
            }
            label19_apply.Text = "";
            try
            {
                jointsCurrent[0].Delta = numericUpDown_j1.Text;
                jointsCurrent[1].Delta = numericUpDown_j2.Text;
                jointsCurrent[2].Delta = numericUpDown_j3.Text;
                jointsCurrent[3].Delta = numericUpDown_j4.Text;
                jointsCurrent[4].Delta = numericUpDown_j5.Text;
                jointsCurrent[5].Delta = numericUpDown_j6.Text;

                jointsCurrent[0].Axis = comboBox1_Axis_J1.Text;
                jointsCurrent[1].Axis = comboBox1_Axis_J2.Text;
                jointsCurrent[2].Axis = comboBox1_Axis_J3.Text;
                jointsCurrent[3].Axis = comboBox1_Axis_J4.Text;
                jointsCurrent[4].Axis = comboBox1_Axis_J5.Text;
                jointsCurrent[5].Axis = comboBox1_Axis_J6.Text;

                jointsCurrent[0].Flip = comboBox1_Flip_J1.Text;
                jointsCurrent[1].Flip = comboBox1_Flip_J2.Text;
                jointsCurrent[2].Flip = comboBox1_Flip_J3.Text;
                jointsCurrent[3].Flip = comboBox1_Flip_J4.Text;
                jointsCurrent[4].Flip = comboBox1_Flip_J5.Text;
                jointsCurrent[5].Flip = comboBox1_Flip_J6.Text;
                label19_apply.Text = "应用成功！\r\n等待数据发送更新";
            }
            catch (Exception err )
            {
                label19_apply.Text = err.Message;
            }
        }

        private void button2save_Click(object sender, EventArgs e)
        {
            if (Curr_RobotId == "")
            {
                MessageBox.Show("请选择机器人");
            }
            ScadaConfig scadaConfig = new ScadaConfig();
            scadaConfig.MachineTool = new List<MachineToolConfig>();
            for (int i = 0; i < MesWorkForm.scadaConfig.MachineTool.Count; i++)
            {
                MachineToolConfig machineToolConfig = MesWorkForm.scadaConfig.MachineTool[i];
                string Id = machineToolConfig.Id;
                if (Id == Curr_RobotId)
                {
                    machineToolConfig.Joints = jointsCurrent;
                }
                scadaConfig.MachineTool.Add(machineToolConfig);
            }

            string scadaConfigStr = JsonConvert.SerializeObject(scadaConfig);
            System.IO.File.WriteAllText(@"config.json", scadaConfigStr);
            MessageBox.Show("保存成功");
        }
    }


    public class DiyJoint
    {
        public string ModelCode { get; set; }
        public string Axis { get; set; }
        public string Flip { get; set; }
        public string Delta { get; set; }
    }
}
