using DeviceCommand.Base;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSMasterCAN;

namespace UIShare.GlobalVariable
{
    public class GlobalInfo:BindableBase
    {
        public event EventHandler? ScopeChanged;
        public Dictionary<string,ScopedContext> ContextDic { get; set; }
        public Dictionary<string,StepRunning> StepRunningDic { get; set; }
        public Dictionary<string, SystemConfig> ConfigDic { get; set; }
        public Dictionary<string, IScopedProvider> ScopeDic { get; set; }

        /// <summary>硬件指纹 → 设备实例的并发池，确保同一物理硬件全局只创建一个驱动实例。</summary>
        public ConcurrentDictionary<string, Lazy<IBaseInterface>> HardwarePool { get; set; }

        /// <summary>CAN 硬件指纹 → ZLGCANFD 实例的并发池，确保同一 CAN 卡全局只创建一个驱动实例。</summary>
        public ConcurrentDictionary<string, Lazy<CAN>> CanPool { get; set; }

        /// <summary>硬件指纹 → 正在使用该设备的作用域名称列表，用于引用计数与安全销毁。</summary>
        public ConcurrentDictionary<string, Lazy<List<string>>> DeviceAndScopeDic { get; set; }

        public String UserName { get; set; } = "Not Logged in";
        public bool IsAdmin { get; set; } = true;
        public string CurrentOpeningScope;
        private string _currentScope = "default";
        public string CurrentScope
        {
            get => _currentScope;
            set
            {
                if (_currentScope != value)
                {
                    _currentScope = value;
                    ScopeChanged?.Invoke(this, EventArgs.Empty);
                }
            }
        }
        public GlobalInfo()
        {
            ContextDic = new();
            StepRunningDic = new();
            ConfigDic = new();
            ScopeDic = new();
            HardwarePool = new ConcurrentDictionary<string, Lazy<IBaseInterface>>(StringComparer.OrdinalIgnoreCase);
            CanPool = new ConcurrentDictionary<string, Lazy<CAN>>(StringComparer.OrdinalIgnoreCase);
            DeviceAndScopeDic = new ConcurrentDictionary<string, Lazy<List<string>>>(StringComparer.OrdinalIgnoreCase);
            CurrentScope = "default";
        }

    }
}
