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

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

	var pa param.DrawReq
	if err := c.BindJSON(&pa); err != nil {
		mhayaLogger.Warnf("if lottery 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 lottery token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}

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

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

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

	_, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if getLotteryConfig token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}
	var resp []param.DrawConfig
	da := data.DrawConfig.GetByType(2)
	for _, v := range da {
		resp = append(resp, param.DrawConfig{ID: v.Order, ItemID: v.Reward[0].ItemID, Num: v.Reward[0].Amount})
	}
	code.RenderResult(c, code.OK, resp)
}
