using BPASmartClient.Business;
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.咖啡机.Enum;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Threading;

namespace BPASmartClient.MorkT.ViewModel
{
    public class DebugViewModel : ObservableObject
    {
        /// <summary>
        /// 设备ID
        /// </summary>
        int DeviceId { get; set; }
        #region 乐白机器人
        /// <summary>
        /// 乐白机器人连接状态
        /// </summary>
        public string RobotConnected { get { return _robotConnected; } set { _robotConnected = value; OnPropertyChanged(); } }
        private string _robotConnected { get; set; }
        /// <summary>
        /// 乐白机器人的模式状态
        /// </summary>
        public ELebaiRModel RobotMode { get { return _robotMode; } set { _robotMode = value; OnPropertyChanged(); } }
        private ELebaiRModel _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 RelayCommand Button_MakeIceCreamCommand { get; set; }
        /// <summary>
        /// 选择冰淇淋模式
        /// </summary>
        public RelayCommand Button_SetIceCreamModelCommand { get; set; }

        /// <summary>
        /// 冰淇淋连接状态
        /// </summary>
        public string IceCreamConnected { get { return _iceCreamConnected; } set { _iceCreamConnected = value; OnPropertyChanged(); } }
        private string _iceCreamConnected { get; set; }
        /// <summary>
        /// 预冷温度
        /// </summary>
        public short YLWD { get { return _yLWD; } set { _yLWD = value; OnPropertyChanged(); } }
        private short _yLWD { get; set; }
        /// <summary>
        /// 回气温度
        /// </summary>
        public short HQWD { get { return _hQWD; } set { _hQWD = value; OnPropertyChanged(); } }
        private short _hQWD { get; set; }
        /// <summary>
        /// 环境温度
        /// </summary>
        public short HJWD { get { return _hJWD; } set { _hJWD = value; OnPropertyChanged(); } }
        private short _hJWD { get; set; }
        /// <summary>
        /// 电流
        /// </summary>
        public short DL { get { return _DL; } set { _DL = value; OnPropertyChanged(); } }
        private short _DL { get; set; }
        /// <summary>
        /// 电压
        /// </summary>
        public short DY { get { return _dy; } set { _dy = value; OnPropertyChanged(); } }
        private short _dy { get; set; }
        /// <summary>
        /// 当前模式
        /// </summary>
        public MORKI_MODE CurrentMode { get { return _CurrentMode; } set { _CurrentMode = value; OnPropertyChanged(); } }
        private MORKI_MODE _CurrentMode;
        /// <summary>
        /// 故障
        /// </summary>
        public MORKI_FAULT IceCreamFault { get { return _IceCreamFault; } set { _IceCreamFault = value; OnPropertyChanged(); } }
        private MORKI_FAULT _IceCreamFault { get; set; }
        /// <summary>
        /// 成型比
        /// </summary>  
        public byte CXB { get { return _cXB; } set { _cXB = value; OnPropertyChanged(); } }
        private byte _cXB { get; set; }
        /// <summary>
        /// 打料完成状态
        /// </summary>
        public string DLCompleted { get { return _dLCompleted; } set { _dLCompleted = value; OnPropertyChanged(); } }
        private string _dLCompleted { get; set; }

        public List<string> IceCreamModes { get; set; } = new List<string>();
        public string SelecteIceCreamdMode { get; set; }

        private void Button_MakeIceCream()
        {
            
        }
        private void Button_SetIceCreamModel()
        {
            MORKI_MODE mORKI_MODE = (MORKI_MODE)Enum.Parse(typeof(MORKI_MODE), SelecteIceCreamdMode);
            new GSIceCream_ModeSetEvent() { DeviceId = DeviceId, Mode = mORKI_MODE }.Publish();
        }
        #endregion

