package controller

import (
	mhayaGin "github.com/mhaya/components/gin"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
	mhayaLogger "github.com/mhaya/logger"
)

func (p *Controller) getLevel(c *mhayaGin.Context) {
	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if getLevel token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}

	rsp := &models.Player{}

	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "start", nil, rsp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[getLevel] token = %s, errCode = %v", token, errCode)
		code.RenderResult(c, errCode)
		return
	}
	var resp param.GetLevelResp
	resp.NowLevel = rsp.Level
	resp.NowInvite = rsp.Item[models.ItemInvite].CurNum
	resp.NowRoll = rsp.Item[models.ItemRoll].CurNum
	ret, ok := data.LevelConfig.Get(rsp.Level + 1)
	if ok {
		resp.NextLevel = rsp.Level + 1
		resp.NextRoll = ret.DiceNum
		resp.NextInvite = ret.Invite
	} else {
		resp.NextLevel = rsp.Level
		resp.NextInvite = rsp.Item[models.ItemInvite].CurNum
		resp.NextRoll = rsp.Item[models.ItemRoll].CurNum
	}

	code.RenderResult(c, code.OK, resp)
}
