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

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

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

	resp := &param.GetRankResp{}
	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "rank", &pa, resp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[rank] param = %v, errCode = %v", pa, errCode)
		code.RenderResult(c, errCode)
		return
	}

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

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

	resp := &param.GetCountryRankResp{}
	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "countryRank", nil, resp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[countryRank] param = %v, errCode = %v", nil, errCode)
		code.RenderResult(c, errCode)
		return
	}

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

func (p *Controller) weeklyRankConfig(c *mhayaGin.Context) {
	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if weeklyRankInfo token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}
	ret, ok := data.RankConfig.Get(3)
	if !ok {
		code.RenderResult(c, code.ActivityNotOpenError)
		return
	}
	resp := param.WeeklyRankConfigResp{}
	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "inviteRecord", nil, &resp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[guide] param = %v, errCode = %v", nil, errCode)
		code.RenderResult(c, errCode)
		return
	}
	resp.WeeklyTotalReward = ret.Reward
	resp.StartTime = ret.StartTime
	resp.EndTime = ret.EndTime

	ret1, ok := data.DiscreteRuleConfig.Get(models.InvitePlayer)
	if !ok {
		code.RenderResult(c, code.ActivityNotOpenError)
		return
	}

	resp.InviteReward = ret1.Reward

	ret2, ok := data.DiscreteRuleConfig.Get(models.InviteVipPlayer)
	if !ok {
		code.RenderResult(c, code.ActivityNotOpenError)
		return
	}

	resp.VipInviteReward = ret2.Reward

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