package com.brilliance.isc.common.validator.es.support.impl;

import com.brilliance.isc.common.validator.es.support.I18NProvider;
import com.brilliance.isc.common.validator.es.support.IJavaCallback;
import com.brilliance.isc.common.validator.es.utils.EsSpringUtils;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import java.util.Locale;

/**
 * I18N的回调
 * @author fukai
 *
 */
@AllArgsConstructor
public class I18NCallback implements IJavaCallback {

    private static Logger logger = LoggerFactory.getLogger(I18NCallback.class);

    private Locale locale;

    @Override
    public Object callback(Object... args) {
        String key = "";
        if (args.length > 0) {
            if (args[0] instanceof String) {
                key = (String) args[0];
                try{
                    I18NProvider i18NProvider = EsSpringUtils.getBean("i18n");
                    return i18NProvider.getMessage(key, locale);
                }catch (NoSuchBeanDefinitionException e){
                    logger.warn("no i18n bean");
                }
            }
        }
        return key;
    }
}
