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.WebUtil;
import com.slodon.b2b2c.model.video.SvideoThemeModel;
import com.slodon.b2b2c.model.video.SvideoThemeVideoModel;
import com.slodon.b2b2c.model.video.SvideoVideoModel;
import com.slodon.b2b2c.video.example.SvideoThemeExample;
import com.slodon.b2b2c.video.example.SvideoThemeVideoExample;
import com.slodon.b2b2c.video.pojo.SvideoTheme;
import com.slodon.b2b2c.video.pojo.SvideoThemeVideo;
import com.slodon.b2b2c.video.pojo.SvideoVideo;
import com.slodon.b2b2c.vo.video.RecommendThemeVO;
import com.slodon.b2b2c.vo.video.SelectVideoVO;
import com.slodon.b2b2c.vo.video.ThemeDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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;

import static com.slodon.b2b2c.vo.video.VideoVO.dealImages;

@Api(tags = "admin-推荐主题")
@RestController
@RequestMapping("v3/video/admin/video/theme")
public class AdminThemeController extends BaseController {

    @Resource
    private SvideoThemeModel svideoThemeModel;
    @Resource
    private SvideoThemeVideoModel svideoThemeVideoModel;
    @Resource
    private SvideoVideoModel svideoVideoModel;

    @ApiOperation("推荐主题列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeName", value = "主题名称", paramType = "query"),
            @ApiImplicitParam(name = "labelId", value = "标签id", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<RecommendThemeVO>> list(HttpServletRequest request, String themeName, Integer labelId) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoThemeExample example = new SvideoThemeExample();
        example.setThemeNameLike(themeName);
        example.setLabelId(labelId);
        List<SvideoTheme> list = svideoThemeModel.getSvideoThemeList(example, pager);
        List<RecommendThemeVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(theme -> {
                vos.add(new RecommendThemeVO(theme));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("推荐主题详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeId", value = "主题id", required = true, paramType = "query")
    })
    @GetMapping("detail")
    public JsonResult<ThemeDetailVO> detail(HttpServletRequest request, Integer themeId) {
        SvideoTheme svideoTheme = svideoThemeModel.getSvideoThemeByThemeId(themeId);
        AssertUtil.notNull(svideoTheme, "获取主题信息为空，请重试");

        ThemeDetailVO detailVO = new ThemeDetailVO(svideoTheme);

        SvideoThemeVideoExample example = new SvideoThemeVideoExample();
        example.setThemeId(themeId);
        example.setOrderBy("theme_video_id asc");
        List<SvideoThemeVideo> list = svideoThemeVideoModel.getSvideoThemeVideoList(example, null);
        List<SelectVideoVO> videoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(themeVideo -> {
                SvideoVideo svideoVideo = svideoVideoModel.getSvideoVideoByVideoId(themeVideo.getVideoId());
                AssertUtil.notNull(svideoVideo, "视频不存在");
                SelectVideoVO vo = new SelectVideoVO(themeVideo);
                vo.setVideoContent(svideoVideo.getVideoContent());
                if (svideoVideo.getVideoType() == VideoConst.SVIDEO_TYPE_2) {
                    vo.setImageList(dealImages(svideoVideo.getVideoImage()));
                }
                videoList.add(vo);
            });
        }
        detailVO.setVideoList(videoList);
        return SldResponse.success(detailVO);
    }

