package main

import (
	"net/http"
	redismgr "server/db/redis"
	"server/msg"

	"github.com/gin-gonic/gin"
	"github.com/name5566/leaf/log"
)

func ReqRegister(c *gin.Context) {
	req := c.MustGet("protobuf_data").(*msg.ReqRegister)

	log.Debug(req.Account, req.Password)

	userId, _ := redismgr.GetUserIDFromRedisByAP(req.Account, req.Password)

	if userId != "" {
		c.ProtoBuf(http.StatusOK, &msg.ResRegister{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  " Account is exist！",
			},
		})
		return
	}

	userId, Success := redismgr.SetUserIDFromRedisByAP(req.Account, req.Password)
	if !Success {
		c.ProtoBuf(http.StatusOK, &msg.ResRegister{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 102,
				ErrorMsg:  "server registerHandler error",
			},
		})
		return
	}

	redismgr.SaveUserInfoToRedis(&msg.UserInfo{
		UserId: userId,
		MCoin:  0,
		MHead:  req.MHead,
		Name:   req.NikeName,
	})
	c.ProtoBuf(http.StatusOK, &msg.ResRegister{
		Success: true,
		ErrMsg:  nil,
	})
}
