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

import com.casic.dcms.bean.ProcessPersonBean;
import com.casic.dcms.listener.OnGetNextProcessListener;
import com.casic.dcms.mvp.model.IProcessPersonModel;
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 ProcessPersonModelImpl implements IProcessPersonModel {

    private final OnGetNextProcessListener nextProcessListener;

    public ProcessPersonModelImpl(OnGetNextProcessListener listener) {
        this.nextProcessListener = listener;
    }

    /**
     * {@link IProcessPersonModel}
     */
    @Override
    public Subscription sendProcessPersonRequest() {
        Observable<ProcessPersonBean> observable = RetrofitServiceManager.getProcessPersonResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ProcessPersonBean>() {
            @Override
            public void onCompleted() {

            }

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

            @Override
            public void onNext(ProcessPersonBean result) {
                if (result != null) {
                    nextProcessListener.onSuccess(result);
                }
            }
        });
    }
}
