package com.wisewoods.base.net;

import android.net.ParseException;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;

import javax.net.ssl.SSLHandshakeException;

import okhttp3.Connection;
import retrofit2.HttpException;

public class NetWorkCodeException {
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
    public static final int UNKNOWN = 1000;
    public static final int PARSE_ERROR = 1001;
    public static final int NETWORD_ERROR = 1002;
    public static final int HTTP_ERROR = 1003;
    public static final int SSL_ERROR = 1005;

    public NetWorkCodeException() {
    }

    public static ResponseThrowable getResponseThrowable(Throwable e) {
        ResponseThrowable ex;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ResponseThrowable();
            switch (httpException.code()) {
                case 401:
                case 403:
                    ex.code = 1003;
                    ex.message = "请检查权限";
                    break;
                case 404:
                case 408:
                case 500:
                case 502:
                case 503:
                case 504:
                default:
                    ex.code = 1003;
                    ex.message = "网络错误";
            }

            return ex;
        } else if (e instanceof ServerException) {
            ServerException resultException = (ServerException) e;
            ex = new ResponseThrowable();
            ex.code = resultException.code;
            ex.message = resultException.message;
            return ex;
        } else if (!(e instanceof JsonParseException) && !(e instanceof JSONException) && !(e instanceof ParseException)) {
            if (!(e instanceof Connection) && !(e instanceof ConnectException)) {
                if (e instanceof SSLHandshakeException) {
                    ex = new ResponseThrowable();
                    ex.code = 1005;
                    ex.message = "证书验证失败";
                    return ex;
                } else if (e instanceof LogicException) {
                    ex = new ResponseThrowable();
                    ex.code = ((LogicException) e).code;
                    ex.message = ((LogicException) e).getMessage();
                    return ex;
                } else {
                    ex = new ResponseThrowable();
                    ex.code = 1002;
                    ex.message = "网络错误";
                    return ex;
                }
            } else {
                ex = new ResponseThrowable();
                ex.code = 1002;
                ex.message = "连接失败";
                return ex;
            }
        } else {
            ex = new ResponseThrowable();
            ex.code = 1001;
            ex.message = "解析错误";
            return ex;
        }
    }

    public class ServerException extends RuntimeException {
        public int code;
        public String message;

        public ServerException() {
        }
    }

    public static class ResponseThrowable extends Exception {
        public int code;
        public String message;

        public ResponseThrowable() {
        }
    }
}
