package v1

import (
	"context"
	"designs/app/common/request"
	"designs/config"
	"designs/global"
	"designs/response"
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"strconv"
	"strings"
)

/* 排行类型 */
const (
	RankTypeByEver  = 1 //永久不过期
	RankTypeByDay   = 2 //日榜
	RankTypeByWeek  = 3 //周榜
	RankTypeByMonth = 4 //月榜
)

// AddUserToBlackList 将玩家加入黑名单
func AddUserToBlackList(c *gin.Context) {
	form := request.Check(c, &struct {
		Code string `json:"code" binding:"required"`
		Gid  string `json:"gid" binding:"required"`
		Pf   string `json:"pf" binding:"required"`
	}{})

	userKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.user_table_key") + form.Code

	//存在用户 读取数据
	userdata, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
	if err != nil {
		response.Fail(c, 1003, "用户数据不存在"+err.Error())
		return
	}
	curHid := userdata["hid"]

	//清除省榜数据
	rankDataKeys := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_province_table_key") + curHid + ":" + strconv.Itoa(RankTypeByEver) + ":*"
	keys, _ := global.App.Redis.Keys(context.Background(), rankDataKeys).Result()
	for _, val := range keys {
		defScore, _ := global.App.Redis.ZScore(context.Background(), val, form.Code).Result()
		lastindex := strings.LastIndex(val, ":")
		if lastindex != -1 {
			curField := val[lastindex+1:]
			//删除出省排行数据
			global.App.Redis.ZRem(context.Background(), val, form.Code)
			//处理省排行汇总数据
			rankSumDataKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_province_table_key") + "Sum" + ":" + curField
			global.App.Redis.ZIncrBy(context.Background(), rankSumDataKey, -defScore, curHid).Result()
		}
	}

	//清除排行榜数据
	rankDataKeyTotal := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_table_key") + strconv.Itoa(RankTypeByEver) + ":*"
	keysTotal, _ := global.App.Redis.Keys(context.Background(), rankDataKeyTotal).Result()
	for _, val := range keysTotal {
		lastindex := strings.LastIndex(val, ":")
		if lastindex != -1 {
			//清除排行榜数据
			global.App.Redis.ZRem(context.Background(), val, form.Code)
		}
	}

	//加入黑名单队列
	blackListKey := config.Get("app.black_list_table")
	err = global.App.Redis.ZAdd(context.Background(), blackListKey, &redis.Z{Member: userKey}).Err()
	if err != nil {
		response.Fail(c, 1003, "加入黑名单失败"+err.Error())
		return
	}

	response.Success(c, gin.H{
		"msg": "加入黑名单成功",
	})
}

// DeleteUserFormBlackList 将玩家从从黑名单中去掉
func DeleteUserFormBlackList(c *gin.Context) {
	form := request.Check(c, &struct {
		Code string `json:"code" binding:"required"`
		Gid  string `json:"gid" binding:"required"`
		Pf   string `json:"pf" binding:"required"`
	}{})

	userKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.user_table_key") + form.Code

	//存在用户 读取数据
	_, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
	if err != nil {
		response.Fail(c, 1003, "用户数据不存在"+err.Error())
		return
	}

	//从黑名单队列中删除
	blackListKey := config.Get("app.black_list_table")
	err = global.App.Redis.ZRem(context.Background(), blackListKey, userKey).Err()
	if err != nil {
		response.Fail(c, 1003, "从黑名单中移除失败"+err.Error())
		return
	}
	response.Success(c, gin.H{
		"msg": "从黑名单中移除成功",
	})
}

// ReadBlackList 查看目前黑名单有哪些人
func ReadBlackList(c *gin.Context) {
	blackListKey := config.Get("app.black_list_table")

	data, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
	if err != nil {
		response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
		return
	}

	var userData []interface{}
	for _, value := range data {
		user, err := global.App.Redis.HGetAll(context.Background(), value).Result()
		if err != nil {
			response.Fail(c, 1003, "GetUserData err"+err.Error())
			return
		}
		userData = append(userData, user)
	}

	response.Success(c, gin.H{
		"msg": "读取黑名单列表成功",
	})
}
