package cn.reghao.bnt.web.config.spring;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import cn.reghao.bnt.web.admin.service.LogService;
import cn.reghao.bnt.web.config.AppProperties;
import cn.reghao.bnt.web.config.log.MyLogbackAppender;
import cn.reghao.bnt.web.devops.machine.service.MachineService;
import cn.reghao.bnt.web.devops.util.BuilderInit;
import cn.reghao.jutil.jdk.machine.id.MachineId;
import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author reghao
 * @date 2019-09-26 17:22:16
 */
@Slf4j
@Order(2)
@Component
public class SpringLifecycle implements ApplicationRunner, DisposableBean {
    private final MachineService machineService;
    private final LogService logService;
    private final Environment environment;
    private final BuilderInit builderInit;

    public SpringLifecycle(MachineService machineService, AppProperties appProperties,
                           LogService logService, Environment environment, BuilderInit builderInit) {
        this.machineService = machineService;
        this.logService = logService;
        this.environment = environment;
        this.builderInit = builderInit;
    }

    @Override
    public void run(ApplicationArguments args) throws IOException {
        initSys();
        log.info("bnt started");
    }

    @Override
    public void destroy() {
        machineService.setMachineOffline();
        log.info("bnt shutdown...");
    }

    private void initSys() {
        builderInit.init();
    }

    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        log.info("Application is ready! You can start processing requests.");
        // 在这里执行初始化逻辑
        initLogger();
    }

    private void initLogger() {
        String env = environment.getActiveProfiles()[0];
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger rootLogger = loggerContext.getLogger("ROOT");
        Appender<ILoggingEvent> appender = rootLogger.getAppender("myLogbackAppender");
        if (appender instanceof MyLogbackAppender myLogbackAppender) {
            MachineId machineId = new MachineIdLinux();
            String app = environment.getProperty("spring.application.name");
            String host = machineId.ipv4();

            myLogbackAppender.setApp(app);
            myLogbackAppender.setHost(host);
            myLogbackAppender.setEnv(env);
            myLogbackAppender.setLogService(logService);
            myLogbackAppender.start();
            log.info("bnt use MyLogbackAppender...");
        }
    }
}
