using GalaSoft.MvvmLight;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.WPF.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;

namespace SlnMesnac.WPF.ViewModel
{
    public class DeviceMonitorViewModel : ViewModelBase, IDisposable
    {
        private ISqlSugarClient? sqlSugarClient;
        private Timer RefreRfidTimer;
        private bool isProcessing = false;

        public DeviceMonitorViewModel()
        {
            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();

            RefreRfidTimer = new Timer(2000); // 2 秒间隔
            RefreRfidTimer.Elapsed += async (sender, e) => await OnTimedEventAsync(sender, e);
            RefreRfidTimer.AutoReset = true;
            RefreRfidTimer.Enabled = true;
        }

        #region 界面属性定义

        private ObservableCollection<RfidInfo> rfidList = new ObservableCollection<RfidInfo>();

        public ObservableCollection<RfidInfo> RfidList
        {
            get { return rfidList; }
            set
            {
                rfidList = value;
                RaisePropertyChanged(() => RfidList);
            }
        }

        #endregion 界面属性定义

        private async Task OnTimedEventAsync(Object source, ElapsedEventArgs e)
        {
            if (isProcessing)
            {
                return;
            }

            try
            {
                isProcessing = true;
                await LoadRfidDataAsync();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                isProcessing = false;
            }
        }

        private async Task LoadRfidDataAsync()
        {
            List<BaseRfidInfo> rfidInfos = await sqlSugarClient!.Queryable<BaseRfidInfo>().ToListAsync();

            await Application.Current.Dispatcher.InvokeAsync(() =>
            {
                //RfidList.Clear();
                foreach (BaseRfidInfo dbRfid in rfidInfos)
                {
                    RfidInfo? rfid = RfidList.FirstOrDefault(x => x.Name == dbRfid.Remark);
                    if (rfid == null)
                    {
                        rfid = new RfidInfo();
                        rfid.Name = dbRfid.Remark;
                        rfid.Ip = dbRfid.EquipIp;
                        rfid.IsOnline = dbRfid.IsOnline;
                        rfid.ImageSource = dbRfid.IsOnline == 1 ? "/Templates/image/rfid-green.png" : "/Templates/image/rfid-red.png";
                        RfidList.Add(rfid);
                    }
                    else
                    {
                        rfid.IsOnline = dbRfid.IsOnline;
                        rfid.ImageSource = dbRfid.IsOnline == 1 ? "/Templates/image/rfid-green.png" : "/Templates/image/rfid-red.png";
                    }
                }
            });
        }

        public void Dispose()
        {
            RefreRfidTimer?.Stop();
            RefreRfidTimer?.Dispose();
        }
    }
}