package com.pqs9900.listener;

import com.njcn.mapper.configuration.SysMapper;
import com.njcn.pojo.data.TimerManage;
import com.pqs9900.task.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;

import java.util.ArrayList;
import java.util.List;


public class SchedulerListener implements ApplicationListener<ApplicationEvent> {
    @Autowired
    private MySchudler mySchudler;
    @Autowired
    private SysMapper sysMapper;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {

        if(event instanceof ContextClosedEvent){
            //spring容器关闭时调用
            if(mySchudler!=null){
                mySchudler.destry();
            }
        }else if(event instanceof ContextRefreshedEvent ){
            //spring容器启用时调用
            if (((ContextRefreshedEvent) event).getApplicationContext().getParent() == null) {
                MyJobDetail userJob = new MyJobDetail("casual", "userJob", CasualUserTask.class);
                MyTrigger userTrigger = new MyTrigger("casualTrigger", "userTriggerGroup", "0 0/5 * * * ?");

                MyJobDetail lockUserJob = new MyJobDetail("lockUser", "userJob", LockUserState.class);
                MyTrigger lockUserTrigger = new MyTrigger("lockUserTrigger", "userTriggerGroup", "0 0/1 * * * ?");

                MyJobDetail translateJob = new MyJobDetail("translate", "translateJob", JudgeTranslate.class);
                MyTrigger translateTrigger = new MyTrigger("translateTrigger", "translateTriggerGroup", "0 0/5 * * * ?");

                MyJobDetail topTerminalJob = new MyJobDetail("topTerminal", "topTerminalJob", TopExceptionTask.class);
                MyTrigger topTerminalTrigger = new MyTrigger("topTerminalTrigger", "topTerminalTriggerGroup", "0 0 2 * * ? *");

                List<TimerManage> list;
                try { //防止数据库没有建表，则不开启
                    list = sysMapper.getTimerManage();
                } catch (Exception e) {
                    list = new ArrayList<>();
                }

                for (TimerManage timerManage : list) {
                    switch (timerManage.getGroupName()) {
                        case "userTriggerGroup": {
                            mySchudler.startJob(userJob, userTrigger, timerManage.getState());
                            mySchudler.startJob(lockUserJob, lockUserTrigger, timerManage.getState());
                            break;
                        }
                        case "translateTriggerGroup": {
                            mySchudler.startJob(translateJob, translateTrigger, timerManage.getState());
                            break;
                        }
                        case "topTerminalTriggerGroup": {
                            mySchudler.startJob(topTerminalJob, topTerminalTrigger, timerManage.getState());
                            break;
                        }
                    }
                }
            }
        }
    }
}
