package com.ai.da.controller;

import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.*;
import com.ai.da.model.vo.CommentVO;
import com.ai.da.model.vo.PortfolioVO;
import com.ai.da.service.PortfolioService;
import com.ai.da.service.UserFollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "Portfolio模块")
@Slf4j
@RestController
@RequestMapping("/api/portfolio")
public class PortfolioController {

    @Resource
    private PortfolioService portfolioService;

    @Resource
    private UserFollowService userFollowService;

    @ApiOperation(value = "发布作品集")
    @PostMapping("/publish")
    public Response<Long> publish(@RequestParam("file") MultipartFile canvas, @RequestParam("data") String data) {
        return Response.success(portfolioService.publish(canvas, data));
    }

    @ApiOperation(value = "删除作品集")
    @GetMapping("/delete")
    public Response<Boolean> delete(@RequestParam("id") Long id) {
        return Response.success(portfolioService.delete(id));
    }

    @CrossOrigin
    @ApiOperation(value = "作品集page")
    @PostMapping("/page")
    public Response<PageBaseResponse<PortfolioVO>> page(@Valid @RequestBody QueryPortfolioPageDTO query) {
        return Response.success(portfolioService.page(query));
    }

    @CrossOrigin
    @ApiOperation(value = "作品详情")
    @PostMapping("/detail")
    public Response<PortfolioVO> detail(@Valid @RequestBody PortfolioDTO portfolioDTO) {
        return Response.success(portfolioService.detail(portfolioDTO));
    }

    @ApiOperation(value = "选择作品")
    @PostMapping("/choose")
    public Response<ProjectChooseVO> choose(@Valid @RequestBody PortfolioDTO portfolioDTO) {
        return Response.success(portfolioService.choose(portfolioDTO));
    }

    @ApiOperation(value = "更新作品")
    @PostMapping("/update")
    public Response<PortfolioVO> update(@Valid @RequestBody PortfolioDTO portfolioDTO) {
        return Response.success(portfolioService.update(portfolioDTO));
    }

    @ApiOperation(value = "广场用户注册")
    @PostMapping("/designWorksRegister")
    public Response<Boolean> designWorksRegister(@Valid @RequestBody DesignWorksRegisterDTO designWorksRegisterDTO) {
        return Response.success(portfolioService.designWorksRegister(designWorksRegisterDTO));
    }

    @ApiOperation(value = "喜欢")
    @GetMapping("/like")
    public Response<Boolean> like(@RequestParam("id") Long id) {
        return Response.success(portfolioService.like(id));
    }

    @ApiOperation(value = "不喜欢")
    @GetMapping("/unlike")
    public Response<Boolean> unlike(@RequestParam("id") Long id) {
        return Response.success(portfolioService.unlike(id));
    }

    @ApiOperation(value = "获取点赞数")
    @GetMapping("/getLikeCount")
    public Response<Long> getLikeCount(@RequestParam("id") Long id) {
        return Response.success(portfolioService.getLikeCount(id));
    }

    @ApiOperation(value = "评论")
    @PostMapping("/comment")
    public Response<Boolean> comment(@Valid @RequestBody CommentDTO commentDTO) {
        return Response.success(portfolioService.comment(commentDTO));
    }

    @ApiOperation(value = "评论列表")
    @PostMapping("/commentPage")
    public Response<PageBaseResponse<CommentVO>> commentPage(@Valid @RequestBody CommentPageDTO commentPageDTO) {
        return Response.success(portfolioService.commentPage(commentPageDTO));
    }

    @ApiOperation(value = "增加浏览量")
    @GetMapping("/viewsIncrease")
    public Response<Boolean> viewsIncrease(@RequestParam("id") Long id) {
        return Response.success(portfolioService.viewsIncrease(id));
    }

    @ApiOperation(value = "浏览量获取")
    @GetMapping("/viewsGet")
    public Response<Long> viewsGet(@RequestParam("id") Long id) {
        return Response.success(portfolioService.viewsGet(id));
    }

    @ApiOperation(value = "删除评论")
    @PostMapping("/commentDelete")
    public Response<Boolean> commentDelete(@Valid @RequestBody CommentDTO commentDTO) {
        return Response.success(portfolioService.commentDelete(commentDTO));
    }

    @ApiOperation(value = "关注")
    @GetMapping("/follow")
    public Response<String> follow(@RequestParam("followeeId") Long followeeId) {
        portfolioService.follow(followeeId);
        return Response.success(BusinessException.getMessageFromResource("subscription.success"));
    }

    @ApiOperation(value = "取消关注")
    @GetMapping("/cancelFollow")
    public Response<String> cancelFollow(@RequestParam("followeeId") Long followeeId) {
        portfolioService.cancelFollow(followeeId);
        return Response.success(BusinessException.getMessageFromResource("unsubscribe.success"));
    }

    @ApiOperation(value = "获取关注列表")
    @PostMapping("/getFolloweeList")
    public Response<List<AccountFollowVO>> getFolloweeList(@Valid @RequestBody GetFollowListDTO getFollowListDTO) {
        return Response.success(userFollowService.getFolloweeList(getFollowListDTO));
    }

    @ApiOperation(value = "获取粉丝列表")
    @PostMapping("/getFollowerList")
    public Response<List<AccountFollowVO>> getFollowerList(@Valid @RequestBody GetFollowListDTO getFollowListDTO) {
        return Response.success(userFollowService.getFollowerList(getFollowListDTO));
    }


/*    @ApiOperation(value = "按标签名查询作品")
    @GetMapping("/queryPortfolioByTag")
    public Response<List<PortfolioVO>> queryPortfolioByTag(@RequestParam(value = "tagName", required = false) String tagName,
                                                           @RequestParam(value = "tagId", required = false) Long tagId) {
        return Response.success(portfolioService.queryPortfolioByTag(tagName, tagId));
    }*/

    @ApiOperation(value = "将企业或教育版中的作品发布到公共gallery")
    @GetMapping("/toPublic")
    public Response<String> setPortfolioToPublic(@RequestParam("portfolioId") Long portfolioId) {
        portfolioService.setPortfolioToPublic(portfolioId);
        return Response.success("success");
    }
}
