using System.Collections.Generic;
using System.Linq;
using MrWu.Debug;

namespace Server.Config
{
    public partial class NodeConfigCategory
    {
        private Dictionary<int, List<NodeConfig>> _nodeMap;
        
        partial void PostInit()
        {
            _nodeMap = new Dictionary<int, List<NodeConfig>>();
            for (int i = 0; i < _dataList.Count; i++)
            {
                if (!_nodeMap.TryGetValue(_dataList[i].NodeId, out var list))
                {
                    _nodeMap.Add(_dataList[i].NodeId, list = new List<NodeConfig>());
                }

                for (int j = 0; j < list.Count; j++)
                {
                    if (_dataList[i].NodeNum == list[j].NodeNum)
                    {
                        Debug.Error($"节点配置重复! {list[j].NodeId} {list[j].NodeNum}");
                    }
                }
                list.Add(_dataList[i]);
            }
        }
        
        public NodeConfig GetByNodeId(int nodeId)
        {
            if (!_nodeMap.TryGetValue(nodeId, out var list))
            {
                return null;
            }

            return list.FirstOrDefault();
        }
        
        /// <summary>
        /// 获取服务器自身配置
        /// </summary>
        /// <returns></returns>
        public NodeConfig GetSelfConfig()
        {
            if (!_nodeMap.TryGetValue(TableManager.Instance.NodeInfo.Id,out var list))
            {
                return null;
            }

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].NodeNum == TableManager.Instance.NodeInfo.NodeNum)
                {
                    return list[i];
                }
            }

            return null;
        }

        /// <summary>
        /// 是否是多开节点
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public bool IsMult(int nodeId)
        {
            if (!_nodeMap.TryGetValue(nodeId, out var list))
            {
                return false;
            }

            return list.Count > 1;
        }
    }
}