using Common;
using CommunityToolkit.Mvvm.Input;
using MegaRobo.C00225155.ControlDevices;
using MegaRobo.Contract;
using MegaRobo.WpfInfrastructure.Abstractions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace MegaRobo.C00225155App.DevicesViewModels
{
    public class HighTemperatureShakeViewModel : ViewModelBaseEx
    {
        private ObservableCollection<string> loginfo = new ObservableCollection<string>();
        public ObservableCollection<string> LogInfos
        {
            get => loginfo;
            set => SetProperty(ref loginfo, value);
        }

        private List<int> deviceidList;
        /// <summary>
        /// 从站集合
        /// </summary>
        public List<int> DeviceIDList
        {
            get { return deviceidList; }
            set { SetProperty(ref deviceidList, value); }
        }

        private int deviceid;
        /// <summary>
        /// 设备从站号
        /// </summary>
        public int DeviceID
        {
            get { return deviceid; }
            set { SetProperty(ref deviceid, value); }
        }

        private int speed;
        /// <summary>
        /// 转速
        /// </summary>
        public int Speed
        {
            get { return speed; }
            set { SetProperty(ref speed, value); }
        }

        private int nowspeed;
        /// <summary>
        /// 当前转速
        /// </summary>
        public int NowSpeed
        {
            get { return nowspeed; }
            set { SetProperty(ref nowspeed, value); }
        }

        private int temp;
        /// <summary>
        /// 温度
        /// </summary>
        public int Temperature
        {
            get { return temp; }
            set { SetProperty(ref temp, value); }
        }

        private int nowtemp;
        /// <summary>
        /// 当前温度
        /// </summary>
        public int NowTemperature
        {
            get { return nowtemp; }
            set { SetProperty(ref nowtemp, value); }
        }

        private HighTemperatureShakeService _highTemperatureShakeService;
        private HighTemperatureMotorControlService _highTemperatureMotorControlService;
        public ICommand CommandControl { get; set; }

        public override void Initialize(ICoreService coreService, string title, params object[] args)
        {
            base.Initialize(coreService, title, args);
        }

        protected override void LoadServices()
        {
            base.LoadServices();
            _highTemperatureShakeService = this.CoreService.GetServiceInstance<HighTemperatureShakeService>();
            _highTemperatureMotorControlService = this.CoreService.GetServiceInstance<HighTemperatureMotorControlService>();
        }

        protected override async void LoadDatas()
        {
            base.LoadDatas();
            DeviceIDList = new List<int>();
            DeviceIDList.AddRange(new[] { 1, 2, 3, 4 });
        }

        protected override void Register()
        {
            base.Register();
            CommandControl = new RelayCommand<string>(ExecuteCommandControl);
        }

        private async void ExecuteCommandControl(string? parameter)
        {
            bool bresult = false;
            if (parameter != null)
            {
                switch (parameter)
                {
                    case "SetSpeed":
                        WriteLog($"设置搅拌速度{Speed}");
                        bresult = await _highTemperatureMotorControlService.SetMotorSpeed((byte)DeviceID, Speed);
                        if (bresult) WriteLog($"设置搅拌速度成功");
                        else WriteLog($"设置搅拌速度失败");
                        break;
                    case "SetTemperature":
                        WriteLog($"设置温度{Temperature}");
                        bresult = await _highTemperatureShakeService.SetTemperature(Temperature, (byte)DeviceID, false);
                        if (bresult) WriteLog($"设置温度成功");
                        else WriteLog($"设置温度失败");
                        break;
                    case "GetSpeed":
                        NowSpeed = await _highTemperatureMotorControlService.GetMotorActualSpeed((byte)DeviceID);
                        WriteLog($"获取到转速：{NowSpeed}");
                        break;
                    case "GetTemperature":
                        NowTemperature = await _highTemperatureShakeService.GetActualTemperature((byte)DeviceID);
                        WriteLog($"获取到温度：{NowTemperature}");
                        break;
                    case "CloseTemperature":
                        bresult = await _highTemperatureShakeService.SetTemperature(0, (byte)DeviceID, true);
                        if (bresult) WriteLog($"关闭温度成功");
                        else WriteLog($"关闭温度失败");
                        break;
                }
            }
        }

        private void WriteLog(string info)
        {
            ObservableCollection<string> infoss = File_Operator.DeepCopy(LogInfos);
            if (infoss.Count > 300) infoss.Clear();
            infoss.Insert(0, $"{DateTime.Now}---{info}");
            LogInfos = File_Operator.DeepCopy(infoss);
        }
    }
}
