package com.xiyue.core.share;

public class ErrorMessage {
    public int code;
    public String message;
    public Exception exception;
    public static String separator = ":";

    @SuppressWarnings("unused")
    public ErrorMessage() {
    }

    public ErrorMessage(int code, String message) {
        this(code, message, null);
    }

    public ErrorMessage(int code, String message, Exception exception) {
        this.code = code;
        this.message = message;
        this.exception = exception;
    }

    @Override
    public String toString() {
        return "{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", exception=" + exception +
                '}';
    }

    public static ErrorMessage of(int code) {
        return new ErrorMessage(code, Code.desc(code));
    }
    public static ErrorMessage of(int code, String subMessage) {
        return new ErrorMessage(code, Code.desc(code) + separator + subMessage);
    }

    public static ErrorMessage of(int code, String subMessage, Exception e) {
        return new ErrorMessage(code, Code.desc(code) + separator + subMessage, e);
    }

    public static ErrorMessage emptyData(String subMessage) {
        if (subMessage == null || subMessage.trim().length() == 0) {
            return of(Code.EMPTY);
        } else {
            return of(Code.EMPTY,subMessage);
        }
    }

    public static ErrorMessage incompleteData(String subMessage) {
        return of(Code.INCOMPLETE,subMessage);
    }


    /**
     * 状态码
     * 用以表示与服务端交互响应状态及数据状态
     * 大于0的留给http
     */
    public static class Code {

        /**
         * 网络相关或数据获取相关
         */
        public static final int NET = -10;
        /**
         * 网络相关或数据获取相关,将http单独列出
         */
        public static final int HTTP = -21;
        /**
         * 网络相关或数据获取相关,将超时单独列出
         */
        public static final int TIMEOUT = -30;


        public static final int PARSE = -101;
        public static final int EMPTY = -102;
        public static final int INCOMPLETE = -103;
        //public static final int GET = -104;

        public static final int DECRYPT = -105;
        public static final int CODE = -106;
        public static final int OTHER = -999;

        private static class Desc {
            public static final String TIMEOUT = "网络访问超时";
            public static final String NET = "网络访问异常";
            public static final String HTTP = "HTTP访问异常";


            public static final String PARSE = "数据解析失败";
            public static final String EMPTY = "数据为空";
            public static final String INCOMPLETE = "数据不完整";
            //public static final String GET = "获取数据失败";
            public static final String DECRYPT = "数据解密失败";
            public static final String CODE = "返回码错误";

            public static final String OTHER = "其他异常";
        }

        public static String desc(int code) {
            if(code>0){
                return String.valueOf(code);
            }
            switch (code) {
                case NET:
                    return Desc.NET;
                case HTTP:
                    return Desc.HTTP;
                case TIMEOUT:
                    return Desc.TIMEOUT;
                case PARSE:
                    return Desc.PARSE;
                case EMPTY:
                    return Desc.EMPTY;
                case INCOMPLETE:
                    return Desc.INCOMPLETE;
//                case GET:
//                    return Desc.GET;
                case DECRYPT:
                    return Desc.DECRYPT;
                case CODE:
                    return Desc.CODE;
                default:
                return Desc.OTHER;
            }
        }
    }

}

