package v1

import (
	"context"
	"designs/app/common/request"
	"designs/config"
	"designs/global"
	"designs/response"
	"github.com/gin-gonic/gin"
)

/* 添加游戏配置 */
//http://127.0.0.1:8787/v1/user/AddGidConfig
func AddGidConfig(c *gin.Context) {
	var data GameConfig
	form := request.Check(c, &data)

	// 在这种情况下，将自动选择合适的绑定
	if data.AppSecret != config.Get("app.app_secret") {
		response.Fail(c, 1003, "密钥参数错误")
		return
	}

	gameConfigData := make(map[string]interface{})
	gameConfigData["gid"] = form.Gid
	gameConfigData["gameName"] = form.GameName
	gameConfigData["wxAppid"] = form.WxAppid
	gameConfigData["wxSecret"] = form.WxSecret
	gameConfigData["ttAppid"] = form.TtAppid
	gameConfigData["ttSecret"] = form.TtSecret
	gidKey := config.Get("app.gid") + form.Gid
	err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
	if err != nil {
		response.Fail(c, 1003, "配置错误")
		return
	}

	response.Success(c, gin.H{
		"data": gameConfigData,
	})
}

/* 获取配置 */
func GetGidConfig(c *gin.Context) {
	var data GetGameCfg
	form := request.Check(c, &data)
	if form.AppSecret != config.Get("app.app_secret") {
		response.Fail(c, 1003, "密钥不对")
	}

	gidKey := config.Get("app.gid") + "*"
	keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
	var gameData = []interface{}{}
	for _, val := range keys {
		res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
		gameData = append(gameData, res)
	}

	response.Success(c, gin.H{
		"data": gameData,
	})
}
