using System.Collections.Generic;

namespace ET.Server
{
    [FriendOf(typeof(Room))]
    public static class RoomSystem
    {
        [ObjectSystem]
        public class RoomAwakeSystem : AwakeSystem<Room, Player>
        {
            protected override void Awake(Room self, Player owner)
            {
                Log.Info($"创建房间实体...");
                self.RoomId = RandomGenerator.RandRoomId();
                self.MaxNum = 4;
                self.Type = 1;
                self.OwnerId = owner.Id;
                self.CreateTime = TimeHelper.ServerNow();
                // 添加房间玩家集合
                self.Players.Add(owner.Id, owner);
                // 添加麻将主逻辑组件
                switch (self.Type)
                {
                    case 1:
                        // 黄冈晃晃
                        self.AddComponent<HGHHMainCheckerComponent>();
                        break;
                    case 2:
                        break;
                }
                
                // 添加本地房间数据
                self.DomainScene().GetComponent<GameRoomComponent>().Add(self);
            }
        }
        
        [ObjectSystem]
        public class RoomDestroySystem : DestroySystem<Room>
        {
            protected override void Destroy(Room self)
            {
                Log.Info($"销毁房间实体...");
                // 移除本地房间数据
                self.DomainScene().GetComponent<GameRoomComponent>()?.Remove(self.RoomId);
            }
        }
        
        /// <summary>
        /// 玩家是否已满
        /// </summary>
        /// <param name="self"></param>
        public static bool PlayerIsMax(this Room self)
        {
            if (self.Players.Count >= self.MaxNum)
            {
                return true;
            }

            return false;
        }
        
        /// <summary>
        /// 获取房间内所有玩家
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static SortedList<long, Player> GetAllPlayers(this Room self)
        {
            return self.Players;
        }

        /// <summary>
        /// 加入一个玩家
        /// </summary>
        /// <param name="self"></param>
        /// <param name="player"></param>
        public static void Add(this Room self, Player player)
        {
            self.Players.Add(player.Id, player);
        }
        
        /// <summary>
        /// 移除一个玩家
        /// </summary>
        /// <param name="self"></param>
        /// <param name="playerId"></param>
        public static void Remove(this Room self, long playerId)
        {
            self.Players.Remove(playerId);
        }
        
        /// <summary>
        /// 是否玩家都已准备,可开始
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static bool IsStart(this Room self)
        {
            int cnt = 0;
            
            foreach (Player player in self.Players.Values)
            {
                if (player is { State: 1 })
                {
                    cnt += 1;
                }
            }

            if (cnt >= self.MaxNum)
            {
                return true;
            }
            
            return false;
        }

        /// <summary>
        /// 根据当前玩家位置,获取另外三家位置顺序
        /// </summary>
        /// <param name="self"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static int[] GetSorcPos(this Room self, int pos)
        {
            int[] nextPos = pos switch
            {
                3 => new int[] { 0, 1, 2 },
                2 => new int[] { 3, 0, 1 },
                1 => new int[] { 2, 3, 0 },
                0 => new int[] { 1, 2, 3 },
                _ => null
            };
            return nextPos;
        }
    }
}