using System;
using GameData;
using GameMessage;
using UnityGame;

namespace GameFix
{
    /// <summary>
    /// int[] Seat
    ///   - 下标索引表示进入桌子的初始位置号，也是服务器原始玩家数组的索引
    ///   - 索引对应的内容是存在打乱之后的位置信息
    /// </summary>
    public static class GameDeskUtility
    {
        #region GameDeskInfo 扩展

        /// <summary>
        /// 初始化位置信息
        /// </summary>
        public static void InitDeskSeat(this GameDeskInfo deskInfo, int playerCnt)
        {
            deskInfo.Seat = new int[playerCnt];
            for (int i = 0; i < playerCnt; i++)
            {
                deskInfo.Seat[i] = i;
            }
        }

        public static void UpdateSeat(this GameDeskInfo deskInfo, int[] seat)
        {
            deskInfo.Seat = (int[])seat.Clone();
        }

        public static int GetServerSeat(this GameDeskInfo deskInfo, int serverSeat)
        {
            return GetServerSeat(deskInfo.Seat, serverSeat);
        }
        
        public static int GetNextServerSeat(this GameDeskInfo deskInfo, int serverRealSeat)
        {
            return GetNextServerSeat(deskInfo.Seat, serverRealSeat);
        }
        
        public static int GetNextServerSeat(this GameDeskInfo deskInfo, int serverRealSeat, int n)
        {
            return GetNextServerSeat(deskInfo.Seat, serverRealSeat, n);
        }
        
        public static void SwitchSeat(this GameDeskInfo deskInfo, int originalSeat, int targetSeat)
        {
            SwitchSeat(deskInfo.Seat, originalSeat, targetSeat);
        }
#if !Server
        public static int ClientToServerSeat(this GameDeskInfo deskInfo, int clientSeat)
        {
            return ClientToServerSeat(deskInfo.Seat, clientSeat);
        }
        
        public static int ServerToClientSeat(this GameDeskInfo deskInfo, int serverSeat)
        {
            return ServerToClientSeat(deskInfo.Seat, serverSeat);
        }
#endif
        #endregion
        
#if !Server
        /// <summary>
        /// 客户端转服务器位置
        /// </summary>
        public static int ClientToServerSeat(int[] seat, int clientSeat)
        {
            if (UnityGame.hjhaGlobale.Instance.userData == null)
            {
                return -1;
            }
            
            if (seat == null || seat.Length < clientSeat)
            {
                return -1;
            }

            if (seat.Length <= 0)
            {
                return -1;
            }

            if (clientSeat < 0)
            {
                return 0;
            }

            int mySeat = UnityGame.hjhaGlobale.Instance.userData.seat;
            mySeat = Array.IndexOf(seat, mySeat);
            if (mySeat < 0)
            {
                if (hjhaGlobale.Instance.userData.state == (int)PlayerState.Desk 
                    || hjhaGlobale.Instance.userData.state == (int)PlayerState.Ready 
                    || hjhaGlobale.Instance.userData.state == (int)PlayerState.War)
                {
                    UnityEngine.Debug.LogError($"位置信息错误：MySeat={mySeat}");
                }
                
                return -1;
            }
            int serverSeat = (mySeat + clientSeat) % seat.Length;
            return GetServerSeat(seat, serverSeat);
        }
        
        /// <summary>
        /// 服务器转客户端位置
        /// </summary>
        public static int ServerToClientSeat(int[] seat, int serverSeat)
        {
            if (UnityGame.hjhaGlobale.Instance.userData == null)
            {
                return -1;
            }

            if (seat == null || seat.Length < serverSeat)
            {
                return -1;
            }

            int seatCnt = seat.Length;
            if (seatCnt <= 0)
            {
                return -1;
            }

            if (serverSeat < 0)
            {
                return -1;
            }
            
            int mySeat = UnityGame.hjhaGlobale.Instance.userData.seat;
            mySeat = Array.IndexOf(seat, mySeat);
            serverSeat = Array.IndexOf(seat, serverSeat);

            if (mySeat < 0 || serverSeat < 0)
            {
                UnityEngine.Debug.LogError($"位置信息错误：ServerSeat={serverSeat} MySeat={mySeat}");
                return -1;
            }
            
            return (serverSeat - mySeat + seatCnt) % seatCnt;
        }
#endif
        /// <summary>
        /// 获取真实的服务器位置
        /// </summary>
        public static int GetServerSeat(int[] seat, int serverSeat)
        {
            if (seat == null || seat.Length < serverSeat)
            {
#if Server
                MrWu.Debug.Debug.Error($"位置信息错误：serverSeat={serverSeat}");      
#endif
                return serverSeat;
            }
            
            return seat[serverSeat];
        }

        /// <summary>
        /// 获取 [serverSeat] 下N个玩家的服务器的位置
        /// </summary>
        public static int GetNextServerSeat(int[] seat, int serverSeat, int n)
        {
            if (seat == null || seat.Length < serverSeat)
            {
#if Server
                MrWu.Debug.Debug.Error($"位置信息错误：serverSeat={serverSeat}");      
#endif
                return serverSeat;
            }

            serverSeat = Array.IndexOf(seat, serverSeat);
            if (serverSeat < 0)
            {
#if Server
                MrWu.Debug.Debug.Error($"位置信息错误：serverSeat={serverSeat}");      
#endif
                return serverSeat;
            }
            var nextSeat = (serverSeat + n) % seat.Length;
            return GetServerSeat(seat, nextSeat);
        }

        /// <summary>
        /// 获取 [serverSeat] 下一个玩家的服务器的位置
        /// </summary>
        public static int GetNextServerSeat(int[] seat, int serverSeat)
        {
            return GetNextServerSeat(seat, serverSeat, 1);
        }
        
        /// <summary>
        /// 交换位置
        /// </summary>
        public static void SwitchSeat(int[] seat, int originalSeat, int targetSeat)
        {
            (seat[originalSeat], seat[targetSeat]) = (seat[targetSeat], seat[originalSeat]);
        }
    }
}