package org.dromara.web.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.web.utils.SecurityUtils;
import org.dromara.work.domain.bo.*;
import org.dromara.work.domain.vo.*;
import org.dromara.work.service.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页接口
 * @Author: Maosw
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/index")
@Tag(name = "首页接口")
public class ApiIndexController {

    private final ITpPictureService tpPictureService;

    private final ITpCategoryService tpCategoryService;

    private final ITpWorksNumService tpWorksNumService;

    private final ITpDynamicsNumService tpDynamicsNumService;

    private final ITpCommentsService tpCommentsService;

    private final ITpFollowService tpFollowService;

    /**
     * 分页获取首页轮播/作品/动态列表
     */
    @GetMapping("/tpPictureList")
    public R<IPage<TpPictureVo>> tpPictureList(TpPictureBo bo, PageQuery pageQuery) {
        bo.setStatus(1);
        return R.ok(tpPictureService.selectPageList(bo, null, pageQuery));
    }

    /**
     * 分页获取首页轮播/作品/动态列表(需要登录)
     */
    @GetMapping("/tpPictureListLogin")
    public R<IPage<TpPictureVo>> tpPictureListLogin(TpPictureBo bo, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        bo.setStatus(1);
        return R.ok(tpPictureService.selectPageList(bo, userId, pageQuery));
    }

    /**
     * 分页获取我的作品/动态列表
     */
    @GetMapping("/myTpPictureList")
    public R<IPage<TpPictureVo>> myTpPictureList(TpPictureBo bo, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        bo.setStatus(1);
        return R.ok(tpPictureService.selectPageMyList(bo, pageQuery));
    }

    /**
     * 获取轮播/作品/动态详情
     */
    @GetMapping("/tpPictureDetail/{id}")
    public R<TpPictureVo> tpPictureDetail(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(tpPictureService.queryById(id));
    }

