package com.ssyf.common.web.i18n;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;

import java.util.Locale;
import java.util.Map;

/**
 * 自定义国际化资源
 * 优先从nacos配置中获取数据，其次在从本地配置文件中获取
 *
 * @author huwei
 * @date 2022/7/5 11:45
 */
@Slf4j
@Component
public class CustomMessageSource implements MessageSource {

    /**
     * 动态国际化数据源
     */
    @Autowired
    private MessageProperties messageProperties;

    /**
     * 本地数据源
     */
    @Lazy
    @Autowired
    private ResourceBundleMessageSource messageSource;

    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        //存放路径,需存放在项目的classpath路径下；
        messageSource.setBasename("i18n/message");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Override
    public String getMessage(String s, Object[] objects, String s1, Locale locale) {
        log.warn("【国际化】当前方法未做实现：{}.", this.getClass().getName(), "getMessage(String s, Object[] objects, String s1, Locale locale)");
        return null;
    }

    /**
     * 获取消息的国际化信息
     * 1. 优先从nacos，yml等配置文件中获取国际化信息
     * 2. 再本地配置信息中获取
     * 3. 返回null
     * @param s
     * @param objects
     * @param locale
     * @return
     * @throws NoSuchMessageException
     */
    @Override
    public String getMessage(String s, Object[] objects, Locale locale) throws NoSuchMessageException {
        String result = null;
        //优先从动态配置中获取数据
        Map<String, String> dynamicMessageMap = this.messageProperties.getMessage().get(locale.getLanguage());
        if (dynamicMessageMap == null) {
            //默认配置中获取数据
            dynamicMessageMap = this.messageProperties.getDefaultMessage();
        }
        if (dynamicMessageMap != null) {
            result = dynamicMessageMap.get(s);
        }

        if (StrUtil.isBlank(result)) {
            try {
                return this.messageSource.getMessage(s, objects, locale);
            }catch (Exception e){
                //获取失败，不做处理
            }
        }

        return result;
    }

    /**
     * 获取消息，当消息不不存在时返回null
     *
     * @param s
     * @param objects
     * @param locale
     * @return
     */
    public String getMessageDefaultNull(String s, Object[] objects, Locale locale) {
        return getMessage(s, objects, locale);
    }

    @Override
    public String getMessage(MessageSourceResolvable messageSourceResolvable, Locale locale) throws NoSuchMessageException {
        log.warn("【国际化】当前方法未做实现：{}.", this.getClass().getName(), "getMessage(MessageSourceResolvable messageSourceResolvable, Locale locale) throws NoSuchMessageException");
        return null;
    }
}
