using System;
using System.Threading.Tasks;
using MrWu.Debug;
using Server.Data;
using GameData;
using GameDAL.FriendRoom;
using Server.Pack;
using Server.MQ;
using Server.Data.Module;
using GameMessage;

namespace Server {

    /// <summary>
    /// 进入桌子的处理
    /// </summary>
    public partial class FriendRoomManager {



        /// <summary>
        /// 进入桌子
        /// </summary>
        /// <param name="deskNum">房间号</param>
        /// <param name="pl"></param>
        /// <param name="pwd"></param>
        /// <param name="isClub"></param>
        /// <returns></returns>
        public int EnterDesk(int deskNum, PlayerDataAsyc pl, string pwd, bool isClub,ref string errinfo,ref int errcode) {

            if (pl.friendRoomNum > 0) {
                Debug.Error("您已经在朋友房了!" + pl.friendRoomNum);
                errinfo = "您已经在朋友房了!";
                if(!isClub)
                    GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 19, pl.socketUtil);//您在朋友房中
                errcode = MessageErrCode.ClubJoinDeskHasRoom;
                return -1;
            }

            Desk dsk = FindDesk(deskNum);
            if (dsk == null) {  //没这个房间
                errinfo = "进入房间失败!没有这个房间!";
                Debug.Info("进入房间失败!没有这个房间!" + deskNum);
                if (!isClub)
                    GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 4, pl.socketUtil);//无此房间
                errcode = MessageErrCode.ClubJoinDeskNotDesk;
                return -1;
            }

            DeskInfo dskinfo = dsk.deskinfo;
            DeskRule rule = dskinfo.rule;

