package com.yutou.bilibili.services;

import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.bilibili.Tools.DateFormatUtils;
import com.yutou.bilibili.databases.SystemConfigDatabases;
import com.yutou.bilibili.datas.SystemConfigDatabaseBean;
import com.yutou.common.utils.Log;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

@Service
public class SystemService {
    @Resource
    LiveVideoDownloadService videoService;
    @Resource
    LiveDanmuService danmuService;
    SystemConfigDatabases databases = new SystemConfigDatabases();
    private ScheduledExecutorService timer;
    private ScheduledFuture<?> scheduled;
    BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase();

    public long getLoopTimer() {
        SystemConfigDatabaseBean config = databases.getConfig();
        if (config == null) {
            return databases.getDefaultConfig().getTimerLoop();
        }
        return config.getTimerLoop();
    }

    private final String resetTimer = "00:00:00 - 00:01:00";

    public void start() {
        if (timer == null) {
            timer = Executors.newScheduledThreadPool(1);
        }
        Log.i("执行任务");
        if (scheduled != null) {
            scheduled.cancel(true);
        }
        scheduled = timer.scheduleAtFixedRate(() -> {
            List<LiveConfigDatabaseBean> list = liveConfigDatabase.getAllConfig();
            Log.i("循环任务:" + list.size());
            if (DateFormatUtils.getInstance().checkTime(null, resetTimer)) {
                videoService.clearUserStopList();
                danmuService.clearUserList();
            }
            for (LiveConfigDatabaseBean bean : list) {
                try {
                    if (bean.isRecordDanmu() && bean.checkRecordDanmuTime()) {
                        recordDanmu(bean);
                    } else if (!bean.checkRecordDanmuTime()) {
                        stopRecordDanmu(bean);
                    }
                    if (bean.isRecordLive() && bean.checkRecordLiveTime()) {
                        recordVideo(bean);
                    }
                } catch (Exception e) {
                    Log.e(e);
                }
            }

        }, 0, getLoopTimer(), TimeUnit.MILLISECONDS);
    }

    private void stopRecordDanmu(LiveConfigDatabaseBean bean) {
        if (!videoService.checkDownload(bean.getRoomId())) {
            danmuService.stop(bean.getRoomId(), false);
        }
    }

    public void stop() {
        scheduled.cancel(true);
        videoService.stopAll();
    }

    // 录制弹幕
    private void recordDanmu(LiveConfigDatabaseBean bean) {
        danmuService.start(bean.getRoomId().toString(), false);
    }

    // 录制视频
    private void recordVideo(LiveConfigDatabaseBean bean) {
        videoService.start(bean, false);
    }

    public static void main(String[] args) throws InterruptedException {
        SystemService service = new SystemService();
        Log.i(new Date());
        service.start();
        Thread.sleep(15000);
        Log.i(new Date());
        service.start();
    }


}
