package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CaseHandleBean;
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 CaseHandleListModelImpl implements ICaseHandleListModel {

    private OnGetCaseHandleListListener listener;

    public CaseHandleListModelImpl(OnGetCaseHandleListListener caseHandleListListener) {
        this.listener = caseHandleListListener;
    }

    public interface OnGetCaseHandleListListener {
        void onSuccess(CaseHandleBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(String isOvertime, String isEmergent, int limit, int offset) {
        Observable<CaseHandleBean> observable = RetrofitServiceManager.getCaseHandleListResult(isOvertime,
                isEmergent, limit, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseHandleBean>() {
            @Override
            public void onCompleted() {

            }

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

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