            if (!string.IsNullOrEmpty(rule.pwd)) {
                if (rule.pwd != pwd) {   //密码错误
                    if(!isClub)
                        GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 5, pl.socketUtil);
                    Debug.Error("密码错误!");
                    errinfo = "进入房间失败，密码错误!";
                    errcode = MessageErrCode.ClubJoinDeskPwdError;
                    return -2;
                }
            }

            if ((dsk.state & DeskState.Queue) == 0) { //房间已过期，不能加入
                if (!isClub)
                    GeneralSendPack.SendErrorInfo_Socket(pl.userid, 1, 6, pl.socketUtil);
                Debug.Warning("房间过期!");
                errinfo = "进入房间失败，房间过期!";
                errcode = MessageErrCode.ClubJoinDeskInValid;
                return -3;
            }

            if (dsk.PlayerCnt >= rule.maxCnt) {  //最大开战人数
                if(!isClub)
                    GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 7, pl.socketUtil);
                Debug.Warning("人数太多了!" + rule.maxCnt + "," + dsk.PlayerCnt);
                errinfo = "进入房间失败，房间已满!";
                errcode = MessageErrCode.ClubJoinDeskFull;
                return -4;
            }
            
            if (rule.interceptIp > 0) {//检测ip
                if (rule.interceptIp == 1) { //检查是否相同
                    if (dsk.CheckPlayersIP(pl.ip)) {
                        if (!isClub)
                            GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 8, pl.socketUtil);
                        Debug.Info("检查ip无法通过!");
                        errinfo = "-进入房间失败，有相同ip的玩家在房间中!";
                        errcode = MessageErrCode.ClubJoinDeskSameIp;
                        return -5;
                    }
                } else {    //检查是否相同且不能为空!
                    if (string.IsNullOrEmpty(pl.ip)) {
                        if (!isClub)
                            GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 9, pl.socketUtil);
                        Debug.Error("检查ip无法通过saa");
                        errinfo = "进入房间失败，ip未读取!";
                        errcode = MessageErrCode.ClubJoinDeskIpEmpty;
                        return -6;
                    } else if (dsk.CheckPlayersIP(pl.ip) || isClub) {
                        if (!isClub)
                            GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 8, pl.socketUtil);
                        Debug.Info("检查ip无法通过");
                        errinfo = "进入房间失败，有相同ip的玩家在房间中!";
                        errcode = MessageErrCode.ClubJoinDeskSameIp;
                        return -6;
                    }
                }
            }

            if (rule.interecptimei > 0) {

                if (string.IsNullOrEmpty(pl.device.phone_imei)) {
                    Debug.Warning("玩家的设备唯一编号为空,加入房间失败:" + pl.device.phone_imei);
                    if (!isClub)
                        GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 55, pl.socketUtil);
                    errinfo = "进入房间失败，未获取到设备唯一码!";
                    errcode = MessageErrCode.ClubJoinDeskImeiEmpty;
                    return -10;
                }

                int len = dsk.Count;
                for (int i = 0; i < len; i++) {
                    if (dsk[i] == null)
                        continue;
                    if (dsk[i].device.phone_imei == pl.device.phone_imei) {
                        Debug.Warning("玩家的设备唯一编号一致.加入房间失败:" + dsk[i].userid + "," + pl.userid);
                        if (!isClub)
                            GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 55, pl.socketUtil);
                        errinfo = "进入房间失败，与房间内其他玩家的使用一个设备!";
                        errcode = MessageErrCode.ClubJoinDeskSameImei;
                        return -10;
                    }
                }
            }
            
            if (rule.interceptWeChat > 0) {//拦截账号
                if (rule.interceptWeChat == 1) {
                    if (pl.userType < 100) {    //微信登录，或者微信绑定账号
                        if (!isClub)
                            GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 10, pl.socketUtil);
                        errinfo = "进入房间失败，开启防作弊的房间微信账号不可进入!";
                        errcode = MessageErrCode.ClubJoinDeskNotWeChatAccount;
                        return -7;
                    }
                } else if (pl.userType < 110) { //必须是微信绑定账号
                    if (!isClub)
                        GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 11, pl.socketUtil);
                    errinfo = "进入房间失败，开启防作弊的房间进入需要绑定微信账号!";
                    errcode = MessageErrCode.ClubJoinDeskNotWeChatAccount;
                    return -8;
                }
            }
            
            Debug.Log("检查地理位置：" + rule.interceptpos);
            if (rule.interceptpos > 0) {  //检查地理位置
                                          //和当前房间的所有人比较地理位置 距离小于1千米的就可以坐一起
                if (pl.device.GetPosition().isEmpty) {
                    Debug.Info("玩家的经纬度为空，加入失败!");
                    if (!isClub)
                        GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 55, pl.socketUtil);
                    errinfo = "进入房间失败，未获取到您的位置信息，请开启定位权限后再试!";
                    errcode = MessageErrCode.ClubJoinDeskNotPosition;
                    return -10;
                }

                int len = dsk.Count;
                for (int i = 0; i < len; i++) {
                    if (dsk[i] == null) continue;

                    var result = ting.CheckCheatingLevel(pl, dsk[i]);
                    Debug.Info("得到数量:" + result.Item1);
                    if (result.Item1 > 3) {  //不在安全距离内
                        Debug.Warning("两玩家距离过进，加入房间失败:" + pl.userid + "," + dsk[i].userid);
                        errinfo = "进入房间失败，与房间中的其他玩家距离过近!";
                        if (!isClub)
                            GeneralSendPack.SendErrorInfo_Socket(pl.userid, 2, 55, pl.socketUtil);
                        errcode = MessageErrCode.ClubJoinDeskNotSafeDistance;
                        return -10;
                    }
                }
            }

            //Debug.Log("玩家上桌!");

            if (dsk.AddPlayer(pl, GameManager.instance.friendRoom.id))
            {

                errcode = MessageErrCode.Success;
                return 1;
            }

            errcode = MessageErrCode.ClubJoinDeskJoinFail;
            return -9;
        }

        /*
        /// <summary>
        /// 为这一桌加分,分数越高，玩法进入人的几率越大
        /// </summary>
        /// <param name="deskinfo"></param>
        public void AddScore(DeskInfo deskinfo) {
            //发送游戏信息,
            //发送全桌玩家数据
            if (deskinfo.creatStyle == 5) {
                int clubid = deskinfo.creatUserid;
                string wayid = deskinfo.wayid;
                string weiyima = deskinfo.weiyima;
                
                //int minCnt = deskinfo.rule.minCnt;

                
                Task.Factory.StartNew(
                    () => {
                        //朋友房的房间-加分
                        WayRoom.instance.IncScore(clubid, wayid, weiyima, minCnt, playerCnt);
                        var dt = WayRoom.instance.ClubWayPlayerChange(clubid, wayid, minCnt);
                        ServerHead head = new ServerHead(ServerPackAgreement.ClubPlayWayPlChange);
                        GlobalMQ.instance.DeliveryEveryOne((int)ModuleType.ClubModule, GlobalMQ.CreateMessage(head, dt), true);
                    }
                    );
            }
        }
        */

    }
}
