package ink.wgink.common.startup;

import ink.wgink.common.dao.access.IWhiteListDao;
import ink.wgink.common.dao.env.IEnvDao;
import ink.wgink.common.manager.access.WhiteListManager;
import ink.wgink.common.manager.env.EnvManager;
import ink.wgink.interfaces.init.IInitBaseMenu;
import ink.wgink.interfaces.init.IInitBaseTable;
import ink.wgink.interfaces.init.IInitBaseUser;
import ink.wgink.interfaces.init.IInitMongoData;
import ink.wgink.properties.BaseProperties;
import ink.wgink.properties.ServerProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: CommonStartup
 * @Description: 启动
 * @Author: WangGeng
 * @Date: 2021/6/14 22:14
 * @Version: 1.0
 **/
@Component
public class CommonStartup implements ApplicationRunner {
    private static final Logger LOG = LoggerFactory.getLogger(CommonStartup.class);
    private ApplicationContext applicationContext;
    @Autowired
    private ServerProperties serverProperties;
    @Autowired
    private BaseProperties baseProperties;
    @Autowired(required = false)
    private IInitBaseMenu initBaseMenu;
    @Autowired(required = false)
    private IInitBaseUser initBaseUser;
    private IEnvDao envDao;
    private IWhiteListDao whiteListDao;

    public CommonStartup(ApplicationContext applicationContext, IEnvDao envDao, IWhiteListDao whiteListDao) {
        this.applicationContext = applicationContext;
        this.envDao = envDao;
        this.whiteListDao = whiteListDao;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        serverInit();
        initManager();
        initMongoData();
    }

    /**
     * 初始化 mongo 数据
     */
    private void initMongoData() {
        Map<String, IInitMongoData> initMongoDataMap = applicationContext.getBeansOfType(IInitMongoData.class);
        initMongoDataMap.forEach((key, value) -> value.init());
    }

    /**
     * 初始化管理器
     */
    private void initManager() {
        LOG.debug(">>>>>> 初始化环境变量");
        EnvManager envManager = EnvManager.getInstance();
        envManager.setEnvDao(envDao);
        envManager.refreshEnv();

        LOG.debug(">>>>>> 初始化白名单");
        WhiteListManager whiteListManager = WhiteListManager.getInstance();
        whiteListManager.setBaseProperties(baseProperties);
        whiteListManager.setWhiteListDao(whiteListDao);
        whiteListManager.refreshWhiteList();
    }

    /**
     * 服务初始化
     */
    private void serverInit() {
        boolean isInitTable = serverProperties.getInit() == null || serverProperties.getInit().getTable();
        boolean isInitMenu = serverProperties.getInit() == null || serverProperties.getInit().getMenu();
        boolean isInitUser = serverProperties.getInit() == null || serverProperties.getInit().getUser();
        if (isInitTable) {
            // 建表
            Map<String, IInitBaseTable> initBaseTableMap = applicationContext.getBeansOfType(IInitBaseTable.class);
            initBaseTableMap.forEach((key, value) -> value.createTable());
        }
        if (isInitMenu) {
            // 初始化菜单
            if (initBaseMenu != null) {
                initBaseMenu.init();
            }
        }
        if (isInitUser) {
            // 创建用户
            if (initBaseUser != null) {
                initBaseUser.init();
            }
        }
    }
}
