package com.sxyz.rfid.retrofit.callback;

import com.google.gson.JsonParseException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.utils.ToastUtil;


import org.json.JSONException;

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

/**
 * @Description: 网络请求异常处理
 * @Author: wuchunyuan
 * @CreateDate: 2020/7/9 11:02
 * @UpdateUser: wuchunyuan
 * @UpdateDate: 2020/7/9 11:02
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class NetErrorHandler {
    public static void getErrorMessage(Throwable throwable) {
        if (throwable instanceof HttpException) {     //   HTTP错误
            onException(ExceptionReason.HTTP_ERROR);
        } else if (throwable instanceof TimeoutException || throwable instanceof InterruptedIOException) {//连接服务器超时
            onException(ExceptionReason.CONNECT_TIMEOUT);
        } else if (throwable instanceof UnknownHostException || throwable instanceof ConnectException) {//网络异常,请检查您的网络
            onException(ExceptionReason.CONNECT_ERROR);
        } else if (throwable instanceof JsonParseException || throwable instanceof JSONException
                || throwable instanceof ParseException) {//解析数据失败
            onException(ExceptionReason.PARSE_ERROR);
        } else {//未知异常
            onException(ExceptionReason.UNKNOWN_ERROR);
        }
    }

    /**
     * @description 返回异常错误信息
     * @author wuchunyuan
     * @time 2020/7/9 11:51
     */
    private static void onException(ExceptionReason reason) {
        switch (reason) {
            case HTTP_ERROR://HTTP错误
                ToastUtil.showShort(App.getContext().getResources().getString(R.string.tips_http_error));
                break;
            case CONNECT_TIMEOUT://连接服务器超时
                ToastUtil.showShort(App.getContext().getResources().getString(R.string.tips_connection_server_timeout));
                break;
            case PARSE_ERROR://解析数据失败
                ToastUtil.showShort(App.getContext().getResources().getString(R.string.tips_json_syntax_error));
                break;
            case CONNECT_ERROR://链接错误
                ToastUtil.showShort(App.getContext().getResources().getString(R.string.tips_connect_error));
                break;
            case UNKNOWN_ERROR://未知异常
            default://未知异常
                ToastUtil.showShort(App.getContext().getResources().getString(R.string.tips_unknown_error));
                break;
        }
    }

    /**
     * @author wuchunyuan
     * @description 请求网络失败原因
     * @time 2020/7/9 17:19
     */
    public enum ExceptionReason {
        /**
         * HTTP错误
         */
        HTTP_ERROR,
        /**
         * 连接服务器超时
         */
        CONNECT_TIMEOUT,
        /**
         * 解析数据失败
         */
        PARSE_ERROR,
        /**
         * 连接错误
         */
        CONNECT_ERROR,
        /**
         * 未知错误
         */
        UNKNOWN_ERROR,
    }
}
