package com.YuyeTech.TPlat.controller;

import com.YuyeTech.TPlat.dataobject.RecentlyPlaying;
import com.YuyeTech.TPlat.service.RecentlyPlayingService;
import com.YuyeTech.TPlat.VO.ResultVO;
import com.YuyeTech.TPlat.enums.UserEnum;
import com.YuyeTech.TPlat.exception.UserException;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 最近在在返回接口
 *
 * @author:slambb
 * @date:2020/1/15
 */
@RestController
@RequestMapping("/recently_playing")
@Slf4j
public class RecentlyPlayingController {
    @Autowired
    private RecentlyPlayingService recentlyPlayingService;

    /**
     * 添加用户在玩列表
     *
     * @param userId
     * @param fObjectId    对象id，如游戏id
     * @param recentlyType 对象类型（0:游戏 TODO 1:视频）
     * @return
     */
    @GetMapping("/add")
    public ResultVO addRecentlyPlaying(@RequestParam(value = "userId", required = false) String userId,
                                       @RequestParam(value = "fObjectId") String fObjectId,
                                       @RequestParam(value = "recentlyType") Integer recentlyType) {
        //请求userId为空时候
        if (userId == null) {
            throw new UserException(UserEnum.USER_NOT_EXIST);
        }
        try {
            RecentlyPlaying getRecently = recentlyPlayingService.findByUserIdAndFObjectId(userId, fObjectId);
//            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式
//            Date date = new Date(System.currentTimeMillis());//获取时间
//
//            String dateString = df.format(date);//进行转换
            if (getRecently != null) {

//                tempMap.put("msg", "已收藏过");

                getRecently.setCreateTime(new Date());
               RecentlyPlaying temp =   recentlyPlayingService.addRecentlyItem(getRecently);
               if(temp!=null){
                   Map tempMap = new HashMap();
                   tempMap.put("createTime", temp.getCreateTime());
                   return ResultVOUtil.success(tempMap);
               }else{
                   throw new UserException(UserEnum.RECENTLY_ADD_ERROR);
               }

            }

            //查询对应数据
            RecentlyPlaying recentlyPlayingItem = new RecentlyPlaying();
            recentlyPlayingItem.setUserId(userId);
            recentlyPlayingItem.setFObjectId(fObjectId);
            recentlyPlayingItem.setRecentlyType(recentlyType);
            recentlyPlayingItem.setCreateTime(new Date());
            RecentlyPlaying callbackItem = recentlyPlayingService.addRecentlyItem(recentlyPlayingItem);
            if(callbackItem!=null){
                Map tempMap = new HashMap();
                tempMap.put("createTime", callbackItem.getCreateTime());
                return ResultVOUtil.success(tempMap);
            }else{
                throw new UserException(UserEnum.RECENTLY_ADD_ERROR);
            }
        } catch (UserException e) {
            return ResultVOUtil.error(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("/get")
    public ResultVO getRecentlyPlaying(@RequestParam(value = "userId", required = false) String userId,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        //请求userId为空时候
        if (userId == null) {
            throw new UserException(UserEnum.USER_NOT_EXIST);
        }
        //查询对应数据

//        Map map = recentlyPlayingService.findByUserId(userId, 0 );
        PageRequest request = PageRequest.of(page - 1, size,Sort.by(Sort.Direction.DESC,"createTime"));
        Map map = recentlyPlayingService.findByUserId(userId,0,request);

        if(map == null){
            return ResultVOUtil.error(UserEnum.USER_NOT_RECENTLY.getCode(),UserEnum.USER_NOT_RECENTLY.getMessage());
        }
        return ResultVOUtil.success(map);
    }

    @GetMapping("/get_by_platform")
    public ResultVO getRecentlyPlayingByPlatform(@RequestParam(value = "userId", required = false) String userId,
                                       @RequestParam(value = "platform", required = false) Integer platform,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        //请求userId为空时候
        if (userId == null) {
            throw new UserException(UserEnum.USER_NOT_EXIST);
        }
        //查询对应数据

//        Map map = recentlyPlayingService.findByUserId(userId, 0 );
        PageRequest request = PageRequest.of(page - 1, size,Sort.by(Sort.Direction.DESC,"createTime"));
        Map map = recentlyPlayingService.findByUserIdAndPlatform(userId,0,platform,request);

        if(map == null){
            return ResultVOUtil.error(UserEnum.USER_NOT_RECENTLY.getCode(),UserEnum.USER_NOT_RECENTLY.getMessage());
        }
        return ResultVOUtil.success(map);
    }
}
