package cn.iocoder.yudao.framework.i18n.core;

import cn.hutool.core.util.CharsetUtil;
import cn.iocoder.yudao.framework.common.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.io.IOException;
import java.util.Locale;

/**
 * 多语言国际化消息工具类
 *
 * @author
 */
@Slf4j
public class I18nMessage {
    private static MessageSourceAccessor accessor;

    static {
        ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
        reloadableResourceBundleMessageSource.setBasenames(Constant.I18N_MSG);
        reloadableResourceBundleMessageSource.setCacheSeconds(Constant.I18N_CACHE_SEC);
        reloadableResourceBundleMessageSource.setDefaultEncoding(CharsetUtil.UTF_8);
        accessor = new MessageSourceAccessor(reloadableResourceBundleMessageSource);
    }

    /**
     * 获取一条语言配置信息
     *
     * @param message 配置信息属性名,eg: api.response.code.user.signUp
     * @return
     */
    public static String getMessage(String message) {
        Locale locale = LocaleContextHolder.getLocale();
        try {
            return accessor.getMessage(message, locale);
        } catch (Exception e) {
            return message;
        }
    }

    /**
     * 获取一条语言配置信息
     *
     * @param message 配置信息属性名,eg: api.response.code.user.signUp
     * @return
     */
    public static String getMessage(String message, Object... arg) {
        Locale locale = LocaleContextHolder.getLocale();
        try {
            return accessor.getMessage(message, arg, locale);
        } catch (Exception e) {
            return message;
        }
    }

    /**
     * 获取一条语言配置信息
     * TODO 此方法暂时只支持中英文国际化，如需更多，待优化
     *
     * @param message 配置信息属性名,eg: api.response.code.user.signUp
     * @return
     */
    public static String getMessage(String message, Integer lang) {
        Locale locale = Languages.getLocaleByCode(lang);
        try {
            return accessor.getMessage(message, locale);
        } catch (Exception e) {
            return message;
        }
    }


    /**
     * 获取一条英文语言配置信息
     *
     * @param message 配置信息属性名,eg: api.response.code.user.signUp
     * @return
     */
    public static String getEnMessage(String message) {
        Locale locale = Locale.ENGLISH;
        try {
            return accessor.getMessage(message, locale);
        } catch (Exception e) {
            return message;
        }
    }


    /**
     * 获取一条中文语言配置信息
     *
     * @param message 配置信息属性名,eg: api.response.code.user.signUp
     * @return
     */
    public static String getZhMessage(String message) {
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        try {
            return accessor.getMessage(message, locale);
        } catch (Exception e) {
            return message;
        }
    }

    /**
     * 获取一条语言配置信息（后台管理）
     * 判断当前系统使用的语言
     * @return
     * @throws IOException
     */
    public static Integer getLang() {
        Locale locale = LocaleContextHolder.getLocale();
        return LangEnum.valueOf(locale).getLang();
    }
}
