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/param"
	mhayaLogger "github.com/mhaya/logger"
)

func (p *Controller) guide(c *mhayaGin.Context) {

	var pa param.GuideReq
	if err := c.BindJSON(&pa); err != nil {
		mhayaLogger.Warnf("if guide err {. params=%s", pa)
		code.RenderResult(c, code.Error)
		return
	}
	if pa.Close == 0 && pa.CloseReward == 0 {
		code.RenderResult(c, code.Error)
		return
	}

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

	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}

	resp := &param.GuideResp{}
	errCode := ctl.CallWait(token.TargetPath, "guide", &pa, resp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[guide] param = %v, errCode = %v", pa, errCode)
		code.RenderResult(c, errCode)
		return
	}

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