using BPASmartClient.Business;
using BPASmartClient.Device;
using BPASmartClient.DRCoffee;
using BPASmartClient.EventBus;
using BPASmartClient.GSIceCream;
using BPASmartClient.Helper;
using BPASmartClient.LebaiRobot;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.乐白机器人;
using BPASmartClient.Model.冰淇淋.Enum;
using BPASmartClient.Model.单片机;
using BPASmartClient.Model.单片机.Enum;
using BPASmartClient.Model.咖啡机.Enum;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
namespace BPASmartClient.MorkTLebaiJC.ViewModel;
    public class DebugViewModel : ObservableObject
    {
        
        #region 乐白机器人
        /// <summary>
        /// 乐白机器人连接状态
        /// </summary>
        public string RobotConnected { get { return _robotConnected; } set { _robotConnected = value; OnPropertyChanged(); } }
        private string _robotConnected { get; set; }
        /// <summary>
        /// 乐白机器人的模式状态
        /// </summary>
        public string RobotMode { get { return _robotMode; } set { _robotMode = value; OnPropertyChanged(); } }
        private string _robotMode { get; set; }
        /// <summary>
        /// 机器人控制指令
        /// </summary>
        public RelayCommand<object> Button_RobotControlCommand { get; set; }
        /// <summary>
        /// 机器人控制
        /// </summary>
        /// <param name="o"></param>
        private void Button_RobotControl(object o)
        {
            EventBus.EventBus.GetInstance().Publish(new LebaiRobot_LebaiControlEvent { DeviceId = DeviceId, LebaiControl = o.ToString() });
        }

        #endregion

        #region 果汁机
        /// <summary>
        /// 果汁机的连接状态
        /// </summary>
        public string JuicerConnected { get { return _JuicerConnected; } set { _JuicerConnected = value; OnPropertyChanged(); } }
        private string _JuicerConnected { get; set; }
        
        /// <summary>
        /// 果汁机的状态
        /// </summary>
        public string JuicerState { get { return _JuicerState; } set { _JuicerState = value; OnPropertyChanged(); } }
        private string _JuicerState { get; set; }
        
        #endregion

        #region 咖啡机
        /// <summary>
        /// 咖啡机连接状态
            /// </summary>
        public string CoffeeConnected { get { return _coffeeConnected; } set { _coffeeConnected = value; OnPropertyChanged(); } }
        private string _coffeeConnected { get; set; }
        /// <summary>
        /// 咖啡机状态
        /// </summary>
        public string CoffeeStatus { get { return _coffeeStatus; } set { _coffeeStatus = value; OnPropertyChanged(); } }
        private string _coffeeStatus { get; set; }
        /// <summary>
        /// 应用状态
        /// </summary>
        public string AppStatus { get { return _appStatus; } set { _appStatus = value; OnPropertyChanged(); } }
        private string _appStatus { get; set; }
        /// <summary>
        /// 告警
        /// </summary>
        public string Warning { get { return _warning; } set { _warning = value; OnPropertyChanged(); } }
        public string _warning { get; set; }
        /// <summary>
        /// 故障信息
        /// </summary>
        public string CaffeeFault { get { return _caffeeFault; } set { _caffeeFault = value; OnPropertyChanged(); } }
        public string _caffeeFault { get; set; }

        public List<string> Coffees { get; set; } = new List<string>();
        public string SelectedCoffee { get; set; }

        public List<string> CoffeeCmds { get; set; } = new List<string>();
        public string SelectedCoffeeCmd { get; set; }
        /// <summary>
        /// 制作咖啡
        /// </summary>
        public RelayCommand Button_MakeCoffeeCommand { get; set; }
        /// <summary>
        /// 停止制作咖啡
        /// </summary>
        public RelayCommand Button_StopMakeCoffeCommand { get; set; }
        /// <summary>
        /// 咖啡模式设置
        /// </summary>
        public RelayCommand Button_CoffeeModeSetCommand { get; set; }

        private void Button_MakeCoffee()
        {
            EventBus.EventBus.GetInstance().Publish(new DRCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, DrinkCode = (DrCoffeeDrinksCode)Enum.Parse(typeof(DrCoffeeDrinksCode), SelectedCoffee)});
        }
        private void Button_StopMakeCoffe()
        {
            EventBus.EventBus.GetInstance().Publish(new DRCoffee_CancelMakeCoffeeEvent() { DeviceId = DeviceId });
        }
        private void Button_CoffeeModeSet()
        {
            EventBus.EventBus.GetInstance().Publish(new DRCoffee_CoffeeCommCmdEvent() { DeviceId = DeviceId, CommCmd = (DrCoffeeCommCmd)Enum.Parse(typeof(DrCoffeeCommCmd), SelectedCoffeeCmd)});
        }
        #endregion

        #region 单片机
        public string MCUConnected { get { return _mcuConnected; } set { _mcuConnected = value; OnPropertyChanged(); } }
        private string _mcuConnected { get; set; }

        public RelayCommand Button_MCUOuputCommand { get; set; }
        public RelayCommand Button_MCUDOuputCommand { get; set; }
        public RelayCommand Button_LebaiDOuputCommand { get; set; }
    
        public int PWMInputNumber { get { return _pwmInputNumber; } set { _pwmInputNumber = value; OnPropertyChanged(); } }
        private int _pwmInputNumber = 1;

        public int MCU_PWM_CH { get { return _choosePWM; } set { _choosePWM = value; OnPropertyChanged(); } }
        private int _choosePWM = 1;

        public int MCU_DO_CH { get { return _mcuDO_CH; } set { _mcuDO_CH = value; OnPropertyChanged(); } }
        private int _mcuDO_CH = 1;

        public int Lebai_DO_CH { get { return _lebaiDO_CH; } set { _lebaiDO_CH = value; OnPropertyChanged(); } }
        private int _lebaiDO_CH = 1;

        public int Lebai_DO_Value { get { return _lebaiDO_Value; } set { _lebaiDO_Value = value; OnPropertyChanged(); } }
        private int _lebaiDO_Value = 1;

        private int[] DICH  { get { return _diCH; } set { _diCH = value; OnPropertyChanged(); } }
        private int[] _diCH = new int[8];

        public bool MCU_DO_Value { get { return _mcu_DO_Valuer; } set { _mcu_DO_Valuer = value; OnPropertyChanged(); } }
        private bool _mcu_DO_Valuer = true;

        public bool MCU_DI0 { get { return _mcu_DI0; }set { _mcu_DI0 = value;OnPropertyChanged(); } }
        private bool _mcu_DI0 = true;
        public bool MCU_DI1 { get { return _mcu_DI1; } set { _mcu_DI1 = value; OnPropertyChanged(); } }
        private bool _mcu_DI1 = true;
        public bool MCU_DI2 { get { return _mcu_DI2; } set { _mcu_DI2 = value; OnPropertyChanged(); } }
        private bool _mcu_DI2 = true;
        public bool MCU_DI3 { get { return _mcu_DI3; } set { _mcu_DI3 = value; OnPropertyChanged(); } }
        private bool _mcu_DI3 = true;
        public bool MCU_DI4 { get { return _mcu_DI4; } set { _mcu_DI4 = value; OnPropertyChanged(); } }
        private bool _mcu_DI4 = true;
        public bool MCU_DI5 { get { return _mcu_DI5; } set { _mcu_DI5 = value; OnPropertyChanged(); } }
        private bool _mcu_DI5 = true;
        public bool MCU_DI6 { get { return _mcu_DI6; } set { _mcu_DI6 = value; OnPropertyChanged(); } }
        private bool _mcu_DI6 = true;
        public bool MCU_DI7 { get { return _mcu_DI7; } set { _mcu_DI7 = value; OnPropertyChanged(); } }
        private bool _mcu_DI7 = true;
    /// <summary>
    /// 单片机舵机输出
    /// </summary>
    private void Button_McuOutput() 
        {
            string sChoosePWM =Convert.ToString(MCU_PWM_CH+1);
            new WriteMcu() { TagName = "ServoControl", Address = sChoosePWM, Value = PWMInputNumber }.Publish();
        }
        /// <summary>
        /// 单片机DO输出
        /// </summary>
        private void Button_McuDOutput()
        {
            string sChooseDO = Convert.ToString(MCU_DO_CH + 1);
            new WriteMcu() { TagName = "OutputControl", Address = sChooseDO, Value = MCU_DO_Value }.Publish();
        }
        /// <summary>
        /// 乐白的DO输出
        /// </summary>
        private void Button_LebaiDOutput() 
        {
            int sChooseDO = Convert.ToInt16(Lebai_DO_CH);
            bool lebai_DO_Value = Convert.ToInt16(Lebai_DO_Value) == 0;
            new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Pin = sChooseDO, Value = lebai_DO_Value }.Publish();
        }
    #endregion

    /// <summary>
    /// 设备ID
    /// </summary>
    public int DeviceId { get; set; }
        /// <summary>
        /// 设备数据
        /// </summary>
        public ObservableCollection<VariableMonitor> variableMonitors { get; set; }
        
        public DebugViewModel()
        {
            Button_RobotControlCommand = new RelayCommand<object>(Button_RobotControl);
            Button_MakeCoffeeCommand = new RelayCommand(Button_MakeCoffee);
            Button_StopMakeCoffeCommand = new RelayCommand(Button_StopMakeCoffe);
            Button_CoffeeModeSetCommand = new RelayCommand(Button_CoffeeModeSet);

            Button_MCUOuputCommand = new RelayCommand(Button_McuOutput);
            Button_MCUDOuputCommand = new RelayCommand(Button_McuDOutput);
            Button_LebaiDOuputCommand = new RelayCommand(Button_LebaiDOutput);
        foreach (DrCoffeeDrinksCode code in Enum.GetValues(typeof(DrCoffeeDrinksCode)))
            {
                Coffees.Add(code.ToString());
            }
            SelectedCoffee = Coffees[0];
            foreach (DrCoffeeCommCmd code in Enum.GetValues(typeof(DrCoffeeCommCmd)))
            {
                CoffeeCmds.Add(code.ToString());
            }
            SelectedCoffeeCmd = CoffeeCmds[0];

            Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices().ForEach(device =>
            {
                if (device.Name == "MorkT") DeviceId = device.DeviceId
                ;
            });

            

            ThreadManage.GetInstance().StartLong(new Action(() =>
            {

                variableMonitors = Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.FirstOrDefault(p => p.DeviceId == DeviceId)?.variableMonitors;

                if (variableMonitors != null && variableMonitors.Count > 0)
                {
                    RobotConnected  = variableMonitors.FirstOrDefault(p=>p.Notes == "机器人连接状态").CurrentValue == "True" ? "已连接" : "未连接";
                    CoffeeConnected = variableMonitors.FirstOrDefault(p => p.Notes == "咖啡机连接状态").CurrentValue == "True" ? "已连接" : "未连接";
                    JuicerConnected = variableMonitors.FirstOrDefault(p => p.Notes == "果汁机连接状态").CurrentValue == "True" ? "已连接" : "未连接";
                    MCUConnected    = variableMonitors.FirstOrDefault(p => p.Notes == "单片机连接状态").CurrentValue == "True" ? "已连接" : "未连接";

                    RobotMode = variableMonitors.FirstOrDefault(p => p.Notes == "机器人状态").CurrentValue;

                    CoffeeStatus = variableMonitors.FirstOrDefault(p => p.Notes == "咖啡机状态").CurrentValue;
                    AppStatus = variableMonitors.FirstOrDefault(p => p.Notes == "咖啡机应用状态").CurrentValue;
                    Warning = variableMonitors.FirstOrDefault(p => p.Notes == "咖啡机告警").CurrentValue;
                    CaffeeFault = variableMonitors.FirstOrDefault(p => p.Notes == "咖啡机故障").CurrentValue;


                }
                Thread.Sleep(500);
            }), "MorkT-状态刷新");

        }
    }

