using System;
using System.Collections.Generic;
using System.Threading;
using MrWu.Debug;
using Server.Data;
using GameData;
using Game.Queue;
using Game.Robot;
using Server.Core;

namespace Server {

    /// <summary>
    /// 厅管理_金币房部分
    /// </summary>
    public class TingManager_Gold : TingManager {

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

        /// <summary>
        /// 排队器
        /// </summary>
        protected IQueueControll queueControll;

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

            //初始化排队器
            queueControll = GameManager.instance.factory.GetQueueControll(config.QueueName, this);
        }

        /// <summary>
        /// 定时器添加机器人
        /// </summary>
        protected virtual void TimeingAddRobot() { }

        /// <summary>
        /// 检查回收空房间
        /// </summary>
        public virtual bool RecycleRooom(int deskid) {
            if (isFriend) return false;

            //Debug.Log("执行回收房间!");

            if (desks[deskid].CheckRoomIsEmpty()) {//是空房间 回收房间
                List<PlayerDataAsyc> pls = desks[deskid].RecoverRoom();//拿到机器人

                AddIdleing(desks[deskid]);
                RobotController.instance.PushRobot(pls, id);

                Debug.Info("回收房间: " + Environment.NewLine + desks[deskid].ToString());
                return true;
            }
            return false;
        }

        /// <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 (!isExists(deskid)) {
                Debug.Error("游戏数据错误,没有这个玩家:" + deskid + ",tingid:" + pl.tingid + ",userid:" + pl.userid);
                return false;
            }

            //桌子状态 闲置 排队 开战
            Desk desk = desks[deskid];

            
            if (Thread.CurrentThread.ManagedThreadId == ServerConst.MainThreadId)
            {
                Debug.Info("QuitDesk Id of Same");
            }
            else
            {
                Debug.Error("QuitDesk Error Thread is not Same!");
            }
            
            if (desk == null) {
                Debug.Error("桌子对象为null,deskid:" + deskid + ",tingid:" + pl.tingid + ",userid:" + pl.userid);
                return false;
            }
            if (!desk.CanQuit(pl.seat))
                return false;

            if (!desk.RemovePlayer(pl)) {
                Debug.Error("逻辑错误,这桌子没有这人!");
                return false;
            }

            if (!pl.isRobot && pl.roomid >= 0 && pl.roomid < GameManager.instance.roomCnt) {
                var room = GameManager.instance.FindRoom(pl.roomid);
                if (room != null) {
                    room.SetRoomPlayerCnt(pl, true);
                }
                else
                {
                    Debug.Error($"room is null!!!roomid:{pl.roomid}");
                }
            }

            pl.state = (int)PlayerState.INGame;

            pl.roomid = -1;
            pl.tingid = -1;
            pl.deskid = -1;

            // 这里要退出
            if (isSendPack) {
                GeneralSendPack.SendPlayerInfo(pl);
                //场景跳转
                GeneralSendPack.SendSceneChange(pl, SceneType.InGame);
            }

            //不知道这个人在没在排队,取消排队
            queueControll.Cancel(pl);

            if (isRecycle)
                //检查回收桌子
                RecycleRooom(deskid);
            return true;
        }

        /// <summary>
        /// 进入厅
        /// </summary>
        public override bool InTing(PlayerDataAsyc pl, int roomid,ref string errinfo, ref int errorcode, int deskid = -1, string pwd = "", bool isClub = false) {
            Debug.Log("有人来排队:" + pl.socketUtil);

            //添加到排队
            return queueControll.AddPlayer(pl, roomid, deskid);
        }

        /// <summary>
        /// 定时器处理
        /// </summary>
        public override void SecondTime() {
            //处理排队--匹配
            queueControll.DoQueue();

            //定时器添加机器人,比如牛牛，需要不停的去添加机器人。
            TimeingAddRobot();

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

        /// <summary>
        /// 金币场准备
        /// </summary>
        /// <param name="pl"></param>
        public override void Ready(PlayerDataAsyc pl) {
            return;
        }

    }
}
