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) share(c *mhayaGin.Context) {

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

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

	resp := &param.ChangeData{}

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

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

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