using System;
using System.Linq;
using Excel2Json;
using Fort23.UTool;
using Utility;
namespace Fort23.Mono
{
    public class RedDotGroupData
    {
        public int Id;
        /// 
        /// 红点组ID
        /// 
        public int GroupId;
        /// 
        /// key层级 value id和红点信息
        /// 
        public Map> AllRedDotData = new Map>();
        public void Init(int id)
        {
            Id = id;
            GroupId = ConfigComponent.Instance.Get(id).GroupID;
            AddRedDotData(id);
            // Update();
        }
        public void AddRedDotData(int id)
        {
            ReddotConfig reddotConfig = ConfigComponent.Instance.Get(id);
            if (AllRedDotData.ContainsKey(reddotConfig.Layer))
            {
                RedDotData redDotData = new RedDotData();
                redDotData.Id = id;
                redDotData.GroupId = GroupId;
                redDotData.Layer = reddotConfig.Layer;
                redDotData.EnableId = reddotConfig.Enable;
                AllRedDotData[reddotConfig.Layer].Add(redDotData.Id, redDotData);
            }
            else
            {
                Map redDotDatas = new Map();
                RedDotData redDotData = new RedDotData();
                redDotData.Id = id;
                redDotData.GroupId = GroupId;
                redDotData.Layer = reddotConfig.Layer;
                redDotData.EnableId = reddotConfig.Enable;
                redDotDatas.Add(redDotData.Id, redDotData);
                AllRedDotData.Add(redDotData.Layer, redDotDatas);
            }
        }
        public bool TargetLayerIsEnable(int layer)
        {
            bool isEnable = false;
            int count = AllRedDotData.Keys.Count;
            for (int i = count - 1; i >= 0; i--) //优先更新最底层红点
            {
                try
                {
                    int key = AllRedDotData.Keys.ToArray()[i];
                    Map redDotDatas = AllRedDotData[key];
                    foreach (var redDotData in redDotDatas)
                    {
                        if (redDotData.Value.Layer >= layer)
                        {
                            if (redDotData.Value.isEnable)
                            {
                                isEnable = true;
                                break;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    LogTool.Log(e);
                    return false;
                }
            }
            // foreach (var keyValuePair in AllRedDotData)
            return isEnable;
        }
        public void Update()
        {
            int count = AllRedDotData.Keys.Count;
            for (int i = count - 1; i >= 0; i--) //有限更新最底层红点
            {
                try
                {
                    int key = AllRedDotData.Keys.ToArray()[i];
                    Map redDotDatas = AllRedDotData[key];
                    foreach (var keyValuePair in redDotDatas)
                    {
                        keyValuePair.Value.Update();
                    }
                }
                catch (Exception e)
                {
                    LogTool.Log(e);
                }
            }
        }
    }
}