package com.hw.nativeapp.httpnet;

import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.StringRes;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hw.nativeapp.MApplication;
import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.entity.ResponseData;
import com.hw.nativeapp.ui.dialogs.LoadingDialog;

import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

/**
 * 默认错误的处理
 */
public class ErrorConsumer implements Consumer<Throwable> {

    private int stringRes;

    public ErrorConsumer() {
    }

    public ErrorConsumer(@StringRes int stringRes) {
        this.stringRes = stringRes;
    }

    @Override
    public void accept(Throwable throwable) throws Exception {
//        LoadingDialogUtils.cancelLoadingDialog();
        LoadingDialog.getInstance(MApplication.getAppContext()).hide();
        LogUtils.e("error : " + throwable);

        if (throwable instanceof HttpException) {
            HttpException exception = (HttpException) throwable;
            ResponseBody errorBody = exception.response().errorBody();
            String content = errorBody.string();

            if (!TextUtils.isEmpty(content) && content.startsWith("{")) {
                ResponseData responseData = JSONObject.parseObject(content, ResponseData.class);
                if (responseData != null) {
                    String msg = responseData.getMsg();
                    toast(msg);
                }

            } else {
                    String msg = getMsg(exception);
                    if (!TextUtils.isEmpty(msg)) {
                        toast(msg);
                    } else if (stringRes != 0) {
                        ToastUtils.showShort(MApplication.getInstance().getText(stringRes));
                    }

            }

        } else {
            if (stringRes <= 0) {
                String msg = throwable.getMessage();
                if (!TextUtils.isEmpty(msg)) {
                    if (msg.contains("connect")) {
                        msg = MApplication.getInstance().getString(R.string.network_error);
                    }
                    if(msg.indexOf("No address associated with hostname") > -1){
                        toast("网络错误,请检查网络！");
                    } else if (msg.indexOf(".setVisibility(int)") > -1){

                    } else if (msg.indexOf("to post notification on channel") > -1){

                    }else if (msg.indexOf("on a null object reference") > -1){

                    } else if (msg.indexOf("Index: 0, Size: 0") > -1){

                    } else {
                        toast(msg);
                    }
                } else {
                    if(msg.indexOf("No address associated with hostname") > -1){
                        toast("网络错误,请检查网络！");
                    } else if (msg.indexOf(".setVisibility(int)") > -1){

                    } else if (msg.indexOf("to post notification on channel") > -1){

                    }else if (msg.indexOf("on a null object reference") > -1){

                    }else {
                        toast(msg);
                    }
                }
            } else {
                ToastUtils.showShort(MApplication.getInstance().getText(stringRes));
            }
        }

    }

    private void toast(String msg2) {
        ResponseConsumer.showErrorMsg(msg2);
    }

//    protected void needLogin() {
//        App.logout(App.getInstance());
//    }

    private String getMsg(HttpException exception) {
        String msg = exception.message();
        if (TextUtils.isEmpty(msg)) {
            msg = exception.getMessage();
        }
        if (msg != null && msg.contains("time out")) {
            msg = MApplication.getInstance().getString(R.string.network_time_out);
        }
        return msg;
    }

}
