package com.YuyeTech.TPlat.game.controller;

import com.YuyeTech.TPlat.VO.ResultVO;
import com.YuyeTech.TPlat.config.GameConfigTime;
import com.YuyeTech.TPlat.game.gameInfo.projo.GameCategory;
import com.YuyeTech.TPlat.dto.TDGameDTO;
import com.YuyeTech.TPlat.game.gameInfo.service.GameCategoryService;
import com.YuyeTech.TPlat.game.gameInfo.service.GameInfoService;
import com.YuyeTech.TPlat.game.gameInfo.service.RecommendService;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import com.YuyeTech.TPlat.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

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

/**
 * 3d 视界相应的接口
 * @author:slambb
 * @date:2020/12/28
 */
@RestController
@RequestMapping("/td")
@Slf4j
public class A_TDController {
    @Autowired
    private GameInfoService gameInfoService;

    @Autowired
    private GameCategoryService gameCategoryService;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private GameConfigTime gameConfig;


    /**
     * 获取游戏分类
     * @return
     */
    @GetMapping("/game_category")
    public ResultVO getTDGameCategory(){
       List<GameCategory> gameCategoryList =  gameCategoryService.findAll();
        return ResultVOUtil.success(gameCategoryList);
    }
    /**
     * 根据游戏类目，获取游戏列表
     * @return
     */
    @GetMapping("/games_by_category")
    public ResultVO getGameListByCategory(@RequestParam(value = "categoryType", required = false) Integer categoryType,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "size", defaultValue = "10") Integer size){
        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size);
        List<TDGameDTO> gameDTOList =  gameInfoService.TDFindList(categoryType,request).stream().map(
                e->new TDGameDTO(
                        e.getGameName(),
                        aliyunOSSUtil.addDomainName(e.getGameIcon()),
                        e.getGameDescription(),
                        aliyunOSSUtil.addDomainName(e.getGamePicture()),
                        e.getGameWebUrl(),
                        e.getCreateTime()
                )
        ).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("List",gameDTOList);
        map.put("currentPage", page);
        map.put("currentSize", size);
        return ResultVOUtil.success(map);
    }

    /**
     * 获取游戏推荐列表
     * @return
     */
    @GetMapping("/game_recommend")
    public ResultVO getRecommendAllGame(@RequestParam(value = "recommendType", required = false) Integer recommendType,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "size", defaultValue = "10") Integer size) {


        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.ASC,"createTime"));
        List<TDGameDTO>  tdGameDTOS= recommendService.TDFindByRecommendType(recommendType,request).stream().map(
                e->new TDGameDTO(
                        e.getGameName(),
                        aliyunOSSUtil.addDomainName(e.getGameIcon()),
                        e.getGameDescription(),
                        aliyunOSSUtil.addDomainName(e.getGamePicture()),
                        e.getGameWebUrl(),
                        e.getCreateTime()
                )
        ).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("List",tdGameDTOS);
        map.put("currentPage", page);
        map.put("currentSize", size);
        return ResultVOUtil.success(map);
    }

    /**
     * 根据用户名搜索信息
     * @return
     */
    @GetMapping("/searchByGameName")
    public ResultVO searchByGameName(
            @RequestParam(value = "gameName")String gameName,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {

        //todo 根据游戏名搜索,查询游戏信息
            PageRequest request = PageRequest.of(page-1, size);
            List<TDGameDTO> gameInfoPage =  gameInfoService.fuzzySearchByGameName(request,gameName).stream().map(
                e->new TDGameDTO(
                        e.getGameName(),
                        aliyunOSSUtil.addDomainName(e.getGameIcon()),
                        e.getGameDescription(),
                        aliyunOSSUtil.addDomainName(e.getGamePicture()),
                        e.getGameWebUrl(),
                        e.getCreateTime()
                )
            ).collect(Collectors.toList());
            Map map = new HashMap();
            map.put("List",gameInfoPage);
            map.put("currentPage", page);
            map.put("currentSize", size);
            return ResultVOUtil.success(map);

    }
}
