using Opc.Ua;
using OpcBasic;
using Robot.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace MesWork
{
    public partial class Temp { }
    public partial class B_Read_N_Axis
    {
        /// <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>
        string Id = "";
        /// <summary>
        /// 
        /// </summary>
        string Name = "";
        /// <summary>
        /// 
        /// </summary>
        public void Start(int interval, int intervalUpdate ,string opName, List<JointConfig> Joints, string Id, string Name)
        {
            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(out bool success);
                        if (success == false)
                        {
                            continue;
                        }
                        //if (j.Select(it => it == 0).Count() == j.Length)
                        //{
                        //    continue;
                        //}
                        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);
                            MesWorkForm.WriteLog(sendStr);
                        }
                        sendStr_Temp = sendStr;
                    } catch (Exception err) { } Thread.Sleep(interval);
                } })).Start();

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

                        var j = Read_N_Axis(out bool success);
                        if (success == false)
                        {
                            continue;
                        }
                        //if (j.Select(it => it == 0).Count() == j.Length)
                        //{
                        //    continue;
                        //}
                        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);
                        MesWorkForm.WriteLog(sendStr);
                    } catch (Exception err) { } Thread.Sleep(intervalUpdate);
                }})).Start();

            new Thread(new ThreadStart(delegate ()
            {
                while (true)
                {
                    var deviceInfo = new DeviceInfo();
                    deviceInfo.order = "";
                    deviceInfo.Name = Name;
                    deviceInfo.Id = Id;

                    deviceInfo.Axis = new List<AxisInfo>();
                    deviceInfo.OtherParameter = new List<ParameterInfo>();

                    var j = Read_N_Axis(out bool success);

                    if (success == false)
                    {
                        continue;
                    }
                    //if (j.Select(it => it == 0).Count() == j.Length)
                    //{
                    //    continue;
                    //}

                    deviceInfo.Axis.Add(new AxisInfo() { AxisName = "J1", AxisValue = Math.Round(j[0], 2).ToString() });
                    deviceInfo.Axis.Add(new AxisInfo() { AxisName = "J2", AxisValue = Math.Round(j[1], 2).ToString() });
                    deviceInfo.Axis.Add(new AxisInfo() { AxisName = "J3", AxisValue = Math.Round(j[2], 2).ToString() });
                    deviceInfo.Axis.Add(new AxisInfo() { AxisName = "J4", AxisValue = Math.Round(j[3], 2).ToString() });

                    var payloadInfoStr = MesWorkForm.Make_DeviceInfo(new List<DeviceInfo>() { deviceInfo });

                    MesWorkForm.SendMsg(payloadInfoStr);

                    Thread.Sleep(300);
                }
            }))
            { IsBackground = true }.Start();


        }


        public double[] Read_N_Axis(out bool success)
        {
            success = true;
            double[] p = new double[this.axisNum];

            var jointArray = MesWorkForm.ReadPLC_GroupData(this.opName);
            if (axisNum != jointArray.Count)
            {
                return p;
            }
            var listFalse = new List<bool>();
            foreach (KeyValuePair<string, object> item in jointArray)
            {
                var TF = (TagFormat)item.Value;
                var index = Convert.ToInt32(TF.ShaftID);
                var value = Convert.ToDouble(TF.TagValue);
                switch (value)
                {
                    case -1:
                    case -2:
                    case -5:
                    case 100:
                        listFalse.Add(false);
                        break;
                    default:
                        break;
                }
                
                p[index] = value;
            }
            if (listFalse.Count() > 0)
            {
                success = false;
                return p;
            }

            return p;
        }


       

    }
}
