package org.agt.framework.banner.core;

import cn.hutool.core.thread.ThreadUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

/**
 * 项目启动成功后，提供文档相关的地址
 *
 * @author 千通源码
 */
@Slf4j
public class BannerApplicationRunner implements ApplicationRunner {

    @Resource
    private Environment environment;

    @Override
    public void run(ApplicationArguments args) {
        ThreadUtil.execute(() -> {
            ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒，保证输出到结尾

            String port = environment.getProperty("server.port");
            String ip = "127.0.0.1";
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                log.error(e.getMessage());
            }

            log.info("\n----------------------------------------------------------------\n\t" +
                            "项目启动成功！\n\t" +
                            "接口文档 Local: \t\t{} \n\t" +
                            "接口文档 External: \t{} \n" +
                            "----------------------------------------------------------------",
                    "http://localhost:" + port + "/doc.html",
                    "http://" + ip + ":" + port + "/doc.html");

        });
    }

}
