package com.yutou.bilibili.Controllers;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.bilibili.datas.ResultData;
import com.yutou.bilibili.datas.ReturnCode;
import com.yutou.bilibili.services.LiveVideoDownloadService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LiveVideoController {
    @Resource
    LiveVideoDownloadService videoService;

    @RequestMapping("/live/video/list")
    @ResponseBody
    public JSONObject getLiveVideoList() {
        return ResultData.success(videoService.getDownloadTasks());
    }

    @RequestMapping("/live/video/stop")
    @ResponseBody
    public JSONObject stopDownload(String roomId) {
        videoService.stop(roomId, true);
        return ResultData.success(true);
    }

    @RequestMapping("/live/video/start")
    @ResponseBody
    public JSONObject startDownload(String roomId) {
        BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase();
        List<LiveConfigDatabaseBean> list = liveConfigDatabase.getAllConfig();
        liveConfigDatabase.close();
        for (LiveConfigDatabaseBean bean : list) {
            if (bean.getRoomId().toString().equals(roomId)) {
                videoService.start(bean, true);
                return ResultData.success(true);
            }
        }
        return ResultData.fail(ReturnCode.RC999);
    }
}
