package com.crm.rely.backend.configuration;

import com.crm.rely.backend.core.constant.Constants;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * @program: crm-backend
 * @description:
 * @author: houn
 * @create: 2019-07-25 17:25
 */

@Component
public class LocaleMessage {

    @Autowired
    private MessageSource messageSource;


    /**
     * @param code：对应文本配置的key.
     * @return 对应地区的语言消息字符串
     */
    public String getMessage(String code) {
        return getMessage(code, new Object[]{});
    }

    public String getMessage(String code, String defaultMessage) {
        Locale locale = getLocale();
        return getMessage(code, null, defaultMessage, locale);
    }

    public String getMessage(String code, String defaultMessage, Locale locale) {
        return getMessage(code, null, defaultMessage, locale);
    }

    public String getMessage(String code, Locale locale) {
        return getMessage(code, null, "", locale);
    }

    public String getMessage(String code, Object[] args) {
        return getMessage(code, args, "");
    }

    public String getMessage(String code, Object[] args, Locale locale) {
        return getMessage(code, args, "", locale);
    }

    public String getMessage(String code, Object[] args, String defaultMessage) {
        Locale locale = getLocale();
        return getMessage(code, args, defaultMessage, locale);
    }

    public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        if (locale == null) {
            locale = getLocale();
        }
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }

    public String getMessageByLang(String code, String lang) {
        Locale locale = null;
        if ("en".equals(lang)) {
            locale = new Locale(lang, "US");
        } else {
            locale = new Locale("zh", "CN");
        }

        return getMessage(code, locale);
    }

    private Locale getLocale() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String language = request.getHeader(Constants.Language);
        Locale locale = null;
        if (Strings.isNullOrEmpty(language)) {
            locale = getLocale("en", "US");
        } else {
            switch (language) {
                case "cn":
                    locale = getLocale("zh", "CN");
                    break;
                default:
                    locale = getLocale("en", "US");
                    break;
            }
        }
        return locale;
    }

    private Locale getLocale(String language, String country) {
        return new Locale(language, country);
    }
}
