package com.tenlionsoft.baselib.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.core.beans.ErrorBean;

import java.net.SocketTimeoutException;

import retrofit2.HttpException;

/**
 * 作者: Adam
 * 日期: 2019/8/13 - 09:51
 * 邮箱: itgaojian@163.com
 * 描述:
 */
public class ExceptionHandler {

    public static void handleException(Throwable throwable) {
        if (throwable instanceof HttpException) {
            HttpException ex = (HttpException) throwable;
            try {
                String errBody = ex.response().errorBody().string();
                if (!TextUtils.isEmpty(errBody)) {
                    ErrorBean errorBean = new Gson().fromJson(errBody, ErrorBean.class);
                    if (!TextUtils.isEmpty(errorBean.getMsg())) {
                        ToastUtils.show(errorBean.getMsg());
                    } else if (!TextUtils.isEmpty(errorBean.getMessage())) {
                        ToastUtils.show(errorBean.getMessage());
                    } else {
                        ToastUtils.show("网络错误,请稍后重试");
                    }
                }
            } catch (Exception e1) {
                ToastUtils.show("网络错误,请稍后重试");
                e1.printStackTrace();
            }
        } else if (throwable instanceof JsonParseException) {
            ToastUtils.show("解析错误");
        } else if (throwable instanceof SocketTimeoutException) {
            ToastUtils.show("网络连接超时");
        } else {
            ToastUtils.show("网络错误,请稍后重试");
        }
    }

    public static int handleExceptionWithLogin(Throwable throwable) {
        if (throwable instanceof HttpException) {
            HttpException ex = (HttpException) throwable;
            try {
                String errBody = ex.response().errorBody().string();
                if (!TextUtils.isEmpty(errBody)) {
                    ErrorBean errorBean = new Gson().fromJson(errBody, ErrorBean.class);
                    LogUtils.e(errorBean.getCode() + "===" + errorBean.getMessage());
                    if (!TextUtils.isEmpty(errorBean.getMsg())) {
                        ToastUtils.show(errorBean.getMsg());
                    } else {
                        if (!TextUtils.isEmpty(errorBean.getMessage())) {
                            ToastUtils.show(errorBean.getMessage());
                        } else {
                            ToastUtils.show("网络错误,请稍后重试" + errorBean.getStatus());
                        }
                    }
                    return errorBean.getCode();
                } else {
                    return 0;
                }
            } catch (Exception e1) {
                ToastUtils.show("网络错误,请稍后重试");
                e1.printStackTrace();
                return 0;
            }
        } else if (throwable instanceof JsonParseException) {
            ToastUtils.show("解析错误,请稍后重试");
            return 0;
        } else if (throwable instanceof SocketTimeoutException) {
            ToastUtils.show("网络连接超时");
            return 0;
        } else {
            ToastUtils.show("网络错误,请稍后重试");
            return 0;
        }

    }
}
