package com.union.ai.api.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Properties;

@Slf4j
public class PropertiesUtil {
    private static Properties prop = new Properties();

    static {
        try {
            log.info("load properties... ");
            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("base-config.properties"));
            log.info("load properties success.");
        } catch (IOException e) {
            log.error("load properties failed.", e);
        }
    }

    public static String getProperty(String key) {
        return prop.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return StringUtils.defaultIfBlank(prop.getProperty(key), defaultValue);
    }

    public static String getPropertyReplaced(String key, Object[] replacements) {
        try {
            return String.format(prop.getProperty(key), replacements);
        } catch (Exception e) {
            log.error("getReplacedValue failed. key={}, replacements={}", key, replacements, e);
            return null;
        }
    }
}
