using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HBLConsole.Service;
using Lebai.SDK;
using Lebai.SDK.Dtos;
using Robotc;

namespace HBLConsole.Communication
{
    public class LebaiHelper
    {

        private volatile static LebaiHelper _Instance;
        public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper());
        private LebaiHelper() { }
        private LebaiRobotClient client;
        private RobotData robotData;
        public bool IsConnected { get { return null != robotData; } }

        public void Connect(string ip)
        {
            bool ErrorFlag = false;
            while (robotData == null)
            {
                try
                {
                    client = new LebaiRobotClient(ip);
                    robotData = client.GetRobotData().Result;
                }
                catch (Exception ex)
                {
                    if (!ErrorFlag)
                    {
                        MessageLog.GetInstance.Show(ex.ToString());
                        ErrorFlag = true;
                    }
                    Thread.Sleep(3000);
                }
            }
            StartRobot();
            MessageLog.GetInstance.Show("乐百机器人连接成功！");
        }


        public void GetSpeed()
        {
            var aa = client.GetActualTcpSpeed().GetAwaiter();
          

        }

        /// <summary>
        /// 获取机器人当前运行的命令
        /// </summary>
        /// <returns></returns>
        public CmdStatus GetRobotRunCmd()
        {
            if (robotData == null) return default(CmdStatus);
            return robotData.CurrentCmd;
        }

        /// <summary>
        /// 启动机器人
        /// </summary>
        public async void StartRobot()
        {
            if (robotData != null)
            {
                await client.StartSys();
                await client.Sync();
                MessageLog.GetInstance.Show("机器人启动成功");
            }

        }

        /// <summary>
        /// 获取抓手重量
        /// </summary>
        /// <returns></returns>
        public double GetClawWdight()
        {
            if (robotData != null)
            {
                return client.GetClawWeight().Result.Weight_;
            }
            return 0;
        }



        /// <summary>
        /// 获取信号量
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public SignalResult GetValueAsync(int index = 0)
        {
            if (robotData == null) return default(SignalResult);
            SignalValue signalValue = new SignalValue();
            signalValue.Index = index;
            return client?.GetSignal(signalValue).Result;
        }

        /// <summary>
        /// 设置信号量
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public SignalResult SetValue(int value, int index = 0)
        {
            if (robotData == null) return default(SignalResult);
            SignalValue signalValue = new SignalValue();
            signalValue.Index = index;
            signalValue.Value = value;
            return client.SetSignal(signalValue).Result;
        }

        /// <summary>
        /// 运行指定的场景
        /// </summary>
        /// <param name="id"></param>
        public void Scene(int id)
        {
            if (robotData == null) return;
            client?.RunScene(id);
        }
    }
}
