package com.YuyeTech.HeartRate.game.gameInfo.controller;

import com.YuyeTech.HeartRate.VO.ResultVO;
import com.YuyeTech.HeartRate.common.pojo.Result;
import com.YuyeTech.HeartRate.config.GameConfigTime;
import com.YuyeTech.HeartRate.game.gameInfo.projo.GameCategory;
import com.YuyeTech.HeartRate.form.GameInfoForm;
import com.YuyeTech.HeartRate.game.gameInfo.service.GameCategoryService;
import com.YuyeTech.HeartRate.game.gameInfo.service.RecommendService;
import com.YuyeTech.HeartRate.game.gameInfo.projo.GameInfo;
import com.YuyeTech.HeartRate.dto.GameDTO;
import com.YuyeTech.HeartRate.game.gameInfo.service.GameInfoService;
import com.YuyeTech.HeartRate.utils.AliyunOSSUtil;
import com.YuyeTech.HeartRate.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author:slambb
 * @date:2020/1/10
 */
@RestController
@RequestMapping("/game")
@Slf4j
public class GameInfoController {
    @Autowired
    private GameInfoService gameInfoService;

    @Autowired
    private GameCategoryService gameCategoryService;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private GameConfigTime gameConfig;

    @GetMapping("/list")
    @Deprecated
    public ResultVO getAllGame(
    ) {
        List<GameInfo> gameInfos = gameInfoService.findAll();

        for (GameInfo gameInfo : gameInfos) {
            //修改url，添加访问域名

            String gameIcon = gameInfo.getGameIcon();
            if (gameIcon != null) {
                String urlIcon = aliyunOSSUtil.addDomainName(gameIcon);
                gameInfo.setGameIcon(urlIcon);
            }

            String gamePicture = gameInfo.getGamePicture();
            if (gamePicture != null) {
                String urlPicture = aliyunOSSUtil.addDomainName(gamePicture);
                gameInfo.setGamePicture(urlPicture);
            }

        }

        return ResultVOUtil.success(gameInfos);

    }

