using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MegaRobo.C00225155.ControlDevices;
using MegaRobo.C00225155.DataAccess;
using MegaRobo.Contract;
using MegaRobo.WpfInfrastructure.Abstractions;
using Microsoft.Xaml.Behaviors.Core;
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 LowTemperatureMagneticStirViewModel : ViewModelBaseEx
    {
        private List<int> deviceidList;

        public List<int> DeviceIDList
        {
            get { return deviceidList; }
            set { SetProperty(ref deviceidList, value); }
        }


        private ObservableCollection<string> loginfo = new ObservableCollection<string>();
        public ObservableCollection<string> LogInfos
        {
            get => loginfo;
            set => SetProperty(ref loginfo, 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 int hottemp;
        /// <summary>
        /// 加热温度（用于除霜）
        /// </summary>
        public int HotTemperature
        {
            get { return hottemp; }
            set { SetProperty(ref hottemp, value); }
        }


        private string lidState;
        /// <summary>
        /// 开关盖状态
        /// </summary>
        public string LidState
        {
            get { return lidState; }
            set { SetProperty(ref lidState, value); }
        }


        private LowTemperatureMagneticStirService _lowTempMagneticStirService;

        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();
            _lowTempMagneticStirService = this.CoreService.GetServiceInstance<LowTemperatureMagneticStirService>();
        }

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

        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 _lowTempMagneticStirService.SetMotorSpeed(Speed, (byte)DeviceID);
                        if(bresult)WriteLog($"设置搅拌速度成功");
                        else WriteLog($"设置搅拌速度失败");
                        break;
                    case "SetTemperature":
                        WriteLog($"设置温度{Temperature}");
                        bresult = await _lowTempMagneticStirService.SetTemperature(Temperature, (byte)DeviceID,false);
                        if (bresult) WriteLog($"设置温度成功");
                        else WriteLog($"设置温度失败");
                        break;
                    case "SetHotTemperature":
                        WriteLog($"设置加热温度{HotTemperature}");
                        bresult = await _lowTempMagneticStirService.SetTemperature(HotTemperature, (byte)DeviceID, false);
                        if (bresult) WriteLog($"设置加热温度成功");
                        else WriteLog($"设置加热温度失败");
                        break;
                    case "GetSpeed":
                        NowSpeed = await _lowTempMagneticStirService.GetActualMotorSpeed((byte)DeviceID);
                        WriteLog($"获取到转速：{NowSpeed}");
                        break;
                    case "GetTemperature":
                        NowTemperature = await _lowTempMagneticStirService.GetActualTemperature((byte)DeviceID);
                        WriteLog($"获取到温度：{NowTemperature}");
                        break;
                    case "GetLidState":
                        LidState = await _lowTempMagneticStirService.GetLidStatus((byte)DeviceID);
                        WriteLog($"获取开关盖状态：{LidState}");
                        break;
                    case "CloseTemperature":
                        bresult = await _lowTempMagneticStirService.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);
        }

    }
}
