package com.tofly.latte_core.net.exception;

import android.app.usage.UsageEvents;

import androidx.annotation.Nullable;

import com.google.gson.JsonParseException;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.concurrent.TimeoutException;

import javax.net.ssl.SSLHandshakeException;

import retrofit2.HttpException;

public class ExceptionHandle {

    public static final int UNAUTHORIZED = 401;
    public static final int FORBIDDEY = 403;
    public static final int NOT_FOUND = 404;
    public static final int REQUEST_TIMEOUT = 408;
    public static final int INTERNAL_SERVER_ERROR = 500;
    public static final int BAD_GATEWAY = 502;
    public static final int SERVICE__UNAVAILABLE = 503;
    public static final int GATEWAY_TIMEOUT = 504;

    public static ResponseThrowable handleException(Throwable e) {
        ResponseThrowable ex;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ResponseThrowable(e, ERROR.HTTP_ERROR);
            switch (httpException.code()) {
                case UNAUTHORIZED:
                    EventBus.getDefault().post(new MyExceptionEvent(MyExceptionEvent.UNAUTHORIZED_EVENT));
                    ex.message = "";
                    break;
                case FORBIDDEY:
                    ex.message = "资源不可用";
                    break;
                case NOT_FOUND:
                    ex.message = "404";
                    break;
                case REQUEST_TIMEOUT:
                    ex.message = "请求超时";
                    break;
                case INTERNAL_SERVER_ERROR:
                    ex.message = "500，内部服务错误";
                    break;
                case BAD_GATEWAY:
                    ex.message = "无响应";
                    break;
                case SERVICE__UNAVAILABLE:
                    ex.message = "服务器不能求http请求";
                    break;
                case GATEWAY_TIMEOUT:
                    ex.message = "网关超时";
                    break;
                default:
                    ex.message = "网络错误";
                    break;
            }
            return ex;
        } else if (e instanceof UnknownHostException) {
            ex = new ResponseThrowable(e, ERROR.NETWORK_ERROR);
            ex.message = "网络不可用";
            return ex;
        } else if (e instanceof ServerException) {
            ServerException serverException = (ServerException) e;
            ex = new ResponseThrowable(serverException, serverException.code);
            ex.message = serverException.message;
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException ||
                e instanceof ParseException) {
            ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
            ex.message = "解析错误";
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ResponseThrowable(e, ERROR.NETWORK_ERROR);
            ex.message = "连接失败";
            return ex;
        } else if (e instanceof SSLHandshakeException) {
            ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else if (e instanceof TimeoutException) {
            ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
            ex.message = "连接超时";
            return ex;
        } else if (e instanceof SocketTimeoutException) {
            ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
            ex.message = "连接超时";
            return ex;
        } else {
            ex = new ResponseThrowable(e, ERROR.UNKNOWN);
            // ex.message = "未知错误";
            ex.message = e.getMessage();
            return ex;
        }
    }

    public class ERROR {
        public static final int UNKNOWN = 1000;
        public static final int PARSE_ERROR = 1001;
        public static final int NETWORK_ERROR = 1002;
        public static final int SSL_ERROR = 1005;
        public static final int HTTP_ERROR = 1003;
        public static final int TIMEOUT_ERROR = 1006;
    }


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

        public ResponseThrowable(Throwable throwable, int code) {
            super(throwable);
            this.code = code;
        }

        @Nullable
        @Override
        public String getMessage() {
            return message;
        }
    }

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

        public ServerException(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }

    public static class MyExceptionEvent {
        public final static String UNAUTHORIZED_EVENT = "unAuthorizedExceptionEvent";

        public String message;
        public Object arg0;

        public MyExceptionEvent(String message) {
            this.message = message;
        }

        public MyExceptionEvent(String message, Object arg0) {
            this.arg0 = arg0;
            this.message = message;
        }
    }
}
