using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Lebai.SDK;
using Lebai.SDK.Dtos;
using Robotc;
using BPASmartClient.Message;
using BPASmartClient.Helper;
using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
using BPASmartClient.Peripheral;
using BPASmartClient.EventBus;
using static BPASmartClient.EventBus.EventBus;
using BPASmartClient.Model;
using BPASmartClient.LebaiRobot;
using BPASmartClient.Model.乐白机器人;

namespace BPASmartClient.Lebai
{
    public class LebaiRobot: BasePeripheral
    {

        /// <summary>
        /// 抓手上传感器输入位索引
        /// </summary>
        public int HandSensor { get; set; }
        /// <summary>
        /// 输出信号量索引
        /// </summary>
        public int OutputSingalValue { get; set; }

        protected override void InitStatus()
        {
            status["Connected"] = false;
            status["HandSensor"] = 0;
            status["OutputSingalValue"] = 0;
        }

        public override void Start()
        {
            LebaiHelper.GetInstance().Connect(communicationPar.IPAddress);
            ThreadManage.GetInstance().StartLong(() => { LebaiHelper.GetInstance().Reconnect(communicationPar.IPAddress); Thread.Sleep(5000); }, "机器人重连检测");
            ThreadManage.GetInstance().StartLong(() =>
            {
                IsConnected = LebaiHelper.GetInstance().IsConnected;
                status["OK"] = LebaiHelper.GetInstance().GetValueAsync().Ok;
                status["Value"] = LebaiHelper.GetInstance().GetValueAsync().Value;
                if (LebaiHelper.GetInstance().robotData != null) status["RobotMode"] = LebaiHelper.GetInstance().robotData.RobotMode.Mode;
                LebaiHelper.GetInstance().GetRobotModeStatus();
                Thread.Sleep(10);
            },"获取乐白机器人数据");

        }

        public override void Stop()
        {

        }

        public override void Init()
        {
            EventBus.EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack )
            {

            });
            //获取机器人信号
            EventBus.EventBus.GetInstance().Subscribe<LebaiRobot_GetInputEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                if (@event is LebaiRobot_GetInputEvent getInput)
                {
                    callBack.Invoke(LebaiHelper.GetInstance().GetInput(getInput.Pin));
                }
            });
            //获取Tcp信号
            EventBus.EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                if (@event is LebaiRobot_GetInputEvent getTCPInput)
                {
                    callBack.Invoke(LebaiHelper.GetInstance().GetTcpInput(getTCPInput.Pin));
                }
            });
            //机器人输入信号
            EventBus.EventBus.GetInstance().Subscribe<LebaiRobot_SetValueEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                if (@event is LebaiRobot_SetValueEvent SetValueEvent)
                {
                    LebaiHelper.GetInstance().SetValue(SetValueEvent.RobotSetValue);  
                }
            });
            //控制机器人
            EventBus.EventBus.GetInstance().Subscribe<LebaiRobot_LebaiControlEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                if (@event is LebaiRobot_LebaiControlEvent lebaiControlEvent)
                {
                    switch (lebaiControlEvent.LebaiControl)
                    {
                        case "机器人启动":
                            LebaiHelper.GetInstance().StartRobot();
                            break;
                        case "启动示教":
                            LebaiHelper.GetInstance().StartTeachMode();
                            break;
                        case "停止示教":
                            LebaiHelper.GetInstance().EndtTeachMode();
                            break;
                        case "机器人急停":
                            LebaiHelper.GetInstance().EStopRobot();
                            break;
                        default:
                            break;
                    }
                    
                }
            });
            //选择机器人场景
            EventBus.EventBus.GetInstance().Subscribe<LebaiRobot_LebaiSenceEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
            {
                if (@event == null) return;
                if (@event is LebaiRobot_LebaiSenceEvent lebaiSenceEvent)
                {
                    LebaiHelper.GetInstance().Scene(lebaiSenceEvent.LebaiSence);
                }
            });

            InitStatus();
            Start();
            MessageLog.GetInstance.Show("乐白机器人初始化完成");
        }

        
    }
}
