package com.YuyeTech.TPlat.controller;

import com.YuyeTech.TPlat.dataobject.Favorites;
import com.YuyeTech.TPlat.service.FavoritesService;
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.transaction.annotation.Transactional;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:slambb
 * @date:2020/1/14
 */
@RestController
@RequestMapping("/favorites")
@Slf4j
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;

    @GetMapping("/get")
    public ResultVO getFavorites(@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);
        }
        //查询对应数据
        PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createTime"));
        Map map = favoritesService.findByUserId(userId, 0, request);

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

        return ResultVOUtil.success(map);
    }
    @GetMapping("/get_by_platform")
    public ResultVO getFavoritesByPlatform(@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);
        }
        //查询对应数据
        PageRequest request = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createTime"));
        Map map = favoritesService.findByUserIdAndPlatform(userId, 0,platform, request);

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

        return ResultVOUtil.success(map);
    }

    /**
     * 收藏对象
     *
     * @param userId
     * @param fObjectId     收藏对象id
     * @param favoritesType 收藏类型(0 : 是游戏，)
     * @return
     */
    @GetMapping("/add")
    public ResultVO addFavorites(@RequestParam(value = "userId", required = false) String userId,
                                 @RequestParam(value = "fObjectId") String fObjectId,
                                 @RequestParam(value = "favoritesType") Integer favoritesType) {
        //请求userId为空时候
        if (userId == null) {
            throw new UserException(UserEnum.USER_NOT_EXIST);
        }

        try {
            boolean bHasFavoriteItem = favoritesService.findByUserIdAndFObjectId(userId, fObjectId);

            if (bHasFavoriteItem) {
                Map tempMap = new HashMap();
                tempMap.put("bHas", true);
                tempMap.put("msg", "已收藏过");
                return ResultVOUtil.success(tempMap);
            }

            //查询对应数据
            Favorites favorites = new Favorites();
            favorites.setUserId(userId);
            favorites.setFObjectId(fObjectId);
            favorites.setFavoritesType(favoritesType);
            favorites.setCreateTime(new Date());
            //添加后返回值 Favorites tempFavorite  =
            favoritesService.addFavorite(favorites);
            Map tempMap = new HashMap();
            tempMap.put("bHas", false);
            tempMap.put("msg", "新收藏");
//        tempMap.put("favorite",tempFavorite);
            return ResultVOUtil.success(tempMap);

        } catch (UserException e) {
            return ResultVOUtil.error(e.getCode(), e.getMessage());
        }

    }


    @GetMapping("/modify")
    @Transactional
    public ResultVO modifyFavorites(@RequestParam(value = "userId", required = false) String userId,
                                    @RequestParam(value = "fObjectId") String fObjectId,
                                    @RequestParam(value = "favoritesType") Integer favoritesType) {

        try {
            boolean bHasFavoriteItem = favoritesService.findByUserIdAndFObjectId(userId, fObjectId);

            //如果有数据
            if (bHasFavoriteItem) {
                String temp = favoritesService.delectFavorite(userId, fObjectId);
                Map tempMap = new HashMap();
                tempMap.put("bHas", false);
                tempMap.put("msg", "已删除");
                return ResultVOUtil.success(tempMap);

            }
            //没有数据，创建新的对象，写入数据库
            //查询对应数据
            Favorites favorites = new Favorites();
            favorites.setUserId(userId);
            favorites.setFObjectId(fObjectId);
            favorites.setFavoritesType(favoritesType);
            favorites.setCreateTime(new Date());
            //添加后返回值 Favorites tempFavorite  =
            favoritesService.addFavorite(favorites);
            Map tempMap = new HashMap();
            tempMap.put("bHas", true);
            tempMap.put("msg", "新收藏");
//        tempMap.put("favorite",tempFavorite);
            return ResultVOUtil.success(tempMap);

        } catch (UserException e) {
            return ResultVOUtil.error(e.getCode(), e.getMessage());
        }

    }
}
