package com.slodon.b2b2c.controller.video.admin;

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.video.AuthorModel;
import com.slodon.b2b2c.model.video.VideoSettingModel;
import com.slodon.b2b2c.video.example.AuthorExample;
import com.slodon.b2b2c.video.pojo.Author;
import com.slodon.b2b2c.vo.video.AuthorVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "admin-作者管理")
@RestController
@RequestMapping("v3/video/admin/video/author")
public class AdminAuthorController extends BaseController {

    @Resource
    private AuthorModel authorModel;
    @Resource
    private VideoSettingModel videoSettingModel;

    @ApiOperation("作者列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "作者类型:audit-作者审核,默认不传", paramType = "query"),
            @ApiImplicitParam(name = "memberName", value = "会员名称", paramType = "query"),
            @ApiImplicitParam(name = "memberNickname", value = "会员昵称", paramType = "query"),
            @ApiImplicitParam(name = "permissionState", value = "发布权限状态:1-待审核，2-审核通过(正常发布)，3-审核拒绝，4-禁止发布", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<AuthorVO>> list(HttpServletRequest request, String memberName, String memberNickname,
                                             Integer permissionState, String type) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        AuthorExample example = new AuthorExample();
        example.setMemberNameLike(memberName);
        example.setMemberNicknameLike(memberNickname);
        if (!StringUtil.isEmpty(type) && "audit".equals(type)) {
            if (!StringUtil.isNullOrZero(permissionState)) {
                example.setPermissionState(permissionState);
            } else {
                example.setPermissionStateIn(VideoConst.PERMISSION_STATE_1 + "," + VideoConst.PERMISSION_STATE_3);
            }
        } else {
            if (!StringUtil.isNullOrZero(permissionState)) {
                example.setPermissionState(permissionState);
            } else {
                example.setPermissionStateIn(VideoConst.PERMISSION_STATE_2 + "," + VideoConst.PERMISSION_STATE_4);
            }
        }
        List<Author> list = authorModel.getAuthorList(example, pager);
        List<AuthorVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(svideoAuthor -> {
                //设置默认头像
                if (StringUtil.isEmpty(svideoAuthor.getMemberAvatar())) {
                    svideoAuthor.setMemberAvatar(videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue());
                }
                //无昵称,用会员账号
                if (StringUtil.isEmpty(svideoAuthor.getMemberNickname())) {
                    svideoAuthor.setMemberNickname(svideoAuthor.getMemberName());
                }
                vos.add(new AuthorVO(svideoAuthor));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("审核")
    @OperationLogger(option = "审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorId", value = "作者id", required = true),
            @ApiImplicitParam(name = "isPass", value = "审核状态:true-通过，false-拒绝", required = true),
            @ApiImplicitParam(name = "remark", value = "拒绝理由,禁止理由")
    })
    @PostMapping("audit")
    public JsonResult audit(HttpServletRequest request, Integer authorId, boolean isPass, String remark) {
        Author authorDb = authorModel.getAuthorByAuthorId(authorId);
        AssertUtil.isTrue(authorDb.getPermissionState() != VideoConst.PERMISSION_STATE_1, "非法操作");

        Author author = new Author();
        author.setAuthorId(authorId);
        if (isPass) {
            author.setPermissionState(VideoConst.PERMISSION_STATE_2);
        } else {
            AssertUtil.notEmpty(remark, "请填写拒绝理由!");
            author.setPermissionState(VideoConst.PERMISSION_STATE_3);
            author.setRemark(remark);
        }
        author.setUpdateTime(new Date());
        authorModel.updateAuthor(author);
        return SldResponse.success("处理完成");
    }

    @ApiOperation("禁止发布或者解除禁止")
    @OperationLogger(option = "禁止发布或者解除禁止")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorId", value = "作者id", required = true),
            @ApiImplicitParam(name = "isPass", value = "审核状态:true-通过，false-拒绝", required = true),
            @ApiImplicitParam(name = "remark", value = "拒绝理由,禁止理由")
    })
    @PostMapping("isPublish")
    public JsonResult isPublish(HttpServletRequest request, Integer authorId, boolean isPass, String remark) {
        Author author = new Author();
        author.setAuthorId(authorId);
        if (isPass) {
            author.setPermissionState(VideoConst.PERMISSION_STATE_2);
            author.setRemark("");
        } else {
            AssertUtil.notEmpty(remark, "请填写禁止理由!");
            author.setPermissionState(VideoConst.PERMISSION_STATE_4);
            author.setRemark(remark);
        }
        author.setUpdateTime(new Date());
        authorModel.updateAuthor(author);
        return SldResponse.success();
    }
}