    /**
     * 发布作品/动态
     */
    @RepeatSubmit()
    @PostMapping("/addTpPicture")
    public R<Long> addTpPicture(@Validated(AddGroup.class) @RequestBody TpPictureBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpPictureService.insertByBo(bo));
    }

    /**
     * 删除我的作品/动态
     */
    @DeleteMapping("/deleteTpPicture/{ids}")
    public R<Boolean> deleteTpPicture(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Long[] ids) {
        return R.ok(tpPictureService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取全部分类
     * @param bo
     * @return
     */
    @GetMapping("/tpCategoryList")
    public R<List<TpCategoryVo>> tpCategoryList(TpCategoryBo bo) {
        List<TpCategoryVo> list = tpCategoryService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 动态点赞/收藏
     */
    @RepeatSubmit()
    @PostMapping("/addTpDynamicsNum")
    public R<Boolean> addTpDynamicsNum(@Validated(AddGroup.class) @RequestBody TpDynamicsNumBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpDynamicsNumService.insertByBo(bo));
    }

    /**
     * 删除我的点赞/收藏的动态
     */
    @PostMapping("/deleteTpDynamicsNum")
    public R<Boolean> deleteTpDynamicsNum(@RequestBody TpDynamicsNumBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpDynamicsNumService.deleteTpDynamicsNum(bo));
    }

    /**
     * 分页查询我点赞/收藏的动态
     */
    @GetMapping("/myTpDynamicsNumList")
    public R<IPage<TpDynamicsNumVo>> myTpDynamicsNumList(TpDynamicsNumBo bo, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpDynamicsNumService.selectPageList(bo, pageQuery));
    }

    /**
     * 作品点赞/收藏
     */
    @RepeatSubmit()
    @PostMapping("/addTpWorksNum")
    public R<Boolean> addTpWorksNum(@Validated(AddGroup.class) @RequestBody TpWorksNumBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpWorksNumService.insertByBo(bo));
    }

    /**
     * 删除我的点赞/收藏的作品
     */
    @PostMapping("/deleteTpWorksNum")
    public R<Boolean> deleteTpWorksNum(@RequestBody TpWorksNumBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpWorksNumService.deleteTpWorksNum(bo));
    }

    /**
     * 分页查询我点赞/收藏的作品
     */
    @GetMapping("/myTpWorksNumList")
    public R<IPage<TpWorksNumVo>> myTpWorksNumList(TpWorksNumBo bo, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpWorksNumService.selectPageList(bo, pageQuery));
    }

    /**
     * 根据作品ID查询我是否点赞/收藏了该作品
     */
    @PostMapping("/getTpWorksNumById")
    public R<Boolean> getTpWorksNumById(@RequestBody TpWorksNumBo bo) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpWorksNumService.selectOneByUserId(bo.getId(), bo.getType(), userId));
    }

    /**
     * 根据动态ID查询我是否点赞/收藏了该动态
     */
    @PostMapping("/getTpDynamicsNumById")
    public R<Boolean> getTpDynamicsNumById(@RequestBody TpDynamicsNumBo bo) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(tpDynamicsNumService.selectOneByUserId(bo.getId(), bo.getType(), userId));
    }


    /**
     * 分页查询评论列表
     */
    @GetMapping("/tpCommentsList")
    public R<IPage<TpCommentsVo>> tpCommentsList(TpCommentsBo bo, PageQuery pageQuery) {
        return R.ok(tpCommentsService.selectPageList(bo, pageQuery));
    }

    /**
     * 发布评论
     */
    @RepeatSubmit()
    @PostMapping("/addTpComments")
    public R<Boolean> addTpComments(@Validated(AddGroup.class) @RequestBody TpCommentsBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpCommentsService.insertByBo(bo));
    }

    /**
     * 删除我的评论
     */
    @DeleteMapping("/deleteTpComments/{ids}")
    public R<Boolean> deleteTpComments(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Long[] ids) {
        return R.ok(tpCommentsService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 关注
     */
    @RepeatSubmit()
    @PostMapping("/addTpFollow")
    public R<Boolean> addTpFollow(@Validated(AddGroup.class) @RequestBody TpFollowBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpFollowService.insertByBo(bo));
    }

    /**
     * 取消关注
     */
    @PostMapping("/deleteTpFollow")
    public R<Boolean> deleteTpFollow(@RequestBody TpFollowBo bo) {
        Long userId = SecurityUtils.getUserId();
        bo.setUserId(userId);
        return R.ok(tpFollowService.deleteTpFollow(bo));
    }

    /**
     * 分页查询我关注的人的动态
     */
    @GetMapping("/myTpFollowList")
    public R<IPage<TpPictureVo>> myTpFollowList(PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        List<Long> ids = tpFollowService.queryListIds(userId);
        if (CollUtil.isEmpty(ids)) {
            return R.fail("没有关注的人");
        }
        return R.ok(tpPictureService.queryPageListByIds(ids, userId, pageQuery));
    }

    /**
     * 根据用户ID查询我是否关注了该用户
     */
    @GetMapping("/getTpFollow/{id}")
    public R<Boolean> getTpFollow(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        TpFollowBo bo = new TpFollowBo();
        bo.setUserId(userId);
        bo.setToUserId(id);
        return R.ok(tpFollowService.exists(bo));
    }

    /**
     * 根据id获取作品的点赞数、收藏数、评论数
     */
    @GetMapping("/getTpPictureNum/{id}")
    public R<TpPictureNumVo> getTpPictureNum(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        TpPictureNumVo vo = new TpPictureNumVo();
        Long dynamicsNum = tpWorksNumService.queryListCounr(id,1);
        Long collectNum = tpWorksNumService.queryListCounr(id,2);
        Long commentsNum = tpCommentsService.queryListCounr(id,1);
        vo.setDynamicsNum(dynamicsNum);
        vo.setCollectNum(collectNum);
        vo.setCommentsNum(commentsNum);
        return R.ok(vo);
    }


    /**
     * 根据id获取作品的点赞数、收藏数、评论数(需要登录)
     */
    @GetMapping("/getTpPictureNumLogin/{id}")
    public R<TpPictureNumVo> getTpPictureNumLogin(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        TpPictureNumVo vo = new TpPictureNumVo();
        Long dynamicsNum = tpWorksNumService.queryListCounr(id,1);
        Long collectNum = tpWorksNumService.queryListCounr(id,2);
        Long commentsNum = tpCommentsService.queryListCounr(id,1);
        //查询我是否点赞了该作品
        Boolean dynamics = tpWorksNumService.selectOneByUserId(id, 1, userId);
        //查询我是否收藏了该作品
        Boolean collect = tpWorksNumService.selectOneByUserId(id, 2, userId);
        vo.setDynamics(dynamics);
        vo.setCollect(collect);
        vo.setDynamicsNum(dynamicsNum);
        vo.setCollectNum(collectNum);
        vo.setCommentsNum(commentsNum);
        return R.ok(vo);
    }

    /**
     * 根据id获取动态的点赞数、收藏数、评论数
     */
    @GetMapping("/getTpPictureCount/{id}")
    public R<TpPictureNumVo> getTpDynamicsNum(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        TpPictureNumVo vo = new TpPictureNumVo();
        Long dynamicsNum = tpDynamicsNumService.queryListCounr(id,1);
        Long collectNum = tpDynamicsNumService.queryListCounr(id,2);
        Long commentsNum = tpCommentsService.queryListCounr(id,2);
        vo.setDynamicsNum(dynamicsNum);
        vo.setCollectNum(collectNum);
        vo.setCommentsNum(commentsNum);
        return R.ok(vo);
    }

    /**
     * 根据id获取动态的点赞数、收藏数、评论数(需要登录)
     */
    @GetMapping("/getTpPictureCountLogin/{id}")
    public R<TpPictureNumVo> getTpPictureCountLogin(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        TpPictureNumVo vo = new TpPictureNumVo();
        Long dynamicsNum = tpDynamicsNumService.queryListCounr(id,1);
        Long collectNum = tpDynamicsNumService.queryListCounr(id,2);
        Long commentsNum = tpCommentsService.queryListCounr(id,2);
        //查询我是否点赞了该动态
        Boolean dynamics = tpDynamicsNumService.selectOneByUserId(id, 1, userId);
        //查询我是否收藏了该动态
        Boolean collect = tpDynamicsNumService.selectOneByUserId(id, 2, userId);
        vo.setDynamics(dynamics);
        vo.setCollect(collect);
        vo.setDynamicsNum(dynamicsNum);
        vo.setCollectNum(collectNum);
        vo.setCommentsNum(commentsNum);
        return R.ok(vo);
    }

}
