package com.YuyeTech.TPlat.service.impl;

import com.YuyeTech.TPlat.dataobject.Favorites;
import com.YuyeTech.TPlat.repository.FavoritesServiceRepository;
import com.YuyeTech.TPlat.service.FavoritesService;
import com.YuyeTech.TPlat.dto.GameDTO;
import com.YuyeTech.TPlat.repository.GameInfoRepository;
import com.YuyeTech.TPlat.service.GameTagReferenceService;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
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/14
 */
@Service
public class FavoritesServiceImpl implements FavoritesService {
    @Autowired
    private FavoritesServiceRepository favoritesServiceRepository;

    @Autowired
    private GameInfoRepository gameInfoRepository;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private GameTagReferenceService gameTagReferenceService;

    @Override
    public Map findByUserId(String userId, Integer favoriteType, Pageable pageable) {
        List<String> objectId = favoritesServiceRepository.findByUserId(userId, pageable).stream()
                .map(e -> e.getFObjectId()).collect(Collectors.toList());

        if (objectId.size() == 0) return null;

        Map map = new HashMap();
        //游戏返回对应的列表
        if (favoriteType == 0) {
            List<GameDTO> gameInfos = gameInfoRepository.GIRepFindByGameIdIn(objectId).stream().map(e -> new GameDTO(
                    e.getGameId(),
                    e.getGameName(),
                    aliyunOSSUtil.addDomainName(e.getGameIcon()),
                    e.getGameDescription(),
                    aliyunOSSUtil.addDomainName(e.getGamePicture()),
                    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 favoriteType, Integer platform, Pageable pageable) {
        List<String> objectId = favoritesServiceRepository.findByUserId(userId, pageable).stream()
                .map(e -> e.getFObjectId()).collect(Collectors.toList());

        if (objectId.size() == 0) return null;

        Map map = new HashMap();
        //游戏返回对应的列表
        if (favoriteType == 0) {
            List<GameDTO> gameInfos = gameInfoRepository.GIRepFindByGameIdIn(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()),
                    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 Favorites addFavorite(Favorites favorites) {
        return favoritesServiceRepository.save(favorites);
    }

    @Override
    public Boolean findByUserIdAndFObjectId(String userId, String fObjectId) {

        Favorites favorites = favoritesServiceRepository.findByUserIdAndFObjectId(userId, fObjectId).orElse(null);
        if (favorites == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public String delectFavorite(String userId, String fObjectId) {
        favoritesServiceRepository.deleteFavoritesByUserIdAndFObjectId(userId, fObjectId);
        return "delete success";
    }
}
