package com.yonge.cooleshow.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.bbs.dto.search.*;
import com.yonge.cooleshow.bbs.enums.ArticleEnum;
import com.yonge.cooleshow.bbs.service.*;
import com.yonge.cooleshow.bbs.vo.*;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/home")
@Api(value = "个人中心", tags = "个人中心")
public class HomeController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private BbsArticleService bbsArticleService;

    @Autowired
    private BbsReplyService replyService;

    @Autowired
    private BbsCollectService collectService;

    @Autowired
    private BbsLikeService likeService;

    @Autowired
    private BbsFollowService followService;

	@PostMapping("")
	@ApiOperation(value = "首页", notes = "传入bbsArticleSearch")
	public HttpResponseResult<PageInfo<BbsArticleVo>> home(@RequestBody BbsArticleSearch query) {
		query.setDelFlag(false);
		query.setStatus(ArticleEnum.PASS);
		IPage<BbsArticleVo> pages = bbsArticleService.selectPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}


    @PostMapping("/article")
    @ApiOperation(value = "文章列表")
    public HttpResponseResult<PageInfo<BbsArticleVo>> articleList(@RequestBody BbsArticleSearch query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            throw  new BizException("请重新登录");
        }
        query.setUserId(user.getId());

        IPage<BbsArticleVo> pages = bbsArticleService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    @PostMapping("/comment")
    @ApiOperation(value = "评论列表")
    public HttpResponseResult<PageInfo<BbsReplyVo>> commentList(@RequestBody BbsReplySearch query) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            throw  new BizException("请重新登录");
        }
        query.setUserId(user.getId());

        IPage<BbsReplyVo> pages = replyService.selectCommentPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    @PostMapping("/reply")
    @ApiOperation(value = "回复列表")
    public HttpResponseResult<PageInfo<BbsReplyVo>> replyList(@RequestBody BbsReplySearch query) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            throw  new BizException("请重新登录");
        }
        query.setUserId(user.getId());

        IPage<BbsReplyVo> pages = replyService.selectReplyPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    @PostMapping("/collect")
    @ApiOperation(value = "收藏夹列表")
    public HttpResponseResult<PageInfo<BbsCollectVo>> collectList(@RequestBody BbsCollectSearch query) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            throw  new BizException("请重新登录");
        }
        query.setUserId(user.getId());
        IPage<BbsCollectVo> pages = collectService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    @PostMapping("/like")
    @ApiOperation(value = "点赞列表")
    public HttpResponseResult<PageInfo<BbsLikeVo>> likeList(@RequestBody BbsLikeSearch query) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            throw  new BizException("请重新登录");
        }
        query.setUserId(user.getId());

        IPage<BbsLikeVo> pages = likeService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @PostMapping("/follow")
    @ApiOperation(value = "关注/粉丝列表")
    public HttpResponseResult<PageInfo<BbsFollowVo>> followList(@RequestBody @Valid BbsFollowSearch query) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            throw  new BizException("请重新登录");
        }
        query.setUserId(user.getId());

        IPage<BbsFollowVo> pages = followService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }
}