using System;
using System.Collections.Generic;
using System.Linq;
using Server.DB.Redis;
using System.Text;
using StackExchange.Redis;
using MrWu.Debug;

namespace GameDAL {
    /// <summary>
    /// 竞技比赛的 禁止玩家游玩
    /// </summary>
    public class ClubBankPlayerInGame {

        public const int DbIdx = 6;

        private ClubBankPlayerInGame() { }

        static ClubBankPlayerInGame() {
            instance = new ClubBankPlayerInGame();
        }

        public static ClubBankPlayerInGame instance {
            get;
            private set;
        }

        /// <summary>
        /// 数据库单项操作
        /// </summary>
        public IDatabaseProxy dbp {
            get {
                return redisManager.Getdb(DbIdx);
            }
        }

        /// <summary>
        /// 数据库操作
        /// </summary>
        public IDatabase db {
            get {
                return redisManager.GetDataBase(DbStyle.main, DbIdx);
            }
        }

        /*
         * 数据结构
         * 
         * ClubBankPlayers:clubid [243633,243663]
         * ClubBankPlayers:clubid [243634,246338]
         * 
         * */
        public const string baseKey = "ClubBankPlayers";

        /// <summary>
        /// 获取竞技比赛的 禁止同桌 所有集合的 key
        /// </summary>
        /// <param name="clubid"></param>
        /// <returns></returns>
        public string GetClubBankKey(int clubid) {
            return $"{baseKey}:{clubid}";
        }

        public List<int> GetAll(int clubId)
        {
            string key = GetClubBankKey(clubId);
            var values = dbp.SetMembers(key);
            var ret = values.Select(x=>int.Parse(x)).ToList();
            return ret;
        }

        /// <summary>
        /// 添加一个禁止游玩的玩家 (过期时间一年)
        /// </summary>
        /// <param name="clubId">竞技比赛id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public bool Add(int clubId, int userId) {
            string key = GetClubBankKey(clubId);
            var isSuccess = dbp.SetAdd(key, userId);
            db.KeyExpire(key, TimeSpan.FromDays(365));
            return isSuccess;
        }

        /// <summary>
        /// 移除玩家限制
        /// <param name="clubId">竞技比赛id</param>
        /// <param name="userId">用户id</param>
        /// </summary>
        public bool Remove(int clubId, int userId) {
            string key = GetClubBankKey(clubId);
            var isSuccess = dbp.SetRemove(key, userId);
            db.KeyExpire(key, TimeSpan.FromDays(365));
            return isSuccess;
        }
        
        /// <summary>
        /// 移除
        /// </summary>
        public bool Remove(int clubId) {
            string key = GetClubBankKey(clubId);
            var isSuccess = dbp.KeyDelete(key);
            return isSuccess;
        }
        
        /// <summary>
        /// 是否包含玩家
        /// <param name="clubId">竞技比赛id</param>
        /// <param name="userId">用户id</param>
        /// </summary>
        public bool Contains(int clubId, int userId) {
            string key = GetClubBankKey(clubId);
            return dbp.SetContains(key, userId);
        }
    }
}