    @ApiOperation("新增推荐主题")
    @OperationLogger(option = "新增推荐主题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeName", value = "主题名称", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true),
            @ApiImplicitParam(name = "isShow", value = "是否显示0:不显示，1显示", required = true),
            @ApiImplicitParam(name = "image", value = "主题图片", required = true),
            @ApiImplicitParam(name = "carouselImage", value = "封面图，用于轮播", required = true),
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true),
            @ApiImplicitParam(name = "videoIds", value = "绑定视频id(用逗号隔开)", required = true)
    })
    @PostMapping("add")
    public JsonResult add(HttpServletRequest request, String themeName, Integer sort, Integer isShow,
                          String image, String carouselImage, Integer labelId, String videoIds) {
        SvideoTheme theme = new SvideoTheme();
        theme.setThemeName(themeName);
        theme.setSort(sort);
        theme.setIsShow(isShow);
        theme.setImage(image);
        theme.setCarouselImage(carouselImage);
        theme.setLabelId(labelId);
        theme.setVideoNum(videoIds.split(",").length);
        theme.setCreateTime(new Date());
        svideoThemeModel.saveSvideoTheme(theme, videoIds);
        return SldResponse.success("新增成功");
    }

    @ApiOperation("修改推荐主题")
    @OperationLogger(option = "修改推荐主题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeId", value = "主题id", required = true),
            @ApiImplicitParam(name = "themeName", value = "主题名称", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true),
            @ApiImplicitParam(name = "isShow", value = "是否显示0:不显示，1显示", required = true),
            @ApiImplicitParam(name = "image", value = "主题图片", required = true),
            @ApiImplicitParam(name = "carouselImage", value = "封面图，用于轮播", required = true),
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true),
            @ApiImplicitParam(name = "videoIds", value = "绑定视频id(用逗号隔开)", required = true)
    })
    @PostMapping("update")
    public JsonResult update(HttpServletRequest request, Integer themeId, String themeName, Integer sort, Integer isShow,
                             String image, String carouselImage, Integer labelId, String videoIds) {
        SvideoTheme theme = new SvideoTheme();
        theme.setThemeId(themeId);
        theme.setThemeName(themeName);
        theme.setSort(sort);
        theme.setIsShow(isShow);
        theme.setImage(image);
        theme.setCarouselImage(carouselImage);
        theme.setLabelId(labelId);
        theme.setVideoNum(videoIds.split(",").length);
        theme.setUpdateTime(new Date());
        svideoThemeModel.updateSvideoTheme(theme, videoIds);
        return SldResponse.success("修改成功");
    }

    @ApiOperation("是否显示推荐主题")
    @OperationLogger(option = "是否显示推荐主题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeId", value = "主题id", required = true),
            @ApiImplicitParam(name = "isShow", value = "是否显示0:不显示，1显示", required = true)
    })
    @PostMapping("isShow")
    public JsonResult isShow(HttpServletRequest request, Integer themeId, Integer isShow) {
        SvideoTheme theme = new SvideoTheme();
        theme.setThemeId(themeId);
        theme.setIsShow(isShow);
        theme.setUpdateTime(new Date());
        svideoThemeModel.updateSvideoTheme(theme, null);
        return SldResponse.success("修改成功");
    }

    @ApiOperation("删除推荐主题")
    @OperationLogger(option = "删除推荐主题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeId", value = "主题id", required = true)
    })
    @PostMapping("del")
    public JsonResult del(HttpServletRequest request, Integer themeId) {
        svideoThemeModel.deleteSvideoTheme(themeId);
        return SldResponse.success("删除成功");
    }

    @ApiOperation("查看视频列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeId", value = "主题id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "videoName", value = "视频名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("videoList")
    public JsonResult<PageVO<SelectVideoVO>> videoList(HttpServletRequest request, Integer themeId, String videoName) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoThemeVideoExample example = new SvideoThemeVideoExample();
        example.setThemeId(themeId);
        example.setVideoNameLike(videoName);
        List<SvideoThemeVideo> list = svideoThemeVideoModel.getSvideoThemeVideoList(example, pager);
        List<SelectVideoVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(themeVideo -> {
                SvideoVideo svideoVideo = svideoVideoModel.getSvideoVideoByVideoId(themeVideo.getVideoId());
                AssertUtil.notNull(svideoVideo, "视频不存在");
                SelectVideoVO vo = new SelectVideoVO(themeVideo);
                vo.setVideoContent(svideoVideo.getVideoContent());
                if (svideoVideo.getVideoType() == VideoConst.SVIDEO_TYPE_2) {
                    vo.setImageList(dealImages(svideoVideo.getVideoImage()));
                }
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("删除主题视频")
    @OperationLogger(option = "删除主题视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeVideoId", value = "主题视频id", required = true)
    })
    @PostMapping("delVideo")
    public JsonResult delVideo(HttpServletRequest request, Integer themeVideoId) {
        svideoThemeVideoModel.deleteSvideoThemeVideo(themeVideoId);
        return SldResponse.success("删除成功");
    }
}
