package com.muchinfo.mtp3century.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.muchinfo.mtp3common.enumtype.GlobalExceptionEnum;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

@Data
@ApiModel(value = "AjaxResult对象", description = "公共响应对象")
@AllArgsConstructor
@NoArgsConstructor
public class AjaxResult<T> {

    // 默认成功的状态码为0
    private Integer code;

    // 返回的消息,默认成功
    private String message;

    // 保存任何数据类型的数据
    private T data;

    private final static Logger log = LoggerFactory.getLogger(AjaxResult.class);

    public static AjaxResult success(Integer code, String message, String languages) {
        message = dealMsg(message, languages);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static AjaxResult success(Integer code, String message, Object[] parameters, String languages) {
        message = dealMsgForParam(message, languages, parameters);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static AjaxResult success(Integer code, String message, String languages, Object data) {
        if (code == 200) {
            message = dealMsg(message, languages);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult success(Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult error(GlobalExceptionEnum globalExceptionEnum) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(globalExceptionEnum.getMessage());
        ajaxResult.setCode(ajaxResult.getCode());
        return ajaxResult;
    }

    public static AjaxResult error(Integer code, String message, String languages) {
        if (StringUtils.isNotBlank(languages)) {
            message = dealMsg(message, languages);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static AjaxResult error(Integer code, String message, String languages, Object data) {
        if (StringUtils.isNotBlank(languages)) {
            message = dealMsg(message, languages);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult error(Integer code, String message, Object[] parameters, String languages) {
        message = dealMsgForParam(message, languages, parameters);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static String dealMsg(String message, String languages) {
        try {
            //处理语种
            String[] split = languages.split("-");
            if (split.length > 1 && !split[0].equals("zh")) {
                languages = split[0];
            }

            // new Locale(xxx) 会转小写，资源文件message-xx-xx.properties 统一要用小写
            Locale spanish = new Locale(languages);

            // 加载资源文件
            ResourceBundle bundle = null;
            // 加载配置指定路径资源文件
            ExternalResourceBundle externalResourceBundle = SpringBeanUtils.getBean(ExternalResourceBundle.class);
            if (externalResourceBundle != null){
                 bundle = externalResourceBundle.getBundle("message", spanish);
            }else{
                // 使用包内默认
                bundle = ResourceBundle.getBundle("message", spanish);
            }

            // 获取并打印文本
        /*    String string = bundle.getString(message);
            byte[] arr = string.getBytes("ISO-8859-1");*/
            return bundle.getString(message);
        } catch (Exception e) {
            return null;
        }
    }

    public static String dealMsgForParam(String message, String languages, Object[] parameters) {
        try {
            // 根据多语言处理消息
            String messageTemplate = dealMsg(message, languages);
            if (StringUtils.isNotBlank(messageTemplate)) {
                return MessageFormat.format(messageTemplate, parameters);
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
