package com.sxyz.rfid.retrofit.callback;


import com.google.gson.Gson;
import com.sxyz.rfid.utils.L;
import com.sxyz.rfid.utils.ToastUtil;

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

/**
 * @Description: 抽象的回调接口 返回数据String
 * @Author: wuchunyuan
 * @CreateDate: 2020/7/8 13:42
 * @UpdateUser: wuchunyuan
 * @UpdateDate: 2020/7/8 13:42
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public abstract class StringObserver<T extends ResponseBase> implements Observer<ResponseBase<String>> {

    @Override
    public void onSubscribe(Disposable d) {
    }

    @Override
    public void onNext(ResponseBase<String> response) {
        boolean mSuccess = response.success;
        String mErrorCode = response.code;
        String mMsg = response.msg;
        if (mErrorCode.equals("-1")) {
            if (mSuccess) {
                onSuccess(response.body);
                L.json("返回的json===>" + new Gson().toJson(response.body));
//                L.i("okHttp\n" + new Gson().toJson(response.body));
            } else {
                ToastUtil.showShort(mMsg);
                return;
            }
        } else {
            ToastUtil.showShort(mMsg);
            return;
        }
    }

    @Override
    public void onError(Throwable throwable) {
        onFailed(throwable);
        com.sxyz.courtai.retrofit.callback.NetErrorHandler.getErrorMessage(throwable);
    }

    @Override
    public void onComplete() {
    }

    public abstract void onSuccess(String response);

    public abstract void onFailed(Throwable throwable);

}
