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

	var pa param.JumpReq
	if err := c.BindJSON(&pa); err != nil {
		mhayaLogger.Warnf("if guide 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 jump 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
	}

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

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

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

	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if savex token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}
	var resp param.ChangeData
	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "savex", &pa, &resp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[savex] param = %v, errCode = %v", pa, errCode)
		code.RenderResult(c, errCode)
		return
	}
	code.RenderResult(c, code.OK, resp)
}

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

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

	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if savetonwall 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
	}
	errCode := ctl.CallWait(token.TargetPath, "savetonwall", &pa, nil)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[savetonwall] param = %v, errCode = %v", pa, errCode)
		code.RenderResult(c, errCode)
		return
	}
	code.RenderResult(c, code.OK)
}
