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

import com.casic.dcms.bean.CaseProcessBean;
import com.casic.dcms.listener.OnGetCaseProcessListener;
import com.casic.dcms.mvp.model.ICaseProcessModel;
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 CaseProcessModelImpl implements ICaseProcessModel {

    private final OnGetCaseProcessListener caseProcessListener;

    public CaseProcessModelImpl(OnGetCaseProcessListener listener) {
        this.caseProcessListener = listener;
    }

    /**
     * {@link ICaseProcessModel}
     */
    @Override
    public Subscription sendCaseProcessRequest(String processId) {
        Observable<CaseProcessBean> observable = RetrofitServiceManager.getCaseProcessResult(processId);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseProcessBean>() {
            @Override
            public void onCompleted() {

            }

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

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