package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CaseNextNodeBean;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CaseNextNodeModelImpl implements ICaseNextNodeModel {

    private OnGetCaseNextNodeListener listener;

    public CaseNextNodeModelImpl(OnGetCaseNextNodeListener nextNodeListener) {
        this.listener = nextNodeListener;
    }

    public interface OnGetCaseNextNodeListener {
        void onSuccess(CaseNextNodeBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(String caseState) {
        Observable<CaseNextNodeBean> observable = RetrofitServiceManager.getProcessNextNodeResult(caseState);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseNextNodeBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null) {
                    listener.onFailure(e);
                }
            }

            @Override
            public void onNext(CaseNextNodeBean resultBean) {
                if (resultBean != null) {
                    listener.onSuccess(resultBean);
                }
            }
        });
    }
}
