// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package game

import (
	"context"
	"juwan-backend/app/game/rpc/pb"

	"juwan-backend/app/game/api/internal/svc"
	"juwan-backend/app/game/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListGamesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取游戏列表
func NewListGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListGamesLogic {
	return &ListGamesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListGamesLogic) ListGames(req *types.PageReq) (resp *types.GameListResp, err error) {
	if req.Limit <= 0 {
		req.Limit = 20
	}

	all, err := l.svcCtx.GameRpc.SearchGames(l.ctx, &pb.SearchGamesReq{
		Offset: req.Offset,
		Limit:  req.Limit,
	})
	if err != nil {
		logx.Errorf("ListGames err:%v", err)
		return nil, err
	}

	list := make([]types.Game, 0, len(all.Games))
	for _, v := range all.Games {
		list = append(list, types.Game{
			Id:       v.Id,
			Name:     v.Name,
			Icon:     v.Icon,
			Category: v.Category,
		})
	}
	return &types.GameListResp{
		Items: list,
		Meta: types.PageMeta{
			Total:  0,
			Offset: req.Offset,
			Limit:  req.Limit,
		},
	}, nil

}
