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/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
	mhayaLogger "github.com/mhaya/logger"
)

func (p *Controller) invite(c *mhayaGin.Context) {
	mhayaLogger.Debugf("execute => invite()")
	var pa param.InviteReq
	if err := c.BindJSON(&pa); err != nil {
		mhayaLogger.Warnf("if guide err {. params=%s", pa)
		code.RenderResult(c, code.Error)
		return
	}
	if pa.ParentName == "" {
		code.RenderResult(c, code.Error)
		return
	}

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

	code.RenderResult(c, code.OK)
}

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

	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if inviteRewardRatio 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
	}

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

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

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

func (p *Controller) inviteNodeReward(c *mhayaGin.Context) {
	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if inviteNodeReward 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
	}

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