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

namespace Server {
    /// <summary>
    /// 厅管理_朋友房部分
    /// </summary>
    public class TingManager_Friend : TingManager {

        /// <summary>
        /// 是否是朋友房
        /// </summary>
        public override bool isFriend {
            get {
                return true;
            }
        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="ting"></param>
        public TingManager_Friend(Server.Config.TingConfig ting) : base(ting) { }

        /// <summary>
        /// 退出桌子
        /// </summary>
        /// <param name="pl"></param>
        /// <param name="isSendPack"></param>
        /// <returns></returns>
        public override bool QuitDesk(PlayerDataAsyc pl, bool isSendPack) {
            int deskid = pl.deskid;
            if (deskid < 0 || deskid >= desks.Count) {
                Debug.Error("错误没有这个桌子id:" + deskid);
                return false;
            }

           

            //桌子状态 闲置 排队 开战
            Desk desk = desks[deskid];
            if (!desk.CanQuit(pl.seat)) {  //开战的桌子不能退出，只能离线
                //Debug.Log("开战的桌子???");
                return false;
            }

            Debug.Info("退出桌子!!!!");

            if (!desks[deskid].RemovePlayer(pl)) {
                //Debug.Error("移除玩家失败!");
                return false;
            }

            if (!pl.isRobot && pl.roomid >= 0 && pl.roomid < GameManager.instance.roomCnt) {
                var room = GameManager.instance.FindRoom(pl.roomid);
                
                //Debug.Log("退出桌子逻辑!");
                room.SetRoomPlayerCnt(pl, true);
            }

            //Debug.Log("退出后玩家状态修改!");
            pl.state = (int)PlayerState.INGame;

            pl.roomid = -1;
            pl.tingid = -1;
            pl.deskid = -1;
            pl.friendRoomNum = -1;
            pl.friendWeiYiMa = string.Empty;

            if (pl.shadowsInfo != null) { //本来就没替身,这句话不会执行
                Debug.Error("朋友房不应该由替身!");
            }

            pl.shadows = null;
            pl.shadowsInfo = null;

            desk.SendPlayersinfo(1, pl.userid, pl.nickname);
            Debug.Info("玩家退出桌子:" + pl.userid);
            return true;
        }

        /// <summary>
        /// 定时器处理
        /// </summary>
        public override void SecondTime() {

            int len = Count;
            for (int i = len - 1; i >= 0; i--) {
                desks[i].SecondTime();
            }

            CheckAllDesksDisTimer();
        }

        /// <summary>
        /// 检查朋友房的解散定时计数器
        /// </summary>
        private int CheckAllDesksDisTimeCnt;

        /// <summary>
        /// 多少秒钟检查一次朋友房的解散
        /// </summary>
        private const int DoTimeCnt_CheckAllDeskDis = 300;

        /// <summary>
        /// 检查朋友房是否需要解散的定时器
        /// </summary>
        public void CheckAllDesksDisTimer() {
            if (!isFriend)
                return;

            CheckAllDesksDisTimeCnt++;

            if (CheckAllDesksDisTimeCnt > DoTimeCnt_CheckAllDeskDis) {
                CheckAllDesksDisTimeCnt = 0;
                CheckAllDesksDis();
            }
        }

        /// <summary>
        /// 加入朋友房
        /// </summary>
        /// <param name="pl"></param>
        /// <param name="roomid"></param>
        /// <param name="deskid"></param>
        /// <param name="pwd"></param>
        /// <param name="isClub"></param>
        /// <returns></returns>
        public override bool InTing(PlayerDataAsyc pl, int roomid,ref string errinfo, ref int errorCode, int deskid = -1, string pwd = "", bool isClub = false)
        {
            return FriendRoomManager.instance.EnterDesk(deskid, pl, pwd, isClub,ref errinfo,ref errorCode) == 1;
        }

        /// <summary>
        /// 检查朋友房规则
        /// </summary>
        /// <param name="gz"></param>
        /// <returns></returns>
        public virtual bool CheckFriendGz(string gz){
            return !string.IsNullOrEmpty(gz);
        }

        /// <summary>
        /// 准备
        /// </summary>
        /// <param name="pl"></param>
        public override void Ready(PlayerDataAsyc pl) {
            if (pl.deskid < 0 || pl.deskid >= Count) {
                Debug.Warning("没有这个桌子!");
                return;
            }
            Debug.Log("准备xxx");
            desks[pl.deskid].Ready(pl, true);
        }

        /// <summary>
        /// 检查桌子信息符合不符合创建
        /// </summary>
        /// <returns>-4,-5逻辑问题 -6房卡不足创建失败 -7未检测到ip -8必须微信登录或绑定 -9 必须微信绑定 -10没有定位信息 -11没有设备信息  -12玩家在游戏中 -13玩家的状态不对</returns>
        public virtual int CheckDeskInfo(PlayerDataAsyc pl, DeskInfo info) {

            Debug.Log("1");
            DeskRule rule = info.rule;

            if (rule.warCnt <= 0) {
                Debug.Error("wacCntERROR:" + rule.warCnt);
                return -4;
            }

            //if (rule.warCnt < cardPan || rule.warCnt % cardPan != 0) {
            //    Debug.Error("a------asdsabbdb" + rule.warCnt + "," + cardPan);
            //    return -5;
            //}

            //检查
            if (info.creatStyle != 5) {
                if (pl.fangka < info.fangka) {
                    Debug.Warning("房卡不足!");
                    //回复房卡不足
                    //SendManager.SendErrorInfo_Socket(pl.userid, 2, 12, pl.socketUtil);
                    return -6;  //房卡不足
                }
            }

            if (rule.interceptIp > 0) {//检测ip
                if (rule.interceptIp > 1) { //检查是否相同
                    Debug.Warning("ip无法通过ip");
                    if (string.IsNullOrEmpty(pl.ip))
                        return -7;
                }
            }

            if (rule.interceptpos > 0) { //没有地理位置的不让进
                if (pl.device.isEmpty) {
                    Debug.Info("没有定位信息，创建房间失败!");
                    return -10;
                }
            }

            if (rule.interecptimei > 0) //检查设备信息
            {
                if (string.IsNullOrEmpty(pl.device.phone_imei)) //没有设备信息,创建失败
                {
                    Debug.Info("设备信息为空!");
                    return -11;
                }
            }

            if (rule.interceptWeChat > 0) {//拦截账号
                Debug.Log("微信拦截!");
                if (rule.interceptWeChat == 1) {//1表示微信登录即可
                    if (pl.userType < 100)  //微信登录，或者微信绑定账号
                        return -8;
                } else  //2必须是微信绑定
                    if (pl.userType < 110)  //必须是微信绑定账号
                    return -9;
            }
            
            return 1;
        }

        /// <summary>
        /// 检查所有的房间是否需要被解散!
        /// </summary>
        /// <returns></returns>
        public void CheckAllDesksDis() {
            int len = desks.Count;
            for (int i = len - 1; i >= 0; i--) {
                var desk = desks[i];

                if (desk.state == DeskState.Idle) continue;

                int isDisBank = desk.deskinfo.CheckDisBank();

                if (isDisBank > 0) {
                    //解散
                    Debug.Info("检查桌子解散!");
                    FriendRoomManager.instance.DisBank(desk, 2 + isDisBank);
                }
            }
        }

    }
}
