using HBLConsole.Attributes;
//using HBLConsole.Business.Devices;
using HBLConsole.Model;
using HBLConsole.Service;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HBLConsole.Factory;
using HBLConsole.Interface;

namespace HBLConsole.ViewModel
{
    public class ProcessMonitoringViewModel : ViewModelBase
    {
        public ProcessMonitoringViewModel()
        {
            WindowName = "流程监控";
        }

        static ProcessMonitoringViewModel()
        {
            GetProcessInfo();
            UpdateProcess();
        }

        /// <summary>
        /// 获取流程信息
        /// </summary>
        private static void GetProcessInfo()
        {
            if (SimpleFactory.GetInstance.GVL == null) return;
            //添加流程监控信息
            foreach (var item in SimpleFactory.GetInstance.GVL.GetType().GetProperties())
            {
                if (item.CustomAttributes.Count() > 0)
                {
                    string? propertyInfo = item.GetCustomAttribute<CircuitAttribute>()?.PropertyIllustrate;
                    string[]? Classifiactions = item.GetCustomAttribute<CircuitAttribute>()?.Classifiaction;
                    if (Classifiactions != null)
                    {
                        foreach (var itemClassifiactions in Classifiactions)
                        {
                            if (ProcessData.GetInstance.Conditions.ContainsKey(itemClassifiactions))
                            {
                                if (propertyInfo != null)
                                {
                                    int index = Array.FindIndex(ProcessData.GetInstance.Conditions[itemClassifiactions].ToArray(), p => p.propertyIllustrate == propertyInfo);
                                    if (index >= 0)
                                    {
                                        ProcessData.GetInstance.Conditions[itemClassifiactions].ElementAt(index).ToolTip = item.Name;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            var Conditions = ProcessData.GetInstance.Conditions;
            if (Conditions != null)
            {
                foreach (var item in Conditions)
                {
                    ProcessConditions process = new ProcessConditions();
                    process.ProcessTitl = item.Key;
                    foreach (var values in item.Value)
                    {
                        process.Conditions.Add(new Condition() { propertyIllustrate = values.propertyIllustrate });
                    }
                    processConditions.Add(process);
                }
            }
        }

        /// <summary>
        /// 更新流程
        /// </summary>
        private static void UpdateProcess()
        {
            if (SimpleFactory.GetInstance.GVL == null) return;
            ThreadManage.GetInstance.StartLong(new Action(() =>
            {
                foreach (var item in SimpleFactory.GetInstance.GVL.GetType().GetProperties())
                {
                    if (item.CustomAttributes.Count() > 0)
                    {
                        string? propertyInfo = item.GetCustomAttribute<CircuitAttribute>()?.PropertyIllustrate;
                        string[]? Classifiactions = item.GetCustomAttribute<CircuitAttribute>()?.Classifiaction;
                        if (Classifiactions != null)
                        {
                            foreach (string? Titles in Classifiactions)
                            {
                                if (Titles != null && propertyInfo != null)
                                {
                                    bool? isNot = ProcessData.GetInstance.Conditions[Titles]?.FirstOrDefault(p => p.propertyIllustrate == propertyInfo)?.IsNot;
                                    int index = Array.FindIndex(processConditions.ToArray(), p => p.ProcessTitl == Titles);

                                    if (index >= 0)
                                    {
                                        int PropertyIndex = Array.FindIndex(processConditions.ElementAt(index).Conditions.ToArray(), p => p.propertyIllustrate == propertyInfo);
                                        if (PropertyIndex >= 0)
                                        {
                                            var res = SimpleFactory.GetInstance.GVL.GetType().GetProperty(item.Name)?.GetValue(SimpleFactory.GetInstance.GVL, null);
                                            if (res != null)
                                            {
                                                if (res is bool blen)
                                                {
                                                    if (isNot != null)
                                                    {
                                                        processConditions.ElementAt(index).Conditions.ElementAt(PropertyIndex).ConditionMet = (bool)isNot ? !blen : blen;
                                                    }
                                                }
                                                else if (res is ConcurrentQueue<OrderLocInfo> OrderLocInfoS)
                                                {
                                                    processConditions.ElementAt(index).Conditions.ElementAt(PropertyIndex).ConditionMet = OrderLocInfoS.Count > 0;
                                                }
                                            }

                                        }

                                    }
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(1000);
            }), "UpdateValue");

        }
    }
}
