package cn.chnmuseum.party.web.controller;

import cn.chnmuseum.party.common.enums.*;
import cn.chnmuseum.party.common.log.MethodLog;
import cn.chnmuseum.party.common.log.OperModule;
import cn.chnmuseum.party.common.log.OperType;
import cn.chnmuseum.party.common.validator.groups.Add;
import cn.chnmuseum.party.common.validator.groups.Update;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.*;
import cn.chnmuseum.party.service.*;
import cn.chnmuseum.party.web.controller.base.BaseController;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <pre>
 * 视频内容 前端控制器
 * </pre>
 *
 * @author Danny Lee
 * @since 2021-03-16
 */
@Slf4j
@RestController
@RequestMapping("/videoContent")
@Api(tags = {"视频内容接口"})
public class VideoContentController extends BaseController {

    @Resource
    private ExhibitionBoardService exhibitionBoardService;
    @Resource
    private VideoContentService videoContentService;
    @Resource
    private CopyrightOwnerService copyrightOwnerService;
    @Resource
    private VideoContentCatService videoContentCatService;
    @Resource
    private AssetService assetService;
    @Resource
    private AuditService auditService;

    @PostMapping(value = "/save")
    @RequiresAuthentication  //@RequiresPermissions("video:content:save")
    @ApiOperation(value = "添加视频内容", notes = "添加视频内容")
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.ADD)
    public Map<String, Object> saveVideoContent(@Validated(value = {Add.class}) VideoContent videoContent) {
        final LambdaQueryWrapper<VideoContent> lambdaQueryWrapper = Wrappers.<VideoContent>lambdaQuery().eq(VideoContent::getName, videoContent.getName().trim());
        final int count = this.videoContentService.count(lambdaQueryWrapper);
        if (count > 0) {
            return getFailResult("400", "名称已存在,请修改名称");
        }
        final List<String> videoFileIdList = videoContent.getVideoFileIdList();
        if (videoFileIdList == null || videoFileIdList.isEmpty()) {
            return getFailResult("400", "视频文件必须上传");
        }
        final List<Asset> assetList = this.assetService.listByIds(videoFileIdList);
        final List<String> languageList = assetList.stream().map(Asset::getLanguage).collect(Collectors.toList());
        if (!languageList.contains(LanguageEnum.ZH.name())) {
            return getFailResult("视频文件必须包含汉语");
        }
        if (StringUtils.isBlank(videoContent.getVideoContentCatId())) {
            return getFailResult("视频内容必须选择");
        }

        videoContent.setAuditStatus(AuditStatusEnum.TBC.name());
        videoContent.setPublished(false);
        videoContent.setDeleted(false);
        // 保存业务节点信息
        boolean result = videoContentService.save(videoContent);
        // 返回操作结果
        if (result) {
            for (String videoFileId : videoFileIdList) {
                final Asset asset = this.assetService.getById(videoFileId);
                asset.setThumbnail(videoContent.getThumbnail());
                asset.setFileType(FileTypeEnum.VIDEO.name());
                asset.setFileCat(FileCatEnum.VIDEO_CONTENT.name());
                asset.setRefItemId(videoContent.getId());
                this.assetService.updateById(asset);

                if (StringUtils.isBlank(videoContent.getName())) {
                    videoContent.setName(asset.getVideoContentName());
                    this.videoContentService.updateById(videoContent);
                }
            }

            final Audit audit = Audit.builder()
                    .content(videoContent.getName())
                    .name(videoContent.getName())
                    .userId(getcurUser().getId())
                    .organId(getcurUser().getOrgId())
                    .refItemId(videoContent.getId())
                    .type(AuditTypeEnum.VIDEO_CONTENT.name())
                    .operation(AuditOperationEnum.ADD.name())
                    .status(AuditStatusEnum.TBC.name())
                    .level(AuditStatusEnum.TBC.name())
                    .build();
            this.auditService.save(audit);
            return getSuccessResult();
        }
        return getFailResult();
    }

    @PutMapping("/update")
    @RequiresAuthentication  //@RequiresPermissions("video:content:update")
    @ApiOperation(value = "修改视频内容信息", notes = "修改视频内容信息")
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.UPDATE)
    public Map<String, Object> updateVideoContent(@Validated(value = {Update.class}) VideoContent videoContent) {
        final LambdaQueryWrapper<VideoContent> lambdaQueryWrapper = Wrappers.<VideoContent>lambdaQuery().eq(VideoContent::getName, videoContent.getName().trim());
        lambdaQueryWrapper.ne(VideoContent::getId, videoContent.getId());
        final int count = this.videoContentService.count(lambdaQueryWrapper);
        if (count > 0) {
            return getFailResult("400", "名称已存在,请修改名称");
        }
        final List<String> videoFileIdList = videoContent.getVideoFileIdList();
        if (videoFileIdList == null || videoFileIdList.isEmpty()) {
            return getFailResult("400", "视频文件必须上传");
        }
        final List<Asset> assetList = this.assetService.listByIds(videoFileIdList);
        final List<String> languageList = assetList.stream().map(Asset::getLanguage).collect(Collectors.toList());
        if (!languageList.contains(LanguageEnum.ZH.name())) {
            return getFailResult("视频文件必须包含汉语");
        }
        //如果不上传文件id,就代表前端删除这个视频文件
        LambdaQueryWrapper<Asset> assetWrapper = new QueryWrapper<Asset>().lambda()
                .eq(Asset::getRefItemId, videoContent.getId())
                .notIn(Asset::getId, videoFileIdList);
        boolean remove = assetService.remove(assetWrapper);

        for (String videoFileId : videoFileIdList) {
            final Asset asset = this.assetService.getById(videoFileId);
            if (!asset.getPublished()) {
                asset.setThumbnail(videoContent.getThumbnail());
                asset.setFileType(FileTypeEnum.VIDEO.name());
                asset.setFileCat(FileCatEnum.VIDEO_CONTENT.name());
                asset.setRefItemId(videoContent.getId());
                this.assetService.updateById(asset);
            }
        }

        final Audit audit = Audit.builder()
                .content(videoContent.getName())
                .name(videoContent.getName())
                .userId(getcurUser().getId())
                .organId(getcurUser().getOrgId())
                .refItemId(videoContent.getId())
                .type(AuditTypeEnum.VIDEO_CONTENT.name())
                .operation(AuditOperationEnum.EDIT.name())
                .status(AuditStatusEnum.TBC.name())
                .level(AuditStatusEnum.TBC.name())
                .modelData(JSONObject.toJSONString(videoContent))
                .build();
        this.auditService.save(audit);
        //修改自身审核状态信息为待初审
        videoContent.setAuditStatus(AuditStatusEnum.TBC.name());
        videoContentService.updateById(videoContent);
        return getSuccessResult();
    }

    @GetMapping("/getExhibitionBoardById/{id}")
    @RequiresAuthentication  //@RequiresPermissions("video:content:delete")
    @ApiOperation(value = "根据视频内容ID查询被引用的展板", notes = "根据视频内容ID查询被引用的展板")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "标识ID", paramType = "path", dataType = "String", required = true)
    })
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.SELECT)
    public Map<String, Object> getExhibitionBoardById(@PathVariable("id") String id) {
        final List<ExhibitionBoard> exhibitionBoardList = this.exhibitionBoardService.list(Wrappers.<ExhibitionBoard>lambdaQuery().eq(ExhibitionBoard::getVideoContentId, id));
        if (!exhibitionBoardList.isEmpty()) {
            final String collect = exhibitionBoardList.stream().map(ExhibitionBoard::getName).collect(Collectors.joining("、"));
            return getResult(collect);
        }
        return getSuccessResult();
    }

    @GetMapping("/getList")
    @RequiresAuthentication  //@RequiresPermissions("video:content:list")
    @ApiOperation(value = "获取视频内容全部列表(无分页)", notes = "获取视频内容全部列表(无分页)")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "auditStatus", value = "审核状态", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "videoContentCatId", value = "视频内容分类ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "videoContentCopyrightOwnerId", value = "视频内容版权方ID", paramType = "query", dataType = "String")
    })
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.SELECT)
    public Map<String, Object> getVideoContentList(@RequestParam(value = "auditStatus", required = false) AuditStatusEnum auditStatus,
                                                   @RequestParam(value = "videoContentCatId", required = false) String videoContentCatId,
                                                   @RequestParam(value = "videoContentCopyrightOwnerId", required = false) String videoContentCopyrightOwnerId) {
        final LambdaQueryWrapper<VideoContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VideoContent::getPublished, true);
        if (auditStatus != null) {
            lambdaQueryWrapper.eq(VideoContent::getAuditStatus, auditStatus.name());
        }
        if (StringUtils.isNotBlank(videoContentCatId)) {
            lambdaQueryWrapper.eq(VideoContent::getVideoContentCatId, videoContentCatId);
        }
        if (StringUtils.isNotBlank(videoContentCopyrightOwnerId)) {
            lambdaQueryWrapper.eq(VideoContent::getVideoContentCopyrightOwnerId, videoContentCopyrightOwnerId);
        }
        lambdaQueryWrapper.orderByDesc(VideoContent::getCreateTime);
        List<VideoContent> videoContentList = videoContentService.list(lambdaQueryWrapper);
        return getResult(videoContentList);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "videoContentCatId", value = "视频内容分类ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "videoContentCopyrightOwnerId", value = "视频内容版权方ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
    @PostMapping("/getPageList")
    @RequiresAuthentication  //@RequiresPermissions("video:content:page")
    @ApiOperation(value = "获取视频内容分页列表", notes = "获取视频内容分页列表")
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.SELECT)
    public Map<String, Object> getAssetPageList(GenericPageParam genericPageParam) {
        LambdaQueryWrapper<VideoContent> queryWrapper = new LambdaQueryWrapper<>();
        // 对名称或编码模糊查询
        if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
            queryWrapper.like(VideoContent::getName, genericPageParam.getNameOrCode());
        }
        if (StringUtils.isNotBlank(genericPageParam.getVideoContentCatId())) {
            queryWrapper.eq(VideoContent::getVideoContentCatId, genericPageParam.getVideoContentCatId());
        }
        if (StringUtils.isNotBlank(genericPageParam.getVideoContentCopyrightOwnerId())) {
            queryWrapper.eq(VideoContent::getVideoContentCopyrightOwnerId, genericPageParam.getVideoContentCopyrightOwnerId());
        }
        // 根据创建时间区间检索
        if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
            queryWrapper.ge(VideoContent::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
                    .le(VideoContent::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
        }
        queryWrapper.eq(VideoContent::getDeleted, false);
        // 设置排序规则
        queryWrapper.orderByDesc(VideoContent::getCreateTime);
        // 设置查询内容
        queryWrapper.select(
                VideoContent::getId,
                VideoContent::getName,
                VideoContent::getAuditStatus,
                VideoContent::getPublished,
                VideoContent::getDeleted,
                VideoContent::getVideoContentCatId,
                VideoContent::getVideoContentCopyrightOwnerId,
                VideoContent::getCreateTime,
                VideoContent::getUpdateTime);
        Page<VideoContent> page = this.videoContentService.page(getPage(), queryWrapper);
        for (VideoContent videoContent : page.getRecords()) {
            if (videoContent.getVideoContentCatId() != null) {
                VideoContentCat videoContentCat = this.videoContentCatService.getById(videoContent.getVideoContentCatId());
                videoContent.setVideoContentCatName(videoContentCat.getName());
            }
            if (videoContent.getVideoContentCopyrightOwnerId() != null) {
                CopyrightOwner copyrightOwner = this.copyrightOwnerService.getById(videoContent.getVideoContentCopyrightOwnerId());
                if (copyrightOwner == null) {
                    videoContent.setVideoContentCopyrightOwnerName("对应的版权方已被删除");
                } else {
                    videoContent.setVideoContentCopyrightOwnerName(copyrightOwner.getName());
                }
            }
        }
        return getResult(page);
    }

    @ApiOperation(value = "获取视频内容详情", notes = "获取视频内容详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标识ID", dataType = "String", paramType = "path", required = true)
    })
    @GetMapping("/get/{id}")
    @RequiresAuthentication  //@RequiresPermissions("video:content:get:id")
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.SELECT)
    public Map<String, Object> getById(@PathVariable("id") String id) {
        VideoContent videoContent = videoContentService.getById(id);
        //修改服务器报500错误
        if (videoContent==null){
            return getSuccessResult();
        }
        if (videoContent.getVideoContentCatId() != null) {
            VideoContentCat videoContentCat = this.videoContentCatService.getById(videoContent.getVideoContentCatId());
            if (videoContentCat != null) {
                videoContent.setVideoContentCatName(videoContentCat.getName());
            }
        }
        if (videoContent.getVideoContentCopyrightOwnerId() != null) {
            CopyrightOwner copyrightOwner = this.copyrightOwnerService.getById(videoContent.getVideoContentCopyrightOwnerId());
            if (copyrightOwner != null) {
                videoContent.setVideoContentCopyrightOwnerName(copyrightOwner.getName());
            }
        }
        final LambdaQueryWrapper<Asset> assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, id);
