package com.unis.framework.banner.core;

import cn.hutool.core.thread.ThreadUtil;
import com.unis.framework.common.util.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.concurrent.TimeUnit;

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

    @Value("${server.port}")
    private int port;
    @Value("${delivery.info.name}")
    private String name;
    @Value("${spring.profiles.active}")
    private String profilesActive;


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

            if (SpringUtils.isProd()) {
                log.info("\n----------------------------------------------------------\n\t" +
                                "【{}】：项目启动成功！\n" +
                                "----------------------------------------------------------"
                        ,name);
                return;
            }
            //获取当前服务器 IP
            String url = "localhost:" + port;
            try {
                url = SpringUtils.getLocalIp() + ":" + port;
            }catch (Exception ignored){
            }

            log.info("\n----------------------------------------------------------\n\t" +
                            "【{}】：项目启动成功！\n\t" +
                            "【当前环境】: \t{}\n\t" +
                            "【接口地址】: \thttp://{} \n\t" +
                            "【接口文档】: \thttp://{}/doc.html \n\t" +
                            "【开发文档】: \t{} \n" +
                            "----------------------------------------------------------",
                    name,profilesActive,url,url,
                    "https://doc.iocoder.cn");
        });
    }
}
