package com.YuyeTech.TPlat.game.gameInfo.service;

import com.YuyeTech.TPlat.dataobject.RecentlyPlaying;
import com.YuyeTech.TPlat.dto.GameDTO;
import com.YuyeTech.TPlat.game.gameInfo.repository.GameInfoRepository;
import com.YuyeTech.TPlat.repository.RecentlyPlayingRepository;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:slambb
 * @date:2020/1/15
 */
@Service
@Slf4j
public class RecentlyPlayingServiceImpl implements RecentlyPlayingService {
    @Autowired
    private RecentlyPlayingRepository recentlyPlayingRepository;

    @Autowired
    private GameInfoRepository gameInfoRepository;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private FavoritesService favoritesService;

    @Autowired
    private GameTagReferenceService gameTagReferenceService;

    @Override
    public Map findByUserId(String userId, Integer recentlyType, Pageable pageable) {
        List<String> objectId = recentlyPlayingRepository.findByUserId(userId, pageable).stream()
                .map(e -> e.getFObjectId()).collect(Collectors.toList());
        if (objectId.size() == 0) return null;

        Map map = new HashMap();
        //游戏返回对应的列表
        if (recentlyType == 0) {
            List<GameDTO> gameInfos = gameInfoRepository.GIRepFindByGameIdInOrderByField(objectId).stream().map(e -> new GameDTO(
                    e.getGameId(),
                    e.getGameName(),
                    aliyunOSSUtil.addDomainName(e.getGameIcon()),
                    e.getGameDescription(),
                    aliyunOSSUtil.addDomainName(e.getGamePicture()),
                    favoritesService.findByUserIdAndFObjectId(userId, e.getGameId()),
                    e.getGameRankingId(),
                    e.getGameRankingShow().equals(1),
                    e.getCreateTime(),
                    gameTagReferenceService.findGameTagByGameId(e.getGameRankingId(), e.getGameId()),
                    e.getPlatform()
            )).collect(Collectors.toList());
            map.put("gameList", gameInfos);
        }

        return map;
    }

    @Override
    public Map findByUserIdAndPlatform(String userId, Integer recentlyType, Integer platform, Pageable pageable) {
        List<String> objectId = recentlyPlayingRepository.findByUserId(userId, pageable).stream()
                .map(e -> e.getFObjectId()).collect(Collectors.toList());
        if (objectId.size() == 0) return null;

        Map map = new HashMap();
        //游戏返回对应的列表
        if (recentlyType == 0) {
            List<GameDTO> gameInfos = gameInfoRepository.GIRepFindByGameIdInOrderByField(objectId).stream()
                    .filter(e->(e.getPlatform().equals(platform) || e.getPlatform().equals(2)) && e.getGameStatus().equals(1) ).map(e -> new GameDTO(
                    e.getGameId(),
                    e.getGameName(),
                    aliyunOSSUtil.addDomainName(e.getGameIcon()),
                    e.getGameDescription(),
                    aliyunOSSUtil.addDomainName(e.getGamePicture()),
                    favoritesService.findByUserIdAndFObjectId(userId, e.getGameId()),
                    e.getGameRankingId(),
                    e.getGameRankingShow().equals(1),
                    e.getCreateTime(),
                    gameTagReferenceService.findGameTagByGameId(e.getGameRankingId(), e.getGameId()),
                    e.getPlatform()
            )).collect(Collectors.toList());
            map.put("gameList", gameInfos);
        }

        return map;
    }

    @Override
    public RecentlyPlaying addRecentlyItem(RecentlyPlaying recentlyItem) {
        return recentlyPlayingRepository.save(recentlyItem);
    }

    @Override
    public RecentlyPlaying findByUserIdAndFObjectId(String userId, String fObjectId) {
        RecentlyPlaying recentlyPlaying = recentlyPlayingRepository.findByUserIdAndFObjectId(userId, fObjectId).orElse(null);

        return recentlyPlaying;
    }

}
