using Common;
using CommunityToolkit.Mvvm.Input;
using MegaRobo.C00225155.ControlDevices;
using MegaRobo.C00225155.ControlDevices.Scanner;
using MegaRobo.C00225155.Entities;
using MegaRobo.Contract;
using MegaRobo.WpfInfrastructure.Abstractions;
using NetTaste;
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 Weight_TS02_ViewModel : ViewModelBaseEx
    {
        private ObservableCollection<int> baudrateList;
        public ObservableCollection<int> BaudRateList
        {
            get { return baudrateList; }
            set { SetProperty(ref baudrateList, value); }
        }

        private ObservableCollection<string> loginfo = new ObservableCollection<string>();
        public ObservableCollection<string> LogInfos
        {
            get => loginfo;
            set => SetProperty(ref loginfo, value);
        }


        private int address = 70;
        public int Address
        {
            get { return address; }
            set { SetProperty(ref address, value); }
        }

        private double weight;
        public double WeightValue
        {
            get { return weight; }
            set { SetProperty(ref weight, value); }
        }

        private int baudrate;
        public int BaudRate
        {
            get { return baudrate; }
            set { SetProperty(ref baudrate, value); }
        }

        private string scan_powder = string.Empty;
        public string Scann_Powder
        {
            get { return scan_powder; }
            set { SetProperty(ref scan_powder, value); }
        }

        private string scan_dose = string.Empty;
        public string Scann_Dose
        {
            get { return scan_dose; }
            set { SetProperty(ref scan_dose, value); }
        }

        private ushort decimelNumber = 1;
        public ushort DecimelNumber
        {
            get { return decimelNumber; }
            set { SetProperty(ref decimelNumber, value); }
        }

        private string scan_react = string.Empty;
        public string Scann_React
        {
            get { return scan_react; }
            set { SetProperty(ref scan_react, value); }
        }

        private string scan_reactrack = string.Empty;
        public string Scann_ReactRack
        {
            get { return scan_reactrack; }
            set { SetProperty(ref scan_reactrack, value); }
        }

        private WeightService_Ts02 WeightService_ts;

        private ScannerService scanner_Dose;
        private ScannerService scanner_Powder;
        private ScannerService scanner_React;
        private ScannerService scanner_ReactRack;

        public IAsyncRelayCommand OperateCommand { get; set; }

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

        protected override void LoadServices()
        {
            base.LoadServices();
            WeightService_ts = this.CoreService.GetServiceInstance<WeightService_Ts02>(DeviceNames.PreWeight.ToString());
            scanner_Dose = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner.ToString());
            scanner_Powder = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner_Powder.ToString());
            //scanner_React = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner_React.ToString());
            //scanner_ReactRack = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner_ReactRack.ToString());
        }

        protected override async void LoadDatas()
        {
            base.LoadDatas();
            BaudRateList = new ObservableCollection<int>();
            BaudRateList.Add(9600); BaudRateList.Add(19200); BaudRateList.Add(38400); BaudRateList.Add(115200);
        }

        protected override void Register()
        {
            base.Register();
            OperateCommand = new AsyncRelayCommand<string>(OperateWeightDevice);
        }


        private async Task OperateWeightDevice(string parameter)
        {
            bool res = false;
            switch (parameter)
            {
                case "ReadWeight":
                    WeightValue = await WeightService_ts.ReadWeightValue();
                    WriteLog($"当前重量{WeightValue}");
                    break;
                case "Zero":
                    res = await WeightService_ts.ResetZero();
                    if (res)
                    {
                        WriteLog($"清零成功");
                    }
                    else
                    {
                        WriteLog($"清零失败");
                    }
                    break;
                case "Peel":
                    res = await WeightService_ts.Peel();
                    if (res)
                    {
                        WriteLog($"去皮成功");
                    }
                    else
                    {
                        WriteLog($"去皮失败");
                    }
                    break;
                case "SetDecimelNumber":
                    res = await WeightService_ts.SetDecimalPoint(DecimelNumber);
                    if (res)
                    {
                        WriteLog($"设置成功");
                    }
                    else
                    {
                        WriteLog($"设置失败");
                    }
                    break;

                case "scan_dose":
                    Scann_Dose = await scanner_Dose.ReadAsnyc(TimeSpan.FromSeconds(5));
                    break;

                case "scan_powder":
                    Scann_Powder = await scanner_Powder.ReadAsnyc(TimeSpan.FromSeconds(5));
                    break;
                case "scan_react":
                    Scann_React = await scanner_React.ReadAsnyc(TimeSpan.FromSeconds(5));
                    break;
                case "scan_reactrack":
                    Scann_ReactRack = await scanner_ReactRack.ReadAsnyc(TimeSpan.FromSeconds(5));
                    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);
        }
    }
}
