package cn.com.ty.lift.business.framework.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.*;

/**
 * @author bieao
 * @description 消息工具类
 * @date 2019/12/3 11:02 AM
 */
@Slf4j
public final class MessageUtils {

    /**
     *  Message 格式化工具
     */
    public final static MessageFormat FORMAT = new MessageFormat("");
    /**
     * Message cache 中文
     */
    public final static Map<String, String> MESSAGE_CACHE_CHMAP = new HashMap<>();


    static {
        //Load Business Message
        Locale.setDefault(new Locale("zh", "CN"));
        ResourceBundle rb = ResourceBundle.getBundle("locale/response");
        Set<String> keySet = rb.keySet();
        for (String keyStr : keySet) {
            MESSAGE_CACHE_CHMAP.put(keyStr, rb.getString(keyStr));
        }
    }

    /**
     * @param key message.properties 存在的key
     * @date 2019/12/3 11:02 AM
     * @description 获取il8n下存在的value
     */
    public static String get(String key) {
        return get(key, (String[]) null);
    }

    /**
     * @param key  message.properties 存在的key
     * @param arg0 填充参数
     * @date 2019/12/3 11:02 AM
     * @description 获取il8n下存在的携带参数的value
     */
    public static String get(String key, String... arg0) {
        String message = MESSAGE_CACHE_CHMAP.get(key);
        log.info("####| 获取配置文件：{}，值：{}", key, message);
        if (StringUtils.isEmpty(message)) return "{}";
        FORMAT.applyPattern(message);
        return FORMAT.format(arg0);
    }

}
