package com.ads.common.data;

import cn.hutool.core.util.StrUtil;
import com.ads.common.redis.RedisManager;
import com.ads.common.util.SpringUtil;
import com.ads.common.util.UtilConst;
import com.ads.common.util.UtilMessage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @author GXS
 * @date 2018年4月9日
 * @Description:json对象
 */
@ApiModel("统一返回类")
@Data
@SuppressWarnings("all")
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final RedisManager redisManager = SpringUtil.getBean(RedisManager.class);
    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    @ApiModelProperty(value = "返回数据")
    private T data;
    @ApiModelProperty(value = "返回信息")
    private String message;

    public JsonResult(Integer code, T data, String message) {
        // 判断message是否多语言
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String messageCache = message;
        if (servletRequestAttributes != null){
            HttpServletRequest request = servletRequestAttributes.getRequest();
            String language = request.getHeader(UtilConst.LANGUAGE);
            if (StrUtil.isBlank(language)) {
                language = "zh_cn";
            }
            messageCache= redisManager.getLanguage(message, language);
            if (StrUtil.isBlank(messageCache)) {
                messageCache = message;
            }
        }
        this.data = data;
        this.message = messageCache;
        this.code = code;
    }

    public static <T> JsonResult<T> success(T data, String desc) {
        return new JsonResult(UtilConst.CODE_0, data, desc);
    }
    public static <T> JsonResult<T> success() {
        return new JsonResult(UtilConst.CODE_0, null, UtilMessage.SUCCESS);
    }
    public static <T> JsonResult<T> success(String desc) {
        return new JsonResult(UtilConst.CODE_0, null, desc);
    }
    public static <T> JsonResult<T> success(T data) {
        return new JsonResult(UtilConst.CODE_0, data, UtilMessage.SUCCESS);
    }

    public static JsonResult fail( String desc) {
        return new JsonResult(UtilConst.CODE_1001, null, desc);
    }

    public static JsonResult fail(Integer code, String desc) {
        return new JsonResult(code, null, desc);
    }

    public static JsonResult fail(Integer code, String desc, String... params) {
        JsonResult result = new JsonResult();
        result.setCode(code);
        // 判断message是否多语言
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String language = request.getHeader(UtilConst.LANGUAGE);
        if (StrUtil.isBlank(language)) {
            language = "zh_cn";
        }
        String messageCache = redisManager.getLanguage(desc, language);
        if (StrUtil.isBlank(messageCache)) {
            messageCache = desc;
        }
        String[] paramCacheArray = new String[params.length];
        int index = 0;
        for (String param : params) {
            String paramCache = redisManager.getLanguage(param, language);
            if (StrUtil.isBlank(paramCache)) {
                paramCache = param;
            }
            paramCacheArray[index] = paramCache;
            index ++;
        }
        result.setMessage(StrUtil.format(messageCache, paramCacheArray));
        return result;
    }


    public static JsonResult error() {
        return new JsonResult(UtilConst.CODE_500, null, UtilMessage.ERROR_MESSAGE);
    }


    public JsonResult() {

    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
