package com.casic.dcms.mvp.model.impl;

import com.casic.dcms.bean.CaseNextNodeBean;
import com.casic.dcms.listener.OnGetCaseNextNodeListener;
import com.casic.dcms.mvp.model.ICaseNextNodeModel;
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 final OnGetCaseNextNodeListener nextNodeListener;

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

    /**
     * {@link ICaseNextNodeModel}
     */
    @Override
    public Subscription sendCaseNextNodeRequest(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) {
                    nextNodeListener.onFailure(e);
                }
            }

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