using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Threading;
using BPA.Message;
using System.Windows;
using Microsoft.Toolkit.Mvvm.Input;
using System.Reflection;
using HBLConsole.Service;
using HBLConsole.GVL;
using HBLConsole.Model;
using HBLConsole.Business;
using HBLConsole.Factory;
using HBLConsole.Attributes;

namespace HBLConsole.ViewModel
{
    internal class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            MqttReceive();
            DoNavChanged("MessageLogView");
            NavChangedCommand = new RelayCommand<object>(DoNavChanged);
            ThreadManage.GetInstance.StartLong(new Action(() =>
            {
                InternetInfo.NetworkConnectState = UniversalHelper.GetInstance.GetNetworkState();
                NetworkConnectState = InternetInfo.NetworkConnectState;
                OrderCount = orderStatusLists.Count;
                Thread.Sleep(1000);
            }), "界面状态监控");
            AlarmMonitoring();
        }

        private void AlarmMonitoring()
        {
            ThreadManage.GetInstance.StartLong(new Action(() =>
            {
                if (SimpleFactory.GetInstance.Alarm != null)
                {
                    foreach (var item in SimpleFactory.GetInstance.Alarm.GetType().GetProperties())
                    {
                        var res = item.GetValue(SimpleFactory.GetInstance.Alarm);
                        if (res != null)
                        {
                            if (res is bool blen)
                            {
                                if (item.CustomAttributes.Count() > 0)
                                {
                                    if (item.CustomAttributes.ElementAt(0)?.ConstructorArguments.Count() > 0)
                                    {
                                        var info = item.CustomAttributes.ElementAt(0)?.ConstructorArguments.ElementAt(0).Value;
                                        if (info != null)
                                        {
                                            App.Current.Dispatcher.Invoke(new Action(() =>
                                            {
                                                AlarmHelper.GetInstance.EdgeAlarm(blen, info.ToString());
                                            }));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(500);
            }), "报警检测监控");
        }

        public RelayCommand<object> NavChangedCommand { get; set; }

        private void DoNavChanged(object obj)
        {
            if (obj != null)
            {
                Type type = Type.GetType($"HBLConsole.View.{obj.ToString()}");
                if (type != null)
                {
                    ConstructorInfo cti = type.GetConstructor(Type.EmptyTypes);
                    if (cti != null)
                        MainContent = (FrameworkElement)cti.Invoke(null);
                }
            }
        }

    }
}
