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.MorkT.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 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 string YLWD { get { return _yLWD; } set { _yLWD = value; OnPropertyChanged(); } }
        private string _yLWD { get; set; }
        /// <summary>
        /// 回气温度
        /// </summary>
        public string HQWD { get { return _hQWD; } set { _hQWD = value; OnPropertyChanged(); } }
        private string _hQWD { get; set; }
        /// <summary>
        /// 环境温度
        /// </summary>
        public string HJWD { get { return _hJWD; } set { _hJWD = value; OnPropertyChanged(); } }
        private string _hJWD { get; set; }
        /// <summary>
        /// 电流
        /// </summary>
        public string DL { get { return _DL; } set { _DL = value; OnPropertyChanged(); } }
        private string _DL { get; set; }
        /// <summary>
        /// 电压
        /// </summary>
        public string DY { get { return _dy; } set { _dy = value; OnPropertyChanged(); } }
        private string _dy { get; set; }
        /// <summary>
        /// 当前模式
        /// </summary>
        public string CurrentMode { get { return _CurrentMode; } set { _CurrentMode = value; OnPropertyChanged(); } }
        private string _CurrentMode;
        /// <summary>
        /// 故障
        /// </summary>
        public string IceCreamFault { get { return _IceCreamFault; } set { _IceCreamFault = value; OnPropertyChanged(); } }
        private string _IceCreamFault { get; set; }
        /// <summary>
        /// 成型比
        /// </summary>  
        public string CXB { get { return _cXB; } set { _cXB = value; OnPropertyChanged(); } }
        private string _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 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 单片机
        /// <summary>
        /// 咖啡杯或冰淇淋杯落杯
        /// </summary>
        public RelayCommand<object> Button_CupControlCommand { get; set; }
        /// <summary>
        /// 单片机冰淇淋机器制冷
        /// </summary>
        public RelayCommand Button_SCChipRefrigerationCommand { get; set; }
        /// <summary>
        /// 单片机冰淇淋机器打料
        /// </summary>
        public RelayCommand Button_SCChipDischargeCommand { get; set; }
        /// <summary>
        /// 模拟测试
        /// </summary>
        public RelayCommand Button_SCChipTestCommand { get; set; }

        private void Button_CupControl(object CupType)
        {
            EventBus.EventBus.GetInstance().Publish(new SCChip_TakeCupEvent() { DeviceId = DeviceId, Cup = (IC_CUP)Enum.Parse(typeof(IC_CUP), CupType.ToString()) });
        }

        private void Button_SCChipRefrigeration()
        {
            EventBus.EventBus.GetInstance().Publish(new SCChip_RotorSwitchEvent { DeviceId = DeviceId, TurnOn = true });
            Thread.Sleep(500);
            EventBus.EventBus.GetInstance().Publish(new SCChip_RotorSwitchEvent { DeviceId = DeviceId, TurnOn = false });
            
        }

        private void Button_SCChipDischarge()
        {
            EventBus.EventBus.GetInstance().Publish(new SCChip_MakeIceCreamEvent { DeviceId = DeviceId, SteeringEngine = IC_SE.SE_2 });
        }

        #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_MakeIceCreamCommand = new RelayCommand(Button_MakeIceCream);
            Button_SetIceCreamModelCommand = new RelayCommand(Button_SetIceCreamModel);

            Button_MakeCoffeeCommand = new RelayCommand(Button_MakeCoffee);
            Button_StopMakeCoffeCommand = new RelayCommand(Button_StopMakeCoffe);
            Button_CoffeeModeSetCommand = new RelayCommand(Button_CoffeeModeSet);

            Button_CupControlCommand = new RelayCommand<object>(Button_CupControl);
            Button_SCChipRefrigerationCommand = new RelayCommand(Button_SCChipRefrigeration);
            Button_SCChipDischargeCommand = new RelayCommand(Button_SCChipDischarge);
            Button_SCChipTestCommand = new RelayCommand(() =>
            {
                Button_SCChipRefrigeration();
                Thread.Sleep(50);
                Button_SCChipDischarge();
            });

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

            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" ? "已连接" : "未连接";
                    IceCreamConnected = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋连接状态").CurrentValue == "True" ? "已连接" : "未连接";
                    CoffeeConnected = 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;

                    YLWD = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器预冷温度").CurrentValue;
                    HQWD = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器回气温度").CurrentValue;
                    HJWD = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器环境温度").CurrentValue;
                    DL = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器电流").CurrentValue;
                    DY = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器电压").CurrentValue;
                    CurrentMode = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器模式").CurrentValue;
                    IceCreamFault = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器故障").CurrentValue;
                    CXB = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器成型比").CurrentValue;
                    DLCompleted = variableMonitors.FirstOrDefault(p => p.Notes == "冰淇淋机器打料完成")?.CurrentValue == "True" ? "打料完成" : "打料中";
                }
                
                Thread.Sleep(500);
            }), "MorkT-状态刷新");
        }

    }
}
