using System.Linq;

namespace ET.Server
{
    [FriendOf(typeof (GameMapComponent))]
    public static class GameMapComponentSystem
    {
        public class GameMapComponentAwakeSystem: AwakeSystem<GameMapComponent>
        {
            /// <summary>
            /// 游戏服场景组件创建
            /// </summary>
            /// <param name="self"></param>
            protected override void Awake(GameMapComponent self)
            {
                GameMapComponent.Instance = self;
                // todo 添加场景销毁检测定时器
            }
        }

        public class GameMapComponentDestroySystem: DestroySystem<GameMapComponent>
        {
            /// <summary>
            /// 游戏服场景组件销毁
            /// </summary>
            /// <param name="self"></param>
            protected override void Destroy(GameMapComponent self)
            {
            }
        }

        public static void Add(this GameMapComponent self, Map map)
        {
            self.allMaps.TryAdd(map.Id, map);
        }

        public static Map Get(this GameMapComponent self, long instanceId)
        {
            self.allMaps.TryGetValue(instanceId, out Map map);
            return map;
        }

        public static Map GetByMapId(this GameMapComponent self, long mapId)
        {
            return self.allMaps.Values.FirstOrDefault(map => map != null && map.Id == mapId);
        }

        public static void Remove(this GameMapComponent self, long instanceId)
        {
            self.allMaps.Remove(instanceId);
        }

        public static Map[] GetAll(this GameMapComponent self)
        {
            return self.allMaps.Values.ToArray();
        }
    }
}