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.QQBotManager;
import com.yutou.qqbot.QQNumberManager;
import com.yutou.qqbot.models.Model;
import lombok.val;
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;
                    }
                    BaiduGPTManager.getManager().clear();
                    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);
                                    }
                                }

                              /*  Bot bot = QQBotManager.getInstance().getBot();
                                if (bot == null) {
                                    return;
                                }
                                Model useModel = (Model) model.getDeclaredConstructor().newInstance();
                                for (Group group : bot.getGroups()) {
                                    if (QQNumberManager.getManager().isUseModel(group.getId(), model)) {
                                        useModel.onTime(group.getId(), time);
                                    }
                                }*/
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }).start();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, 300);
    }
}
