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

namespace Server
{
    /// <summary>
    /// 游戏管理  单线程操作
    /// </summary>
    public partial class GameManager
    {
        /// <summary>
        /// 处理返回包
        /// </summary>
        protected void DoComBack(GamePack pk)
        {
            //在桌上..开战  没开战
            //1.开战,离线处理,退出到大厅_离线

            //3.在游戏中,退出游戏_离线
            DoComBack(pk.pl);
        }

        /// <summary>
        /// 处理返回包
        /// </summary>
        /// <param name="pl"></param>
        /// <param name="isSendPack">视情况而定，不是绝对，要使用请检查逻辑</param>
        public void DoComBack(PlayerDataAsyc pl, bool isSendPack = true)
        {
            ComBack comback = GetComBack(pl);
            PlayerState state = (PlayerState)pl.state;
            //Debug.Log("处理返回包:" + state);
            switch (state)
            {
                case PlayerState.INGame:
                    comback.InGame();
                    break;
                case PlayerState.NOGame:
                    comback.NoGame();
                    break;
                case PlayerState.Desk:
                    comback.Desk();
                    break;
                case PlayerState.Ready:
                    comback.Ready();
                    break;
                case PlayerState.LookOn: //观战 徐添加
                case PlayerState.War:
                    if (!comback.War())
                    {
                        return;
                    }
                    break;
            }
        }

        private ComBack goldComback;
        private ComBack friendComback;

        /// <summary>
        /// 获取返回处理器
        /// </summary>
        /// <param name="pl"></param>
        /// <returns></returns>
        protected virtual ComBack GetComBack(PlayerDataAsyc pl)
        {
            ComBack result = null;
            if (pl.friendRoomNum > 0)
            {
                if (friendComback == null)
                    friendComback = new FriendComBack();
                result = friendComback;
            }
            else
            {
                if (goldComback == null)
                    goldComback = new GoldComBack();
                result = goldComback;
            }

            result.pl = pl;
            return result;
        }

        /// <summary>
        /// 玩家返回处理
        /// </summary>
        protected abstract class ComBack
        {
            /// <summary>
            /// 要执行返回的玩家
            /// </summary>
            public PlayerDataAsyc pl;

            /// <summary>
            /// 当没在游戏中的时候的处理
            /// </summary>
            public virtual void NoGame()
            {
                if (pl.id >= 0)
                {
                    PlayerCollection.Instance.RemovePlayer(pl);
                }
            }

            /// <summary>
            /// 当在游戏中的时候的处理
            /// </summary>
            public abstract void InGame();

            /// <summary>
            /// 当在桌上时的处理
            /// </summary>
            public abstract void Desk();

            /// <summary>
            /// 当在准备时的处理
            /// </summary>
            public abstract void Ready();

            /// <summary>
            /// 当在开战时的处理
            /// </summary>
            public abstract bool War();
        }

        /// <summary>
        /// 金币场的返回处理
        /// </summary>
        protected class GoldComBack : ComBack
        {
            /// <summary>
            /// 金币场 进入游戏时的返回处理
            /// </summary>
            public override void InGame()
            {
                instance.QuitGame(pl, "inGame");
            }

            /// <summary>
            /// 在桌上时的返回处理
            /// </summary>
            public override void Desk()
            {
                Ready();
            }

            /// <summary>
            /// 准备状态的返回处理
            /// </summary>
            public override void Ready()
            {
                instance.QuitRoom(pl);
            }

            /// <summary>
            /// 开战状态的返回处理
            /// </summary>
            public override bool War()
            {
                if (!instance.QuitRoom(pl))
                    instance.OutLine(pl);
                return true;
            }
        }

        /// <summary>
        /// 朋友房返回处理
        /// </summary>
        protected class FriendComBack : ComBack
        {
            /// <summary>
            /// 进入游戏状态的 返回处理
            /// </summary>
            public override void InGame()
            {
            }

            /// <summary>
            /// 在桌上时的返回处理
            /// </summary>
            public override void Desk()
            {
                Ready();
            }

            /// <summary>
            /// 准备状态的返回处理
            /// </summary>
            public override void Ready()
            {
                Desk desk = FriendRoomManager.instance.FindDesk(pl.friendRoomNum);
                if (desk == null)
                {
                    Debug.Error("没找到朋友房所在的房间!{0}{1}", Environment.NewLine, pl);
                    pl.friendRoomNum = -1;
                    pl.friendWeiYiMa = string.Empty;
                    pl.tingid = -1;
                    pl.roomid = -1;
                    pl.deskid = -1;
                    pl.seat = -1;
                    instance.QuitGame(pl, "Ready");
                    return;
                }

                if (desk.deskinfo.noWar)
                {
                    //没开战过的房间
                    bool isDisBank = false; //是否解散房间
                    if (desk.deskinfo.creatStyle == 0 && desk.deskinfo.creatUserid == pl.userid)
                        isDisBank = true; //房主离开 直接解散
                    else if (desk.deskinfo.creatStyle == 5)
                    {
                        //竞技比赛的房间,没人了就解散房间
                        if (desk.PlayerCnt <= 1)
                        {
                            if (desk.PlayerCnt < 1)
                            {
                                Debug.Error("怎么会有这种情况,我没有玩家了!");
                            }

                            isDisBank = true;
                        }
                    }

                    if (isDisBank)
                    {
                        FriendRoomManager.instance.DisBank(desk, 5);
                    }
                    else
                    {
                        Debug.Info("普通玩家离开房间");
                        //退出房间
                        instance.friendRoom.Quit(pl);
                        Debug.Log("从朋友房退出逻辑!");
                        instance.QuitGame(pl, "Ready1");


                        if (desk.deskinfo.creatStyle == 5)
                        {
                            //WayRoom.instance.DecScore(dsk.deskinfo.creatUserid, dsk.deskinfo.wayid, dsk.deskinfo.weiyima);

                            WayManager.instance.ClubWayPlayerChange(desk.deskinfo.creatUserid, desk.deskinfo.wayid,
                                desk.deskinfo.weiyima, false);
                            Debug.Info("桌子删除!");
                            WayManager.instance.WayDeskChangeSend(desk.deskinfo.creatUserid, desk.deskinfo.wayid,
                                desk.deskinfo.weiyima, 2);
                            /*
                            int clubid = desk.deskinfo.creatUserid;
                            string wayid = desk.deskinfo.wayid;
                            string weiyima = desk.deskinfo.weiyima;
                            int cnt = desk.deskinfo.rule.minCnt;
                            Task.Factory.StartNew(
                                () => {
                                    WayRoom.instance.DecScore(clubid, wayid, weiyima);
                                    var dt = WayRoom.instance.ClubWayPlayerChange(clubid, wayid, cnt);
                                    ServerHead head = new ServerHead(ServerPackAgreement.ClubPlayWayPlChange);
                                    GlobalMQ.instance.DeliveryEveryOne((int)ModuleType.ClubModule, GlobalMQ.CreateMessage(head, dt), true);
                                }
                                );
                                */
                        }

                        Debug.Info("玩家退出房间:" + desk.deskinfo);
                    }

                    return;
                }

                //开战过,离线处理
                instance.OutLine(pl);
            }

            /// <summary>
            /// 开战状态的返回处理
            /// </summary>
            public override bool War()
            {
                Debug.Warning("朋友房已经开战，不让退出!");
                //instance.OutLine(pl);
                return false;
            }
        }
    }
}