package cn.reghao.tnb.content.app.vod.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.common.db.PageScroll;
import cn.reghao.tnb.content.app.aop.TimeConsumed;
import cn.reghao.tnb.content.api.dto.VideoCard;
import cn.reghao.tnb.content.app.vod.model.vo.BannerVideoVO;
import cn.reghao.tnb.content.app.vod.model.vo.VideoDetail;
import cn.reghao.tnb.content.app.vod.service.RecommendService;
import cn.reghao.tnb.content.app.vod.service.VideoPostQuery;
import cn.reghao.tnb.content.app.vod.service.VideoQueryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author reghao
 * @date 2023-01-30 13:51:11
 */
@Slf4j
@Tag(name = "视频查询接口")
@RestController
@RequestMapping("/api/content/video")
public class VideoQueryController {
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private AccountQuery accountQuery;
    private final VideoPostQuery videoPostQuery;
    private final RecommendService recommendService;
    private final VideoQueryService videoQueryService;

    public VideoQueryController(VideoPostQuery videoPostQuery, RecommendService recommendService,
                                VideoQueryService videoQueryService) {
        this.videoPostQuery = videoPostQuery;
        this.recommendService = recommendService;
        this.videoQueryService = videoQueryService;
    }

    @TimeConsumed
    @Operation(summary = "获取分区视频", description = "N")
    @GetMapping(value = "/category", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCategoryVideos(@RequestParam("categoryId") int categoryId, @RequestParam("page") int page) {
        PageList<VideoCard> pageList = videoPostQuery.getCategoryVideos(categoryId, page);
        return WebResult.success(pageList);
    }

    @TimeConsumed
    @Operation(summary = "获取分区短视频", description = "N")
    @GetMapping(value = "/short", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCategoryShortVideos() {
        VideoDetail videoDetail = videoPostQuery.getShortVideo();
        return WebResult.success(videoDetail);
    }

    @Operation(summary = "获取某个用户发布的视频", description = "N")
    @GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUserVideos(@RequestParam("userId") String userId, @RequestParam("page") int page) {
        long userId1 = accountQuery.getUserIdLong(userId);
        PageList<VideoCard> pageList = videoPostQuery.getUserVideos(userId1, page);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取某个标签下的视频", description = "N")
    @GetMapping(value = "/tag", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getTagVideos(@RequestParam("tag") String tag, @RequestParam("page") int page) {
        PageList<VideoCard> pageList = videoPostQuery.getTagVideos(tag, page);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取视频详情", description = "N")
    @GetMapping(value = "/detail/{videoId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoPost(@PathVariable("videoId") String videoId) {
        String jsonResult = videoPostQuery.getVideoPostData(videoId);
        return jsonResult;
    }

    @Operation(summary = "获取和某个视频类似的视频", description = "N")
    @GetMapping(value = "/similar", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSimilarVideos(@RequestParam("videoId") String videoId) {
        String jsonResult = videoQueryService.getSimilarVideos(videoId);
        return jsonResult;
    }

    @Operation(summary = "获取热门视频", description = "N")
    @GetMapping(value = "/hot", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getHotList() {
        List<BannerVideoVO> list = videoQueryService.getBannerVideos();
        return WebResult.success(list);
    }

    @TimeConsumed
    @AuthUser
    @Operation(summary = "获取用户的视频时间线", description = "N")
    @GetMapping(value = "/timeline", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoTimeline(@RequestParam("nextId") String nextId) {
        long userId = UserContext.getUserId();
        PageScroll<VideoCard> pageScroll = videoQueryService.getVideoTimeline(userId, nextId);
        return WebResult.success(pageScroll);
    }

    @Operation(summary = "获取推荐视频", description = "N")
    @GetMapping(value = "/recommend", produces = MediaType.APPLICATION_JSON_VALUE)
    @Deprecated
    public String getRecommendVideos(@RequestParam(value = "nextId", required = false) String nextId) {
        if (Integer.parseInt(nextId) > 100) {
            return WebResult.success(Collections.emptyList());
        }

        long userId = UserContext.getUserId();
        List<VideoCard> list = recommendService.getRecommendVideos(userId, nextId);
        return WebResult.success(list);
    }
}
