using BPASmartClient.Business;
using BPASmartClient.Helper;
using BPASmartClient.Model.乐白机器人;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace BPASmartClient.MorkT.ViewModel
{

    public class MonitorViewModel: ObservableObject
    {
        #region 传感器
        /// <summary>
        /// 机器人夹爪传感器
        /// </summary>
        public bool RobotSenser { get { return _robotSenser; } set { _robotSenser = value; OnPropertyChanged(); } }
        private bool _robotSenser;
        /// <summary>
        /// 冰淇淋出口传感器
        /// </summary>
        public bool IceCreamSenser { get { return _iceCreamSenser; } set { _iceCreamSenser = value; OnPropertyChanged(); } }
        private bool _iceCreamSenser;
        /// <summary>
        /// 取餐口检测传感器
        /// </summary>
        public bool TakeMealSenser { get { return _takeMealSenser; } set { _takeMealSenser = value; OnPropertyChanged(); } }
        private bool _takeMealSenser;

        #endregion


        /// <summary>
        /// 设备ID
        /// </summary>
        int DeviceId;
        public MonitorViewModel()
        {
            Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices().ForEach(device =>
            {
                if (device.Name == "MorkT") DeviceId = device.DeviceId;

            });
            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                EventBus.EventBus.GetInstance().Publish(new LebaiRobot_GetTCPInputEvent() { DeviceId = DeviceId, Pin = 1 },(res)=>
                {
                    if(res != null&& res.Length>0&& res[0] is bool b)
                    {
                        RobotSenser = b;
                    }
                });

                EventBus.EventBus.GetInstance().Publish(new LebaiRobot_GetInputEvent() { DeviceId = DeviceId, Pin = 0 }, (res) =>
                {
                    if (res != null && res.Length > 0 && res[0] is bool b)
                    {
                        TakeMealSenser = b;
                    }
                });

                EventBus.EventBus.GetInstance().Publish(new LebaiRobot_GetTCPInputEvent() { DeviceId = DeviceId, Pin = 3 }, (res) =>
                {
                    if (res != null && res.Length > 0 && res[0] is bool b)
                    {
                        IceCreamSenser = b;
                    }
                });
                Thread.Sleep(500);
            }), "MorkT-传感器监视");
        }
    }

    public class BoolToColorConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool) value? new SolidColorBrush(Color.FromRgb(144, 238, 144)) : new SolidColorBrush(Color.FromRgb(178, 34, 34));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
