package com.red.circle.other.app.command.game;

import com.red.circle.other.app.dto.clientobject.game.GamePlayHistoryCO;
import com.red.circle.other.infra.database.mongo.entity.game.UserGamePlayHistory;
import com.red.circle.other.infra.database.mongo.service.game.UserGamePlayHistoryService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 查询用户游玩历史.
 *
 * @author tf
 */
@Component
@RequiredArgsConstructor
public class GamePlayHistoryQryExe {

    private static final int RECENT_LIMIT = 5;

    private final UserGamePlayHistoryService userGamePlayHistoryService;

    public List<GamePlayHistoryCO> execute(String sysOrigin, Long userId) {
        List<UserGamePlayHistory> historyList =
            userGamePlayHistoryService.listRecent(sysOrigin, userId, RECENT_LIMIT);

        return historyList.stream()
            .filter(h -> Objects.nonNull(h.getGameInfo()))
            .map(this::toCO)
            .collect(Collectors.toList());
    }

    private GamePlayHistoryCO toCO(UserGamePlayHistory history) {
        UserGamePlayHistory.GameInfo info = history.getGameInfo();
        GamePlayHistoryCO co = new GamePlayHistoryCO();
        co.setId(info.getId());
        co.setGameId(info.getGameId());
        co.setName(info.getName());
        co.setCover(info.getCover());
        co.setGameCode(info.getGameCode());
        co.setGameMode(info.getGameMode());
        co.setGameOrigin(info.getGameOrigin());
        co.setPlayTime(history.getPlayTime());
        co.setHeight(info.getHeight());
        co.setWidth(info.getWidth());
        return co;
    }
}
