package com.zxhl.cms.net.callback;

import android.text.TextUtils;

import com.zxhl.cms.AppContext;
import com.zxhl.cms.ad.upload.model.Response;
import com.zxhl.cms.ad.upload.model.Response2;
import com.zxhl.cms.common.Constant;
import com.zxhl.cms.net.RxExceptionUtil;
import com.zxhl.cms.net.SettingPreference;
import com.zxhl.cms.utils.JumpUtils;
import com.zxhl.cms.utils.RxBus;
import com.zxhl.cms.utils.Utils;

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

/**
 * Created by gaoleichao on 2018/5/5.
 */

public abstract class BaseObserver2<T> implements Observer<Response2<T>> {

    private Disposable disposable;

    @Override
    public void onNext(@NonNull Response2<T> result) {
        if (TextUtils.equals(result.getError(), "0")) {
            if (result.getData() != null) {
                onSuccess(result.getData());
            } else {
                onSuccess(null);
            }
        } else{
            onFailure(new Exception(result.getMsg()), result.getError(), result.getMsg());//该异常可以汇报服务端
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        onFailure(e, "103", RxExceptionUtil.exceptionHandler(e));
    }

    @Override
    public void onComplete() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        disposable = d;
    }

    public abstract void onSuccess(T result);

    public abstract void onFailure(Throwable e, String code, String errorMsg);
}
