package ink.wgink.login.base.manager;

import com.alibaba.fastjson.JSONObject;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.manager.ISystemConfigManager;
import ink.wgink.login.base.dao.config.IConfigDao;
import ink.wgink.login.base.pojo.dtos.config.ConfigDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ConfigManager
 * @Description: 系统配置管理
 * @Author: WangGeng
 * @Date: 2019/11/20 10:34
 * @Version: 1.0
 **/
public class ConfigManager implements ISystemConfigManager {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigManager.class);
    private static ConfigManager configManager = ConfigManagerBuilder.configManager;
    private IConfigDao configDao;
    private Map<String, String> config;
    public static final String SYSTEM_LICENSE = "systemLicense";
    public static final String CONFIG_KEY = "configKey";
    public static final String CONFIG_VALUE = "configValue";

    private ConfigManager() {
    }

    public static ConfigManager getInstance() {
        return configManager;
    }

    /**
     * 获取配置
     *
     * @return
     */
    @Override
    public Map<String, String> getConfig() {
        if (config == null) {
            refreshConfig();
        }
        return config;
    }

    @Override
    public Map<String, String> getSystemConfigParams() {
        Map<String, String> config = getConfig();
        String systemParams = config.get("systemConfigParams");
        if (StringUtils.isBlank(systemParams)) {
            return null;
        }
        return JSONObject.parseObject(systemParams, Map.class);
    }

    @Override
    public String getSystemConfigParamsValue(String key) {
        Map<String, String> systemParams = getSystemConfigParams();
        if (systemParams == null) {
            return "";
        }
        return systemParams.get(key);
    }

    @Override
    public Map<String, String> getAppConfigParams() {
        Map<String, String> config = getConfig();
        String appParams = config.get("appConfigParams");
        if (StringUtils.isBlank(appParams)) {
            return null;
        }
        return JSONObject.parseObject(appParams, Map.class);
    }

    @Override
    public String getAppConfigParamsValue(String key) {
        Map<String, String> appParams = getAppConfigParams();
        if (appParams == null) {
            return "";
        }
        return appParams.get(key);
    }

    /**
     * 刷新配置
     */
    @Override
    public void refreshConfig() {
        try {
            List<ConfigDTO> configDTOs = configDao.list();
            if (config == null) {
                config = new HashMap<>(0);
            } else {
                config.clear();
            }
            configDTOs.forEach(configDTO -> {
                config.put(configDTO.getConfigKey(), configDTO.getConfigValue());
            });
        } catch (SearchException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    public void setConfigDao(IConfigDao configDao) {
        this.configDao = configDao;
    }

    private static class ConfigManagerBuilder {
        public static final ConfigManager configManager = new ConfigManager();
    }

}
