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.BbsReplySearch;
import com.yonge.cooleshow.bbs.enums.ArticleEnum;
import com.yonge.cooleshow.bbs.valid.AddGroup;
import com.yonge.cooleshow.bbs.valid.OtherGroup;
import com.yonge.cooleshow.bbs.valid.UpdateGroup;
import com.yonge.cooleshow.bbs.vo.BbsAuthRecordVo;
import com.yonge.cooleshow.bbs.vo.BbsReplyVo;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;

import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.bbs.vo.BbsArticleVo;
import com.yonge.cooleshow.bbs.dto.search.BbsArticleSearch;
import com.yonge.cooleshow.bbs.entity.BbsArticle;
import com.yonge.cooleshow.bbs.service.BbsArticleService;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bbsArticle")
@Api(value = "文章", tags = "文章")
public class BbsArticleController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private BbsArticleService bbsArticleService;

    @PostMapping("/addArticle")
    @ApiOperation(value = "详情-发布文章")
    public HttpResponseResult addArticle(@Validated(AddGroup.class) @RequestBody BbsArticle bbsArticle) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        bbsArticle.setUserId(user.getId());
        bbsArticleService.addArticle(bbsArticle);
        return succeed();
    }

    @PostMapping("/updateArticle")
    @ApiOperation(value = "详情-修改文章")
    public HttpResponseResult updateArticle(@Validated(UpdateGroup.class) @RequestBody BbsArticle bbsArticle) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        bbsArticleService.updateArticle(bbsArticle, user.getId());
        return succeed();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", dataType = "List", value = "文章id集合"),
    })
    @PostMapping("/deleteArticle")
    @ApiOperation(value = "详情-删除文章")
    public HttpResponseResult deleteArticle(@RequestBody Map<String, List<Long>> ids) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        bbsArticleService.deleteArticle(ids, user);
        return succeed();
    }

    @PostMapping("/top")
    @ApiOperation(value = "后台管理-置顶文章")
    public HttpResponseResult articleTop(@Validated(OtherGroup.class) @RequestBody BbsArticle article) {
        bbsArticleService.articleTop(article);
        return succeed();
    }

    @GetMapping("/info")
    @ApiOperation(value = "详情-文章")
    public HttpResponseResult<BbsArticleVo> articleInfo(@NotNull Long articleId) {
        return succeed(bbsArticleService.articleInfo(articleId));
    }

    @PostMapping("/reply")
    @ApiOperation(value = "详情-文章评论列表")
    public HttpResponseResult<PageInfo<BbsReplyVo>> articleReply(@RequestBody BbsReplySearch query) {
        IPage<BbsReplyVo> pages =bbsArticleService.articleReply(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }
}