package com.red.circle.other.adapter.app.dynamic;


import com.alibaba.nacos.common.utils.StringUtils;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.IdLongCmd;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.dynamic.*;
import com.red.circle.other.app.dto.cmd.dynamic.*;
import com.red.circle.other.app.service.dynamic.DynamicService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态相关接口
 */
@Slf4j
@RestController
@RequestMapping(value = "/dynamic", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class DynamicRestController extends BaseController {

  private final DynamicService dynamicService;

  /**
   * 创建动态.
   *
   * @eo.name 创建动态.
   * @eo.url /create
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/create")
  @JsonSerialize(using = ToStringSerializer.class)
  public DynamicListCO create(@RequestBody DynamicContentAddCmd cmd) {
    return dynamicService.createDynamic(cmd);
  }

  /**
   * 删除动态.
   *
   * @eo.name 删除动态.
   * @eo.url /delete
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/delete")
  public void delete(@RequestBody @Validated IdLongCmd cmd) {
    dynamicService.deleteDynamic(cmd);
  }

  /**
   * 动态评论列表.
   *
   * @eo.name 动态评论列表.
   * @eo.url /list/comment
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/comment")
  public PageResult<DynamicCommentListCO> listDynamicComment(@Validated DynamicCommentLimitCmd cmd) {
    return dynamicService.listDynamicComment(cmd);
  }

  /**
   * 动态子评论列表.
   *
   * @eo.name 动态子评论列表.
   * @eo.url /list/comment/children
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/comment/children")
  public PageResult<DynamicCommentListCO> listDynamicChildComment(
      @Validated DynamicChildCommentLimitCmd cmd) {
    return dynamicService.listDynamicChildComment(cmd);
  }

  /**
   * 动态点赞列表.
   *
   * @eo.name 动态点赞列表.
   * @eo.url /list/like
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/like")
  public PageResult<DynamicLikeListCO> listDynamicLike(@Validated DynamicLikeLimitCmd cmd) {
    return dynamicService.listDynamicLike(cmd);
  }

  /**
   * 动态礼物列表.
   *
   * @eo.name 动态礼物列表.
   * @eo.url /list/gift
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/gift")
  public PageResult<DynamicGiftListCO> listDynamicGift(@Validated DynamicGiftLimitCmd cmd) {
    return dynamicService.listDynamicGift(cmd);
  }

  /**
   * 动态内容详情.
   *
   * @eo.name 动态内容详情.
   * @eo.url /details
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/details")
  public DynamicListCO details(@Validated IdLongCmd cmd) {
    return dynamicService.getDynamicDetails(cmd);
  }

  /**
   * 动态关注列表.
   *
   * @eo.name 动态关注列表.
   * @eo.url /list/follow
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/follow")
  public PageResult<DynamicListCO> listDynamicFollow(@Validated DynamicLimitCmd cmd) {
    return dynamicService.listDynamicFollow(cmd);
  }

  /**
   * 我的动态列表.
   *
   * @eo.name 我的动态列表.
   * @eo.url /my/list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/my/page")
  public PageResult<DynamicListCO> listMyDynamic(@Validated DynamicMyLimitCmd cmd) {
    return dynamicService.listMyDynamic(cmd);
  }

  /**
   * 最新动态列表.
   *
   * @eo.name 最新动态列表.
   * @eo.url /list/latest
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/latest")
  public PageResult<DynamicListCO> listDynamicLatest(@Validated DynamicLimitCmd cmd) {
    return dynamicService.listDynamicLatest(cmd);
  }

  /**
   * 热门动态列表.
   *
   * @eo.name 热门动态列表.
   * @eo.url /list/popular
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/popular")
  public List<DynamicListCO> listDynamicPopular(@Validated DynamicLimitCmd cmd) {
    return dynamicService.listDynamicPopular(cmd);
  }

  /**
   * 根据标签id查询动态列表.
   *
   * @eo.name 根据标签id查询动态列表.
   * @eo.url /list/by/tag
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/by/tag")
  public List<DynamicListCO> listDynamicByTagId(@Validated DynamicByTagLimitCmd cmd) {
    return dynamicService.listDynamicByTagId(cmd);
  }

  /**
   * 标签列表.
   *
   * @eo.name 标签列表.
   * @eo.url /list/tag
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/tag")
  public List<DynamicTagCO> listDynamicTag(AppExtCommand cmd) {
    return dynamicService.listDynamicTag(cmd);
  }

  /**
   * 删除评论.
   *
   * @eo.name 删除评论.
   * @eo.url /delete/comment
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/delete/comment")
  public Long deleteComment(@RequestBody @Validated AppIdLongCmd cmd) {
    return dynamicService.deleteComment(cmd);
  }

  /**
   * 评论动态或回复用户.
   *
   * @return 数量
   * @eo.name 评论动态或回复用户.
   * @eo.url /comment
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/comment")
  public DynamicCommentListCO comment(@RequestBody @Validated DynamicCommentCmd cmd) {
    return dynamicService.comment(cmd);
  }

  /**
   * 点赞评论.
   *
   * @return 数量
   * @eo.name 点赞评论.
   * @eo.url /like/comment
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/like/comment")
  public Long likeComment(@RequestBody @Validated DynamicLikeCmd cmd) {
    return dynamicService.likeComment(cmd);
  }


  /**
   * 点赞动态.
   *
   * @return 数量
   * @eo.name 点赞动态.
   * @eo.url /like
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/like")
  public Long likeDynamic(@RequestBody @Validated DynamicLikeCmd cmd) {
    return dynamicService.likeDynamic(cmd);
  }

  /**
   * 我的消息列表.
   *
   * @eo.name 我的消息列表.
   * @eo.url /list/message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list/message")
  public PageResult<DynamicMessageListCO> listDynamicMessage(@Validated DynamicLimitCmd cmd) {
    return dynamicService.listDynamicMessage(cmd);
  }

  /**
   * 删除自己全部消息.
   *
   * @eo.name 删除自己全部消息.
   * @eo.url /delete/message/all
   * @eo.method post
   * @eo.request-type formdata
   */
  @PostMapping("/delete/message/all")
  public void deleteMessageAll(AppExtCommand cmd) {
    dynamicService.deleteMessageAll(cmd);
  }

  /**
   * 将我的消息设置为已读.
   *
   * @eo.name 将我的消息设置为已读.
   * @eo.url /message/update/read
   * @eo.method post
   * @eo.request-type formdata
   */
  @PostMapping("/message/update/read")
  public void updateReadMessage(AppExtCommand cmd) {
    dynamicService.updateReadMessage(cmd);
  }

  /**
   * 删除消息.
   *
   * @eo.name 删除消息.
   * @eo.url /delete/message
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/delete/message")
  public void deleteMessage(@RequestBody @Validated IdLongCmd cmd) {
    dynamicService.deleteMessage(cmd);
  }

  /**
   * 根据标签id获得标签.
   *
   * @eo.name 根据标签id获得标签.
   * @eo.url /tag
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/tag")
  public DynamicTagCO getDynamicTag(@Validated IdLongCmd cmd) {
    return dynamicService.getDynamicTag(cmd);
  }

  /**
   * 举报.
   *
   * @eo.name 举报.
   * @eo.url /report
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/report")
  public void report(@RequestBody @Validated DynamicReportCmd cmd) {
    dynamicService.report(cmd);
  }

  /**
   * 校验发送动态权限.
   *
   * @eo.name 校验发送动态权限.
   * @eo.url /check/send/permission
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/check/send/permission")
  public Boolean checkSendPermission(AppExtCommand cmd, Long userId) {
    if (userId != null) {
        cmd.setReqUserId(userId);
    }
    return dynamicService.checkSendPermission2(cmd);
  }

  /**
   * 添加黑名单.
   *
   * @eo.name 添加黑名单.
   * @eo.url /blacklist/add
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/admin/blacklist/add")
  public void addBlacklist(@RequestBody @Validated DynamicBlacklistAddCmd cmd) {
    dynamicService.addBlacklist(cmd);
  }

  /**
   * 删除黑名单.
   *
   * @eo.name 删除黑名单.
   * @eo.url /blacklist/delete
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/admin/blacklist/delete")
  public void deleteBlacklist(@RequestBody @Validated DynamicBlacklistDelCmd cmd) {
    dynamicService.deleteBlacklist(cmd);
  }

  /**
   * 新增待审核动态
   */
  @PostMapping("/approval")
  public void addApprovalDynamic(DynamicContentAddCmd cmd) {
    dynamicService.addApprovalDynamic(cmd);
  }

}
