package org.linlinjava.jz.core.system;

import org.linlinjava.jz.core.util.SystemInfoPrinter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 系统启动服务，用于设置系统配置信息、检查系统状态及打印系统信息
 */
@Component
class SystemInistService {
    private SystemInistService systemInistService;


    @Autowired
    private Environment environment;

    @PostConstruct
    private void inist() {
        systemInistService = this;
        initConfigs();
        SystemInfoPrinter.printInfo("jz 初始化信息", getSystemInfo());
    }


    private final static Map<String, String> DEFAULT_CONFIGS = new HashMap<>();

    static {
        // 小程序相关配置默认值
        DEFAULT_CONFIGS.put(SystemConfig.jz_CALL_BUSY, "3");
        DEFAULT_CONFIGS.put(SystemConfig.jz_CALL_NO_CONNECT_TIMES, "3");
        DEFAULT_CONFIGS.put(SystemConfig.jz_CALL_NO_ANSWER, "3");
        DEFAULT_CONFIGS.put(SystemConfig.jz_CALL_OVERUDE, "24");
        DEFAULT_CONFIGS.put(SystemConfig.jz_CALL_POWER_OFF, "24");
        DEFAULT_CONFIGS.put(SystemConfig.Jz_CALL_NO_CONNECTED, "2");

    }

    //@Autowired
    //private JzMallConfigService jzSystemConfigService;

    private void initConfigs() {
        /*1. 读取数据库全部配置信息
        Map<String, String> configs = jzSystemConfigService.queryAll();

        // 2. 分析DEFAULT_CONFIGS
        for (Map.Entry<String, String> entry : DEFAULT_CONFIGS.entrySet()) {
            if (configs.containsKey(entry.getKey())) {
                continue;
            }

            configs.put(entry.getKey(), entry.getValue());
            jzSystemConfigService.addConfig(entry.getKey(), entry.getValue());
        }

        SystemConfig.setConfigs(configs);*/
    }

    private Map<String, String> getSystemInfo() {

        Map<String, String> infos = new LinkedHashMap<>();

        infos.put(SystemInfoPrinter.CREATE_PART_COPPER + 0, "系统信息");
        // 测试获取application-db.yml配置信息
        infos.put("服务器端口", environment.getProperty("server.port"));
        infos.put("数据库USER", environment.getProperty("spring.datasource.druid.username"));
        infos.put("数据库地址", environment.getProperty("spring.datasource.druid.url"));
        infos.put("调试级别", environment.getProperty("logging.level.org.linlinjava.jz.wx"));

        // 测试获取application-core.yml配置信息
        infos.put(SystemInfoPrinter.CREATE_PART_COPPER + 1, "模块状态");
        infos.put("邮件", environment.getProperty("jz.notify.mail.enable"));
        infos.put("短信", environment.getProperty("jz.notify.sms.enable"));
        infos.put("模版消息", environment.getProperty("jz.notify.wx.enable"));
        infos.put("快递信息", environment.getProperty("jz.express.enable"));
        infos.put("快递鸟ID", environment.getProperty("jz.express.appId"));
        infos.put("对象存储", environment.getProperty("jz.storage.active"));
        infos.put("本地对象存储路径", environment.getProperty("jz.storage.local.storagePath"));
        infos.put("本地对象访问地址", environment.getProperty("jz.storage.local.address"));
        infos.put("本地对象访问端口", environment.getProperty("jz.storage.local.port"));


        return infos;
    }
}