    /**
     * 获取游戏详情
     * @param gameId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO getGameDetail(
            @RequestParam(value = "gameId") String gameId
    ) {
        Map map = gameInfoService.findByGameDetail(gameId,true);

        return ResultVOUtil.success(map);

    }

    @PostMapping("/add_dontLogin")
    @Deprecated
    public ResultVO addGameInfo(@Valid GameInfoForm gameInfoForm, HttpServletRequest request) {

        //文件上传
        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = mrequest.getFiles("gamePicture");
            Iterator<MultipartFile> iter = files.iterator();
            while (iter.hasNext()) {
                MultipartFile photo = iter.next();
                // 现在有文件上传
                if (photo != null) {
                    System.out.println("【*** 文件上传 ***】 name= test");
                    System.out.println("【*** 文件上传 ***】 photoName="
                            + photo.getName());
                    System.out.println("【*** 文件上传 ***】 photoContentType="
                            + photo.getContentType());
                    System.out.println("【*** 文件上传 ***】 photoSize="
                            + photo.getSize());
//                    try {
//                        photo.getInputStream();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
                }
            }
        }
        //1.向gameInfo 添加数据
        GameInfo gameInfo = new GameInfo();
        BeanUtils.copyProperties(gameInfoForm, gameInfo);
        GameInfo gameInfoTemp = gameInfoService.addGameInfo(gameInfo);
        //TODO 2. pictures 里面添加图片，获取图片id，
        //TODO 3. 把游戏id，和图片id，关联起来。在关系表中添加数据 gamePicture


        return ResultVOUtil.success(gameInfoTemp);
    }

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

        if(StringUtils.isEmpty(endTime)){
            endTime = gameConfig.getEndTime();
        }
        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.ASC,"createTime"));
        Map map = recommendService.findByRecommendType(userId,recommendType,endTime,request);

        return ResultVOUtil.success(map);
    }

    @GetMapping("/recommend_list_platform")
    public ResultVO getRecommendAllGame(@RequestParam(value = "recommendType", required = false) Integer recommendType,
                                        @RequestParam(value = "userId", required = false) String userId,
                                        @RequestParam(value = "platform", required = false) Integer platform,
                                        @RequestParam(value = "endTime", required = false) String endTime,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "size", defaultValue = "10") Integer size) {

        if(StringUtils.isEmpty(endTime)){
            endTime = gameConfig.getEndTime();
        }
        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.ASC,"createTime"));
        Map map = recommendService.findByRecommendTypeAndPlatform(userId,recommendType,platform,endTime,request);

        return ResultVOUtil.success(map);
    }

    /**
     * 获取游戏分类
     * @return
     */
    @GetMapping("/game_category_list")
    public ResultVO getGameCategory(){
       List<GameCategory> gameCategoryList =  gameCategoryService.findAll();
        return ResultVOUtil.success(gameCategoryList);
    }
    /**
     * 根据游戏类目，获取游戏列表
     * 废弃
     * @return
     */
    @GetMapping("/list_from_category")
    @Deprecated
    public ResultVO getGameListFromCategory(@RequestParam(value = "categoryType", required = false) Integer categoryType,
                                            @RequestParam(value = "userId", required = false) String userId,
                                            @RequestParam(value = "endTime", required = false) String endTime,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "size", defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(endTime)){
            endTime = gameConfig.getEndTime();
        }
        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size);
        List<GameDTO> gameDTOList =  gameInfoService.findList(userId,categoryType,endTime,request);

        return ResultVOUtil.success(gameDTOList);
    }


    /**
     * 获取游戏列表
     * @param categoryType 所属分类
     * @param userId
     * @param platform 平台
     * @param endTime
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list_from_category_platform")
    @Deprecated
    public ResultVO getGameListFromCategoryAndPlatform(@RequestParam(value = "categoryType", required = false) Integer categoryType,
                                            @RequestParam(value = "userId", required = false) String userId,
                                            @RequestParam(value = "platform",required = false) Integer platform,
                                            @RequestParam(value = "endTime", required = false) String endTime,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "size", defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(endTime)){
            endTime = gameConfig.getEndTime();
        }
        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size);
        List<GameDTO> gameDTOList =  gameInfoService.findListByTypeAndPlatform(userId,categoryType,platform,endTime,request);

        return ResultVOUtil.success(gameDTOList);
    }

    /**
     * 主要根据tag返回
     * @param userId
     * @param platform
     * @param categoryType
     * @param tagId
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/listByTag")
    public Result getGameByTag(@RequestParam(value = "userId", required = false) String userId,
                               @RequestParam(value = "platform",required = false) Integer platform,
                               @RequestParam(value = "categoryType", required = false) Integer categoryType,
                               @RequestParam(value = "tagId", required = false) Integer tagId,
                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "size", defaultValue = "10") Integer size){

        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size);
        List<GameDTO> gameDTOList =  gameInfoService.findListByTag(userId,categoryType,platform,tagId,request);


        return Result.of(gameDTOList);
    }

    @GetMapping("/list_by_ranking_show")
    public ResultVO getGameListByRankingShow(@RequestParam(value = "userId", required = false) String userId,
                                            @RequestParam(value = "rankingShow", required = false) Integer rankingShow,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "size", defaultValue = "10") Integer size){

        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size);
        List<GameDTO> gameDTOList =  gameInfoService.findAllByRankingShow(userId,rankingShow,request);

        return ResultVOUtil.success(gameDTOList);
    }

    @GetMapping("/list_by_ranking_show_and_platform")
    public ResultVO getGameListByRankingShowAndPlatform(@RequestParam(value = "userId", required = false) String userId,
                                             @RequestParam(value = "rankingShow", required = false) Integer rankingShow,
                                             @RequestParam(value = "platform", required = false) Integer platform,
                                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size){

        //查询对应游戏数据
        PageRequest request = PageRequest.of(page - 1, size);
        List<GameDTO> gameDTOList =  gameInfoService.findAllByRankingShowAndPlatform(userId,rankingShow,platform,request);

        return ResultVOUtil.success(gameDTOList);
    }
}
