using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.Common;
using FWindSoft.Data;
using FWindSoft.DataFramework;
using FWindSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Com.FirmLib.UI.BllCommon
{
    public class EquipmentFamilyManager
    {
        //public static readonly EquipmentFamilyManager Instance = new EquipmentFamilyManager();
        private EquipmentFamilyManager()
        { }

        /// <summary>
        /// 获取当前设备信息
        /// </summary>
        /// <returns></returns>
        public static List<EquipmentFamilyItem> GetFamilys()
        {
           return  MemoryCacheUtil.GetCacheItem<List<EquipmentFamilyItem>>("EquipmentFamily", () => BllFactory<EquipmentFamilyBll>.Instance.GetAllItems(), new TimeSpan(0,0,1800))??new List<EquipmentFamilyItem>();
        }
        /// <summary>
        /// 获取设备族中叶子级节点
        /// </summary>
        /// <returns></returns>
        public static List<EquipmentFamilyItem> GetFamilyLeaves()
        {
            var originItems = GetFamilys();
            List<EquipmentFamilyItem> items = new List<EquipmentFamilyItem>();
            Queue<EquipmentFamilyItem> searchItems = new Queue<EquipmentFamilyItem>(originItems);
            while (searchItems.Any())
            {
                var currentItem = searchItems.Dequeue();
                if (currentItem.ChildItems.Any())
                {
                    currentItem.ChildItems.ForEach(c => searchItems.Enqueue(c));
                }
                else
                {
                    items.Add(currentItem);
                }
                    
            }
            return items;
        }
        /// <summary>
        /// 通过code取到名设备族名称
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetFamilyName(string code)
        {
            var listFamilys = GetFamilys();
            var family = listFamilys.FirstOrDefault(f=>f.Code==code);
            if (family != null)
                return family.Name;
            return string.Empty;

        }

        #region 相关信息点信息定义
        private readonly static Dictionary<string, List<EquipmentInfoPointItem>> m_InfoPointsCache = new Dictionary<string, List<EquipmentInfoPointItem>>();
        public static List<EquipmentInfoPointItem> GetInfoPointDefinitions(string code)
        {
            //避免破坏原始的的信息点定义集合
            List<EquipmentInfoPointItem> items = null;
            if (m_InfoPointsCache.TryGetValue(code, out items))
            {
                return new List<EquipmentInfoPointItem>(items); 
            }
            items = BllFactory<EquipmentInfoPointBll>.Instance.GetPointInfoDefinitions(code)??new List<EquipmentInfoPointItem>();
            m_InfoPointsCache.Add(code, items);
            return new List<EquipmentInfoPointItem>(items);
        }
        #endregion

        public static EquipmentFamilyItem GetTopFamily(string code)
        {
            var familys = GetFamilys();
            return GetTopFamily(familys, code);
        }
        public static string GetaTopCode(string code)
        {
            var familys = GetFamilys();
            return GetTopFamily(familys, code)?.Code??string.Empty;
        }
        private static EquipmentFamilyItem GetTopFamily(List<EquipmentFamilyItem> items, string code)
        {
            var familys = items;          
            foreach (var item in familys)
            {
                if (item.Code == code)
                {
                    return item;
                }                
                else
                {
                    var result = GetTopFamily(item.ChildItems ?? new List<EquipmentFamilyItem>(), code);
                    if (result != null)
                        return item;
                }
            }
            return null;
        }
    }
}
