package com.yutou.bilibili.Tools;

import com.yutou.bilibili.BiliBili.Datas.AppData;
import com.yutou.bilibili.BiliBili.Datas.LiveData;
import com.yutou.bilibili.BiliBili.Live;
import com.yutou.bilibili.BiliBili.LiveUtils;
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
import com.yutou.bilibili.Services.ISystemConfigService;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 服务启动后执行
 */
@Component
public class ApplicationInit implements ApplicationRunner {
    @Resource
    IBiliBiliLiveService service;
    @Resource
    ISystemConfigService configService;
    private Timer timer;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        LiveUtils.LiveGiftConfig.getInstance();
        AppData.FFMPEG=configService.getConfig(Config.SYSTEM_VIDEO_FFMPEG);
        AppData.LIVE_SAVE_FFMPEG= configService.getConfig(Config.SYSTEM_VIDEO_SAVE_MODEL) != null && configService.getConfig(Config.SYSTEM_VIDEO_SAVE_MODEL).equals("ffmpeg");
        startTimer();
    }

    private void startTimer() {
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                String oldTime = "";

                @Override
                public void run() {
                    Date date = new Date();
                    String time = new SimpleDateFormat("HH:mm").format(date);
                    if (time.equals(oldTime)) {
                        return;
                    }
                    checkLive();
                    oldTime = time;
                    switch (time) {
                        case "00:00":
                            for (Live live : Live.lives.values()) {
                                live.clearInfo();
                            }
                            break;
                        case "01:00":
                        case "02:00":
                        case "03:00":
                        case "04:00":
                        case "05:00":
                        case "06:00":
                        case "07:00":
                        case "08:00":
                        case "09:00":
                        case "10:00":
                        case "11:00":
                        case "12:00":
                        case "13:00":
                        case "14:00":
                        case "15:00":
                        case "16:00":
                        case "17:00":
                        case "18:00":
                        case "19:00":
                        case "20:00":
                        case "21:00":
                        case "22:00":
                        case "23:00":
                        case "23:59":
                            saveData(time);
                            break;
                    }
                }

                private void checkLive() {
                    for (BilibiliUpInfo info : service.getUpInfo()) {
                        LiveUtils.reloadLiveState(info.getRoomid());
                        if (info.getOfflinelistening() == 1) {
                            if (info.getEnable() == 1) {
                                Live live = LiveUtils.liveContains(info);
                                if (live == null) {
                                    live = new Live();
                                    live.add(info.getRoomid(), !StringUtils.isEmpty(AppTools.readFile(new File("cookies.json"))));
                                }
                            }
                        }
                    }
                }
            }, 0, 2*60 * 1000);
        }
    }

    public void saveData(String time) {
        Date date = new Date();
        String toDay = AppTools.getToDayTime() + " 00:00:00";
        String nowTime = AppTools.getToDayTime() + " " + time + ":00";
        for (BilibiliUpInfo info : service.getUpInfo()) {
            Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(toDay, new ParsePosition(0));
            Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(nowTime, new ParsePosition(0));
            int userCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.INTERACT_WORD}).size();
            int vipCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.ENTRY_EFFECT}).size();
            int giftCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{
                    LiveData.SEND_GIFT,
                    LiveData.COMBO_SEND,
                    LiveData.GUARD_BUY,
                    LiveData.SUPER_CHAT_MESSAGE}).size();
            Live live = LiveUtils.liveContains(info);
            int popularCount = 0;
            if (live != null) {
                popularCount = live.getInfo().getPopular();
            }
            BilibiliLiveInfo liveInfo = new BilibiliLiveInfo();
            liveInfo.setRoomid(info.getRoomid());
            liveInfo.setGiftuser(giftCount);
            liveInfo.setUserindex(userCount);
            liveInfo.setVipuserindex(vipCount);
            liveInfo.setPopular(popularCount);
            liveInfo.setSubtime(date);
            service.addLiveInfo(liveInfo);

        }
    }
}
