package cclotus.weixin.util;

import lombok.Getter;
import lombok.Setter;

@SuppressWarnings("unused")
public class WeixinError extends Exception {
    public final static int ERR_UNKNOWN = -1;
    public final static int ERR_BADPARAM	= -2;
    public final static int ERR_NETWORK	= -3;
    public final static int ERR_ACCESS_DENIED	= -4;
    public final static int ERR_SERVICE_FAULT	= -5;
    public final static int ERR_APPID_INVALID	= -6;
    public final static int ERR_CODE_INVALID	= -7;
    public final static int ERR_TOKEN_INVALID	= -8;
    public final static int ERR_STATE_INVALID	= -9;

    @Getter @Setter
    protected int errcode;
    @Getter @Setter
    protected String errmsg;
    @Getter @Setter
    protected String details;

    public WeixinError(String errmsg){
        super(errmsg);
        this.errcode = ERR_UNKNOWN;
        this.errmsg = errmsg;
    }
    public WeixinError(int errcode){
        super(getErrDesc(errcode));
        this.errcode = errcode;
        this.errmsg = getErrDesc(errcode);
    }
    public WeixinError(int errcode, Throwable cause){
        super(getErrDesc(errcode),cause);
        this.errcode = errcode;
        this.errmsg = getErrDesc(errcode);
        this.details = cause.getLocalizedMessage();
    }
    public WeixinError(int errcode, String errmsg){
        this(errcode, errmsg, (String)null);
    }
    public WeixinError(int errcode, String errmsg, String details){
        super(errmsg);//super(getErrDesc(errcode,details));
        this.errcode = errcode;
        this.errmsg = errmsg;
        this.details = details;
    }
    public WeixinError(int errcode, String errmsg, Throwable cause){
        super(getErrDesc(errcode,errmsg),cause);
        this.errcode = errcode;
        this.errmsg = errmsg;
        this.details = cause.getLocalizedMessage();
    }
    public WeixinError(String errmsg, Throwable cause){
        super(errmsg,cause);
        this.errcode = ERR_UNKNOWN;
        this.errmsg = errmsg;
        this.details = cause.getLocalizedMessage();
    }

    public String toJsonString() {
        if (details != null) {
            return String.format("{ \"errcode\": %d, \"errmsg\": \"%s\", \"details\": \"%s\" }", errcode, errmsg, details);
        } else {
            return String.format("{ \"errcode\": %d, \"errmsg\": \"%s\" }", errcode, errmsg);
        }
    }

    public static String getErrDesc(int errcode){
        switch(errcode){
            case ERR_BADPARAM:
                return "参数无效";
            case ERR_NETWORK:
                return "网络错误";
            case ERR_ACCESS_DENIED:
                return "访问被拒绝";
            case ERR_SERVICE_FAULT:
                return "服务发生故障";
            case ERR_APPID_INVALID:
                return "APPID无效";
            case ERR_TOKEN_INVALID:
                return "ACCESS_TOKEN无效";
            case ERR_CODE_INVALID:
                return "认证码(code)无效";
            case ERR_STATE_INVALID:
                return "认证状态无效";
        }

        return "系统错误";
    }

    public static String getErrDesc(int errcode,String detail){
        if(errcode == ERR_UNKNOWN && detail != null)
            return detail;
        if(detail == null)
            return getErrDesc(errcode);

        return String.format("%s:%s", getErrDesc(errcode),detail);
    }
}
