using System.Collections.Generic;

namespace Unity.Muninn.Model
{
    // 这边集中定义错误码
    public enum MuninnCode
    {
        OK = 0,
        InvalidParam = 10000,
        InternalServerError = 10001,
        OperationFailed = 10002,
        MaxPlayersExceeded = 20001,
        MismatchedAppId = 20002,
        InvalidJoinCode = 20003, // JoinCode不合法
        PlayerJoinedAnotherRoom = 20004, // 该玩家还没退出其他的活跃房间
        ConnectionFailed = 20099,
        UserReLoggedIn = 20101,
        RoomClosed = 20102,
        UnexpectedRoomError = 20103,
        ClientTimedOut = 20104,
        KickPlayerByMasterClient = 20105, // 被房主kick了, 这算原因，不算错误
        KickPlayerFailForSelf = 20106, // 房主不能踢房主本人
        KickPlayerFailForNotFound = 20107, // 踢的用户不在房间内
        KickPlayerFailForNonMasterClient = 20108, // 非房主不能踢人
        UpdatePlayerFailForOffline = 20201, // 要更新的用户不在房间内
        UpdatePlayerFailForPermissionDenied = 20202, // 更新用户属性的权限不够

        // 超过限制
        MaxStickyEventsExceeded = 20300,
        MismatchedStickyEventKey = 20301,
        StickyEventKeyNotFound = 20302,

        // 客户端定义的错误码[1, 10000)
        InvalidClientAppId = 1000,
        InvalidClientAppSecret = 1001,
        InvalidClientPlayerId = 1002,
        MissingJoinCode = 1003,

        // 这部分是lobby相关的错误码
        LobbyAccessDenied = 9000,
        LobbyAuthenticationFailed = 9001,
        LobbyRoomClosed = 9002,
        LobbyRoomUnknown = 9003,
        LobbyQueryRoomFailed = 9004,
        LobbyMissingAuthToken = 9005,
        LobbyCreateRoomFailed = 9006,
        LobbyCreateRoomLimitExceeded = 9007,
        LobbyJoinRoomFailed = 9008,
        LobbyCloseRoomFailed = 9009,

        // 新增错误码
        LobbyRoomNameAlreadyExisted = 9010,
    }

    // 对错误码进行映射工作
    public class MuninnStatusCodeHelper
    {
        // 
        private static Dictionary<uint, string> gErrMap = new Dictionary<uint, string>();

