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

namespace Server
{
    /// <summary>
    /// 朋友房管理
    /// </summary>
    public partial class FriendRoomManager
    {
        /// <summary>
        /// 正在处理的用户
        /// </summary>
        public static HashSet<int> DoingUserIds = new HashSet<int>();

        /// <summary>
        /// 检查用户是否可以进入这个竞技比赛的房间
        /// </summary>
        public class CheckClub
        {

            /// <summary>
            /// 是否可以加入竞技比赛的房间
            /// </summary>
            public bool result
            {
                get;
                private set;
            }

            /// <summary>
            /// 如果不可以加入，不可加入原因的提示语
            /// </summary>
            public string errinfo
            {
                get;
                private set;
            }

            /// <summary>
            /// 桌子id
            /// </summary>
            public readonly int clubid;

            /// <summary>
            /// 哪个用户要进入房间
            /// </summary>
            public readonly int userid;

            /// <summary>
            /// 检查竞技比赛
            /// </summary>
            /// <param name="userid">userid</param>
            /// <param name="clubid">竞技比赛id</param>
            public CheckClub(int userid, int clubid)
            {
                this.userid = userid;
                this.clubid = clubid;
            }

            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public async Task Start()
            {
                try
                {
                    var taskResult = await checkTask(userid, clubid);
                    result = taskResult.result;
                    errinfo = taskResult.errinfo;
                }
                catch (Exception e)
                {
                    Debug.Error("在检查竞技比赛房间时出现错误:" + e.Message);
                    result = false;
                    errinfo = "请求超时! 请稍后再试!";
                }
            }

            /// <summary>
            /// 检查竞技比赛是否可以加入该竞技比赛
            /// </summary>
            /// <param name="userid">用户的userid</param>

            /// <param name="clubid">要进入的桌子id</param>
            /// <returns></returns>
            Task<CheckJoinClubResult> checkTask(int userid,int clubid)
            {
                return Task.Factory.StartNew(
                    () =>
                    {
                        PackHead head = PackHead.Create(ServerPackAgreement.CheckUserJoinWanfa);
                        head.otherInt = new int[] { clubid };
                        //head.otherString = new string[] { deskid };
                        head.userid = userid;
                        string value = GlobalMQ.instance.Call_EveryOne((int)ModuleType.ClubModule, head);
                        head.Dispose();
                        return JsonPack.GetData<CheckJoinClubResult>(value);
                    }
                );
            }

        }
    }
}
