package com.yutou.qqbot.utlis;

import com.yutou.napcat.QQDatabase;
import com.yutou.napcat.model.GroupBean;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQNumberManager;
import com.yutou.qqbot.gpt.BaiduGPTManager;
import com.yutou.qqbot.models.Model;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

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 {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        Model.classList.addAll(AppTools.scanClass("com.yutou.qqbot.models", UseModel.class));
        new Timer().schedule(new TimerTask() {
            private String oldTime = "";

            @Override
            public void run() {
                try {
                    String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
                    if (time.equals(oldTime)) {
                        return;
                    }
                    oldTime = time;
                    for (Class<?> model : Model.classList) {
                        new Thread(() -> {
                            try {
                                List<GroupBean> groups = QQDatabase.getGroups();
                                Model useModel = (Model) model.getDeclaredConstructor().newInstance();
                                for (GroupBean group : groups) {
                                    if (QQNumberManager.getManager().isUseModel(group.getGroupId(), model)) {
                                        useModel.onTime(group.getGroupId(), time);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }).start();
                    }
                   //用switch判断time，case是整点
                    switch (time) {
                        case "00:00":
                        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":
                            BaiduGPTManager.getManager().clear();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, 300);
    }
}