        static MuninnStatusCodeHelper()
        {
            // 这是类的静态构造函数
            // Ok = 0,
            gErrMap[(uint)MuninnCode.OK] = "OK";
            // InvalidParam = 10000,
            gErrMap[(uint)MuninnCode.InvalidParam] = "Invalid Param";
            // InternalServerError = 10001,
            gErrMap[(uint)MuninnCode.InternalServerError] = "Internal Server Error";
            // OperationFailed = 10002,
            gErrMap[(uint)MuninnCode.OperationFailed] = "Operation Failed";
            // MaxPlayersExceeded = 20001,
            gErrMap[(uint)MuninnCode.MaxPlayersExceeded] = "Max Players Exceeded";
            // MismatchedAppId = 20002,
            gErrMap[(uint)MuninnCode.MismatchedAppId] = "Mismatched AppId";
            // InvalidJoinCode                  = 20003, // JoinCode不合法
            gErrMap[(uint)MuninnCode.InvalidJoinCode] = "Invalid Join Code";
            // PlayerJoinAnotherRoom            = 20004, // 该玩家还没退出其他的活跃房间
            gErrMap[(uint)MuninnCode.PlayerJoinedAnotherRoom] = "Player Join Another Room";

            // ConnectionFailed = 20099,
            gErrMap[(uint)MuninnCode.ConnectionFailed] = "Connection Failed";
            // UserReLoggedIn = 20101,
            gErrMap[(uint)MuninnCode.UserReLoggedIn] = "User ReLogged In";
            // RoomClosed = 20102,
            gErrMap[(uint)MuninnCode.RoomClosed] = "Room Closed";
            // UnexpectedRoomError = 20103,
            gErrMap[(uint)MuninnCode.UnexpectedRoomError] = "Unexpected Room Error";
            // ClientTimedOut = 20104,
            gErrMap[(uint)MuninnCode.ClientTimedOut] = "Client TimedOut";
            // KickPlayerByMasterClient = 20105
            gErrMap[(uint)MuninnCode.KickPlayerByMasterClient] = "Kick Player By Master Client";
            // KickPlayerFailForSelf = 20106
            gErrMap[(uint)MuninnCode.KickPlayerFailForSelf] = "Kick Player Fail For Self";
            // KickPlayerFailForNotFound = 20107
            gErrMap[(uint)MuninnCode.KickPlayerFailForNotFound] = "Kick Player Fail For Not Found";
            // KickPlayerFailForNonMasterClient = 20108
            gErrMap[(uint)MuninnCode.KickPlayerFailForNonMasterClient] = "Kick Player Fail For Not Master Client";

            // UpdatePlayerFailForOffline		 = 20201
            gErrMap[(uint)MuninnCode.UpdatePlayerFailForOffline] = "Update Player Fail For Offline"; // 要更新的用户不在房间内
            // UpdatePlayerFailForPermissionDenied = 20202
            gErrMap[(uint)MuninnCode.UpdatePlayerFailForPermissionDenied] =
                "Update Player Fail For Permission Denied"; // 预留, 权限不足

            //
            // // 超过限制
            // MaxStickyEventsExceeded = 20300,
            gErrMap[(uint)MuninnCode.MaxStickyEventsExceeded] = "Max Sticky Events Exceeded";
            // MismatchedStickyEventKey = 20301,
            gErrMap[(uint)MuninnCode.MismatchedStickyEventKey] = "Mismatched Sticky Event Key";
            // StickyEventKeyNotFound   = 20302,
            gErrMap[(uint)MuninnCode.StickyEventKeyNotFound] = "Sticky Event Key Not Found";

            //
            // // 客户端定义的错误码[1, 10000)
            // InvalidClientAppId = 1000,
            gErrMap[(uint)MuninnCode.InvalidClientAppId] = "Invalid Client AppId";
            // InvalidClientAppSecret = 1001,
            gErrMap[(uint)MuninnCode.InvalidClientAppSecret] = "Invalid Client AppSecret";
            // InvalidClientPlayerId = 1002,
            gErrMap[(uint)MuninnCode.InvalidClientPlayerId] = "Invalid Client PlayerId";
            // MissingJoinCode = 1003,
            gErrMap[(uint)MuninnCode.MissingJoinCode] = "Missing Join Code";

            // // 这部分是lobby相关的错误码
            // LobbyAccessDenied = 9000,
            gErrMap[(uint)MuninnCode.LobbyAccessDenied] = "Lobby Access Denied";
            // LobbyAuthenticationFailed = 9001,
            gErrMap[(uint)MuninnCode.LobbyAuthenticationFailed] = "Lobby Authentication Failed";
            // LobbyRoomClosed= 9002,
            gErrMap[(uint)MuninnCode.LobbyRoomClosed] = "Lobby Room Closed";
            // LobbyRoomUnknown	= 9003,
            gErrMap[(uint)MuninnCode.LobbyRoomUnknown] = "Lobby Room Unknown Error";
            // LobbyQueryRoomFailed = 9004, 
            gErrMap[(uint)MuninnCode.LobbyQueryRoomFailed] = "Lobby Query Room Failed";
            // LobbyMissingAuthToken = 9005
            gErrMap[(uint)MuninnCode.LobbyMissingAuthToken] = "Lobby Missing AuthToken";
            // LobbyCreateRoomFailed = 9006
            gErrMap[(uint)MuninnCode.LobbyCreateRoomFailed] = "Lobby Create Room Failed";
            // LobbyCreateRoomLimitExceeded = 9007,
            gErrMap[(uint)MuninnCode.LobbyCreateRoomLimitExceeded] = "Lobby Create Room Limit Exceeded";
            // LobbyJoinRoomFailed = 9008,
            gErrMap[(uint)MuninnCode.LobbyJoinRoomFailed] = "Lobby Join Room Failed";
            // LobbyCloseRoomFailed = 9009
            gErrMap[(uint)MuninnCode.LobbyCloseRoomFailed] = "Lobby Close Room Failed";
            // LobbyRoomNameAlreadyExisted = 9010
            gErrMap[(uint)MuninnCode.LobbyRoomNameAlreadyExisted] = "Lobby Room Name Already Existed";
        }

        // 进行快速的错误码映射
        public static MuninnError Convert(uint code)
        {
            MuninnError error = new MuninnError();
            error.Code = code;
            error.Description = gErrMap[code] ?? "Unknown Error";
            return error;
        }

        public static MuninnError Convert(MuninnCode code)
        {
            return Convert((uint)code);
        }
    }
}