package io.renren.modules.app.controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import com.alibaba.fastjson.JSON;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.MemberFavoriteEntity;
import io.renren.modules.admin.entity.MemberLoginLogEntity;
import io.renren.modules.admin.service.DataBaseService;
import io.renren.modules.admin.service.MemberFavoriteService;
import io.renren.modules.admin.service.MemberLoginLogService;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.form.FavoriteForm;
import io.renren.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户
 *
 * @author huwhois
 * @email huwhois@gmail.com
 * @date 2020-07-09 09:40:13
 */
@RestController
@RequestMapping("/app/user")
@Api("user 个人中心")
public class AppUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MemberLoginLogService memberLoginLogService;
    @Autowired
    private MemberFavoriteService memberFavoriteService;
    @Autowired
    private DataBaseService dataBaseService;

    @Login
    @GetMapping("info")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok().put("user", user);
    }

    @Login
    @GetMapping("userid")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @Login
    @GetMapping("/logininfo")
    @ApiOperation("获取用户最近登录时间和ip")
    public R loginInfo(@RequestAttribute("userId") Integer userId){
        MemberLoginLogEntity loginLog = memberLoginLogService.getLoginInfo(userId);

        return R.ok().put("loginIno", loginLog);
    }
    
    @Login
    @GetMapping("/favoritelist")
    @ApiOperation("获取用户收藏分页列表")
    public R collections(@RequestAttribute("userId") Integer userId, @RequestParam Map<String, Object> params) {
        PageUtils page = memberFavoriteService.queryFavoritePage(userId, params);
        return R.ok().put("page", page);
    }

    @Login
    @PostMapping("/savefavoriteinfo")
    @ApiOperation("收藏")
    public R saveFavoriteInfo(@RequestBody FavoriteForm favorite, @RequestAttribute("userId") Integer userId) {
        // String content = favorite.toString();
        String content = JSON.toJSONString(favorite);
        
        MemberFavoriteEntity memberFavorite= new MemberFavoriteEntity();

        memberFavorite.setContent(content);
        memberFavorite.setMemberId(userId.longValue());
        memberFavorite.setDid(favorite.getId());
        memberFavorite.setType(1);

        memberFavoriteService.save(memberFavorite);

        // 材料收藏量加 1 材料id
        dataBaseService.addFavorite(favorite.getId());

        return R.ok();
    }

    /**
     * 判断是否收藏
     * @param userId
     * @param id 材料id
     * @return
     */
    @Login
    @PostMapping("/isfavorite/{id}")
    public R isFavorite(@RequestAttribute("userId") Integer userId, @PathVariable("id") Long id){
        MemberFavoriteEntity favorite = memberFavoriteService.getOneFavorite(userId.longValue(), id);
        
        if (favorite == null) {
            return R.error(1, "未收藏");
        }

        return R.ok("已收藏").put("fid", favorite.getId());
    }

    @Login
    @PostMapping("/cancelfavorite/{fid}")
    @ApiOperation("取消收藏")
    public R cancelFavorite(@PathVariable("fid") Long fid){
        memberFavoriteService.removeById(fid);

        return R.ok();
    }
    
    @Login
    @GetMapping("/recentlist")
    @ApiOperation("最近访问列表")
    public R reccent(@RequestAttribute("userId") Integer userId) {
        List<MemberFavoriteEntity> list = memberFavoriteService.getRecentList(userId);
        return R.ok().put("list", list);
    }
    
    @Login
    @PostMapping("/saverecentinfo")
    @ApiOperation("最近访问")
    public R saveReCentInfo(@RequestBody FavoriteForm favorite, @RequestAttribute("userId") Long userId) {
        Long did = favorite.getId();
        MemberFavoriteEntity memberFavoriteEntity = memberFavoriteService.getOneRecent(did, userId);

        if (memberFavoriteEntity !=null ) {
            return  R.error(1, "已保存最近浏览记录");
        } else {
            String content = JSON.toJSONString(favorite);
            
            MemberFavoriteEntity memberFavorite= new MemberFavoriteEntity();

            memberFavorite.setContent(content);
            memberFavorite.setMemberId(userId.longValue());
            memberFavorite.setDid(favorite.getId());
            memberFavorite.setType(2);

            memberFavoriteService.save(memberFavorite);

            return R.ok();
        }
    }
}