        #region 咖啡机
        /// <summary>
        /// 咖啡机连接状态
        /// </summary>
        public string CoffeeConnected { get { return _coffeeConnected; } set { _coffeeConnected = value; OnPropertyChanged(); } }
        private string _coffeeConnected { get; set; }
        /// <summary>
        /// 咖啡机状态
        /// </summary>
        public DrCoffeeStatus CoffeeStatus { get { return _coffeeStatus; } set { _coffeeStatus = value; OnPropertyChanged(); } }
        private DrCoffeeStatus _coffeeStatus { get; set; }
        /// <summary>
        /// 应用状态
        /// </summary>
        public DrCoffeeAppStatus AppStatus { get { return _appStatus; } set { _appStatus = value; OnPropertyChanged(); } }
        private DrCoffeeAppStatus _appStatus { get; set; }
        /// <summary>
        /// 告警
        /// </summary>
        public DrCoffeeWarning Warning { get { return _warning; } set { _warning = value; OnPropertyChanged(); } }
        public DrCoffeeWarning _warning { get; set; }
        /// <summary>
        /// 故障信息
        /// </summary>
        public DrCoffeeFault CaffeeFault { get { return _caffeeFault; } set { _caffeeFault = value; OnPropertyChanged(); } }
        public DrCoffeeFault _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; }
        #endregion


        Dictionary<string, object> CurrentData { get; set; }
  
        public DebugViewModel()
        {
            Button_RobotControlCommand = new RelayCommand<object>(Button_RobotControl);
            Button_MakeIceCreamCommand = new RelayCommand(Button_MakeIceCream);
            Button_SetIceCreamModelCommand = new RelayCommand(Button_SetIceCreamModel);

            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];

            foreach (MORKI_MODE code in Enum.GetValues(typeof(MORKI_MODE)))
            {
                IceCreamModes.Add(code.ToString());
            }
            SelecteIceCreamdMode = IceCreamModes[0];

           

            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices().ForEach(device =>
                {
                    CurrentData?.Clear();
                    if (device.Name == "MorkT") CurrentData = device.Status.GetStatus();
                    DeviceId = device.DeviceId;
                });
                if (CurrentData != null && CurrentData.Count != 0)
                {
                    RobotConnected = (bool)CurrentData["Lebai.LebaiRobot.IsConnected"] ? "已连接" : "未连接";
                    IceCreamConnected = (bool)CurrentData["GSIceCream.IceCreamMachine.IsConnected"] ? "已连接" : "未连接";
                    CoffeeConnected = (bool)CurrentData["GSIceCream.IceCreamMachine.IsConnected"] ? "已连接" : "未连接";

                    RobotMode = (ELebaiRModel)CurrentData["Lebai.LebaiRobot.RobotMode"];

                    CoffeeStatus = (DrCoffeeStatus)CurrentData["DRCoffee.CoffeeMachine.Status"];
                    AppStatus = (DrCoffeeAppStatus)CurrentData["DRCoffee.CoffeeMachine.AppStatus"];
                    Warning = (DrCoffeeWarning)CurrentData["DRCoffee.CoffeeMachine.Warning"];
                    CaffeeFault = (DrCoffeeFault)CurrentData["DRCoffee.CoffeeMachine.Fault"];

                    YLWD = (short)CurrentData["GSIceCream.IceCreamMachine.YLWD"];
                    HQWD = (short)CurrentData["GSIceCream.IceCreamMachine.HQWD"];
                    HJWD = (short)CurrentData["GSIceCream.IceCreamMachine.HJWD"];
                    DL = (short)CurrentData["GSIceCream.IceCreamMachine.DL"];
                    DY = (short)CurrentData["GSIceCream.IceCreamMachine.DY"];
                    CurrentMode = (MORKI_MODE)CurrentData["GSIceCream.IceCreamMachine.CurrentMode"];
                    IceCreamFault = (MORKI_FAULT)CurrentData["GSIceCream.IceCreamMachine.Fault"];
                    CXB = (byte)CurrentData["GSIceCream.IceCreamMachine.CXB"];
                    DLCompleted = (bool)CurrentData["GSIceCream.IceCreamMachine.DLCompleted"] ? "打料完成" : "打料中";
                }
                
                Thread.Sleep(500);
            }), "MorkT-状态刷新");
        }

    }
}
