using NPOI.SS.Formula.Functions;
using NPOI.XSSF.Streaming.Values;
using Opc.Ua;
using OpcBasic;
using Robot.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace MesWork
{
    public class B_Read_N_AxisCard
    {
        /// <summary>
        /// 
        /// </summary>
        string sendStr_Temp = "-1";
        /// <summary>
        /// 
        /// </summary>
        string opName = "";
        /// <summary>
        /// 
        /// </summary>
        int axisNum = 1;
        /// <summary>
        /// 
        /// </summary>
        int interval = 100;
        /// <summary>
        /// 
        /// </summary>
        int intervalUpdate = 10000;        
        /// <summary>
        /// 
        /// </summary>
        public void Start(int interval, int intervalUpdate ,string opName, List<JointConfig> Joints)
        {
            this.axisNum = Joints.Count;
            this.interval = interval;
            this.intervalUpdate = intervalUpdate;
            this.opName = opName;
           

            new Thread(new ThreadStart(delegate () { while (true) { try {

                        var j = Read_N_Axis();
                        for (int i = 0; i < j.Length; i++)
                        {
                            Joints[i].DeviceCode = opName;
                            Joints[i].Value = j[i].ToString();
                        }
                        var sendStr = MesWorkForm.Move_Joint(Joints);
                        if (sendStr_Temp != sendStr)
                        {
                            MesWorkForm.SendMsg(sendStr);
                            MesWorkForm.S_C_B_Info_Fast(Joints);
                        }
                        sendStr_Temp = sendStr;
                    } catch (Exception err) { } Thread.Sleep(interval);
                } })).Start();

            new Thread(new ThreadStart(delegate () { while (true) { try {

                        var j = Read_N_Axis();
                        for (int i = 0; i < j.Length; i++)
                        {
                            Joints[i].DeviceCode = opName;
                            Joints[i].Value = j[i].ToString();
                        }
                        var sendStr = MesWorkForm.Move_Joint(Joints);
                        MesWorkForm.SendMsg(sendStr);
                        MesWorkForm.S_C_B_Info_Fast(Joints);
                    } catch (Exception err) { } Thread.Sleep(intervalUpdate);
                }})).Start();


        }


        //public double[] Read_N_Axis()
        //{
        //    double[] p = new double[this.axisNum];

        //    var x1 = Convert.ToDouble(MesWorkForm.ReadPLC(101, this.opName).ToString());
        //    var x2 = Convert.ToDouble(MesWorkForm.ReadPLC(102, this.opName).ToString());
        //    var y1 = Convert.ToDouble(MesWorkForm.ReadPLC(201, this.opName).ToString());
        //    var y2 = Convert.ToDouble(MesWorkForm.ReadPLC(202, this.opName).ToString());
        //    var z1 = Convert.ToDouble(MesWorkForm.ReadPLC(301, this.opName).ToString());
        //    var z2 = Convert.ToDouble(MesWorkForm.ReadPLC(302, this.opName).ToString());

        //    p[0] = x1 + x2 * 0.001;
        //    p[1] = y1 + y2 * 0.001;
        //    p[2] = z1 + z2 * 0.001;

        //    return p;
        //}

        public double[] Read_N_Axis()
        {
            double[] p = new double[this.axisNum];

            //var x1 = Convert.ToDouble(MesWorkForm.ReadPLC(101, this.opName).ToString());
            //var x2 = Convert.ToDouble(MesWorkForm.ReadPLC(102, this.opName).ToString());
            //var y1 = Convert.ToDouble(MesWorkForm.ReadPLC(201, this.opName).ToString());
            //var y2 = Convert.ToDouble(MesWorkForm.ReadPLC(202, this.opName).ToString());
            //var z1 = Convert.ToDouble(MesWorkForm.ReadPLC(301, this.opName).ToString());
            //var z2 = Convert.ToDouble(MesWorkForm.ReadPLC(302, this.opName).ToString());

            double x1 = 0;
            double x2 = 0;
            double y1 = 0;
            double y2 = 0;
            double z1 = 0;
            double z2 = 0;

            var jointArray = MesWorkForm.ReadPLC_GroupData(this.opName);
            foreach (KeyValuePair<string, object> item in jointArray)
            {
                var TF = (TagFormat)item.Value;
                var tagTypeCodeID = Convert.ToInt32(TF.TagTypeCodeID);
                var value = Convert.ToDouble(TF.TagValue);

                switch (tagTypeCodeID)
                {
                    case 101:
                        x1 = value;
                        break;
                    case 102:
                        x2 = value;
                        break;
                    case 201:
                        y1 = value;
                        break;
                    case 202:
                        y2 = value;
                        break;
                    case 301:
                        z1 = value;
                        break;
                    case 302:
                        z2 = value;
                        break;
                    default:
                        break;
                }
            }

            p[0] = x1 + x2 * 0.001;
            p[1] = y1 + y2 * 0.001;
            p[2] = z1 + z2 * 0.001;

            return p;
        }



    }
}
