package com.sybotan.android.demo.retrofit;

import com.sybotan.android.demo.activities.BaseActivity;
import com.sybotan.android.demo.bean.BaseModel;
import com.sybotan.android.demo.viewmodel.BaseViewModel;

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

/**
 * Created by weimo on 2020/7/13.
 */
public abstract class BaseObserver<T> implements Observer<BaseModel<T>> {
    private BaseViewModel baseViewModel;

    private BaseActivity mActivity;

    public BaseObserver(BaseActivity activity, BaseViewModel baseViewModel) {
        mActivity = activity;
        this.baseViewModel = baseViewModel;
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseModel<T> value) {
        if (value.isSuccess()) {
            T t = value.getData();
            onSuccess(t);
        } else {
            onFailure(value.message);
        }
    }

    @Override
    public void onError(Throwable e) {
        baseViewModel.showErrorLoading("");
        /*if (NetUtil.getNetWorkState(MyApplication.getAppContext()) < 0) {
            onFailure("请检查网络设置");
        } else if (e instanceof HttpException) {
            onFailure("请求失败");
        } else if (e instanceof JsonParseException) {
            onFailure("数据错误");
        } else if (e instanceof ConnectException) {
            onFailure("连接失败");
        } else if (e instanceof SocketTimeoutException) {
            onFailure("网络连接超时");
        } else {
            onFailure("未知错误");
        }*/
        e.printStackTrace();
    }

    @Override
    public void onComplete() {

    }

    /**
     * 只是接口调用成功，此项目的接口返回内容无法判断接口返回是成功/失败
     *
     * @param t
     */
    protected abstract void onSuccess(T t);

    /**
     * 接口调用失败
     *
     * @param msg
     */
    protected void onFailure(String msg) {

    }
}
