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
    {
        static Type type;
        static IGvl InterfaceGVL;
        public ProcessMonitoringViewModel()
        {
            WindowName = "流程监控";
            if (type == null)
            {
                InterfaceGVL = SimpleFactory.GetInstance.GetGvl();
                type = InterfaceGVL?.GetType();
                if (ProcessData.GetInstance.Conditions?.Count <= 0)
                {
                    if (InterfaceGVL != null && type != null)
                    {
                        GetProcessInfo();
                        UpdateProcess();
                    }
                }
            }

        }

        /// <summary>
        /// 获取流程信息
        /// </summary>
        private void GetProcessInfo()
        {
            //添加流程监控信息
            foreach (var item in type.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 void UpdateProcess()
        {
            ThreadOperate.GetInstance.StartLong(new Action(() =>
            {
                foreach (var item in type.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 = type.GetProperty(item.Name)?.GetValue(InterfaceGVL, 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");

        }
    }
}