//        assetQueryWrapper.eq(Asset::getPublished, true);
        assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.VIDEO_CONTENT.name());
        final List<Asset> videoFileList = this.assetService.list(assetQueryWrapper);
        videoContent.setVideoFileList(videoFileList);
        videoContent.setVideoFileIdList(videoFileList.stream().map(Asset::getId).collect(Collectors.toList()));

        final LambdaQueryWrapper<Audit> auditQueryWrapper = Wrappers.<Audit>lambdaQuery().eq(Audit::getRefItemId, id);
        final List<Audit> auditList = this.auditService.list(auditQueryWrapper);
        videoContent.setAuditHistoryList(auditList);

        return getResult(videoContent);
    }

    @ApiOperation(value = "获取视频内容详情(审核详情使用)", notes = "获取视频内容详情(审核详情使用)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditId", value = "审核ID", dataType = "String", paramType = "path", required = true)
    })
    @GetMapping("/getAudit/{auditId}")
    @RequiresAuthentication  //@RequiresPermissions("video:content:get:id")
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.SELECT)
    public Map<String, Object> getAuditInfoById(@PathVariable("auditId") String auditId) {
        final VideoContent videoContent = JSONObject.parseObject(this.auditService.getById(auditId).getModelData(), VideoContent.class);
        final String id = videoContent.getId();
        if (videoContent.getVideoContentCatId() != null) {
            VideoContentCat videoContentCat = this.videoContentCatService.getById(videoContent.getVideoContentCatId());
            if (videoContentCat != null) {
                videoContent.setVideoContentCatName(videoContentCat.getName());
            }
        }
        if (videoContent.getVideoContentCopyrightOwnerId() != null) {
            CopyrightOwner copyrightOwner = this.copyrightOwnerService.getById(videoContent.getVideoContentCopyrightOwnerId());
            if (copyrightOwner != null) {
                videoContent.setVideoContentCopyrightOwnerName(copyrightOwner.getName());
            }
        }

        final List<String> videoFileIdList = videoContent.getVideoFileIdList();
        final List<Asset> videoFileList = this.assetService.listByIds(videoFileIdList);
        videoContent.setVideoFileIdList(videoFileIdList);
        videoContent.setVideoFileList(videoFileList);

        final LambdaQueryWrapper<Audit> auditQueryWrapper = Wrappers.<Audit>lambdaQuery().eq(Audit::getRefItemId, id);
        final List<Audit> auditList = this.auditService.list(auditQueryWrapper);
        videoContent.setAuditHistoryList(auditList);

        return getResult(videoContent);
    }

    @DeleteMapping("/delete/{id}")
    @RequiresAuthentication  //@RequiresPermissions("video:content:delete")
    @ApiOperation(value = "根据ID删除视频内容", notes = "根据ID删除视频内容")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "标识ID", paramType = "path", dataType = "String", required = true)
    })
    @MethodLog(operModule = OperModule.VIDEOCONTENT, operType = OperType.DELETE)
    public Map<String, Object> deleteVideoContent(@PathVariable("id") String id) {
        // 视频关联展板时视频不能被删除
        final List<ExhibitionBoard> exhibitionBoardList = this.exhibitionBoardService.list(Wrappers.<ExhibitionBoard>lambdaQuery().eq(ExhibitionBoard::getVideoContentId, id));
        if (CollectionUtil.isNotEmpty(exhibitionBoardList)) {
            return getFailResult("该视频有对应的展板信息,无法删除!");
        }
        final VideoContent videoContent = this.videoContentService.getById(id);
        final Audit audit = Audit.builder()
                .content(videoContent.getName())
                .name(videoContent.getName())
                .refItemId(id)
                .userId(getcurUser().getId())
                .type(AuditTypeEnum.VIDEO_CONTENT.name())
                .operation(AuditOperationEnum.REMOVE.name())
                .status(AuditStatusEnum.TBC.name())
                .level(AuditStatusEnum.TBC.name())
                .build();
        final boolean result = this.auditService.save(audit);
        //修改自己的状态为 待初审
        LambdaUpdateWrapper<VideoContent> set = new UpdateWrapper<VideoContent>().lambda()
                .eq(VideoContent::getId, videoContent.getId())
                .set(VideoContent::getUpdateTime, LocalDateTime.now())
                .set(VideoContent::getAuditStatus, AuditStatusEnum.TBC.name());
        boolean update = videoContentService.update(set);

        if (result) {
            return getSuccessResult();
        }
        return getFailResult();
    }

}