package com.hh.lib_base.retrofit;

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

import com.google.gson.JsonSyntaxException;
import com.hh.lib_base.retrofit.bean.BaseBean;
import com.hh.lib_base.retrofit.exception.BaseException;
import com.hh.lib_base.retrofit.rx.RxErrorHandler;
import com.hh.lib_base.utils.LogUtils;


import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * @Description:
 * @author: Xia WeiHao
 * @date: 2023/4/11
 */
public abstract class SimpleObserver<T> implements Observer<BaseBean<T>> {

    // 当前类名
    public String mTag = getClass().getSimpleName() + "--";
    public RxErrorHandler mRxErrorHandler;
    String url;



    public SimpleObserver(String url) {
        this.url = url;
        mRxErrorHandler = new RxErrorHandler();
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseBean<T> baseResponse) {
        T data = baseResponse.getData();
        onSuccess(data);

    }

    @Override
    public void onError(Throwable e) {

        LogUtils.e("ProgressSubscriber-->onError");
        LogUtils.e("ProgressSubscriber-->onError--" + e.toString());

        e.printStackTrace();

        BaseException baseException = mRxErrorHandler.handleError(e);
        LogUtils.e("ProgressSubscriber-->onError-->code:" + baseException.getCode());
        LogUtils.e("ProgressSubscriber-->onError-->Message:" + baseException.getDispalyMessage());

        LogUtils.e(mTag + "url = " + url);
        if (TextUtils.isEmpty(url)) {
            showError("",baseException.getDispalyMessage() + "");
        } else {
            showError(url, baseException.getDispalyMessage() + "");
        }


        //token失效
        if (BaseException.ERROR_TOKEN.equals(baseException.getCode() + "")) {
            LogUtils.e("ProgressSubscriber   token失效");
            // 可以这边处理直接跳转。也可以这边不处理，放到base的Activity/fragment中去checkLogin判断弹框跳转
//            toLogin();
        }
    }

    @Override
    public void onComplete() {
    }

   public abstract void onSuccess(T t) ;
    public abstract void showError(String url,String msg);


}
