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

import com.casic.dcms.bean.UrgentCaseBean;
import com.casic.dcms.listener.OnGetUrgentCaseListListener;
import com.casic.dcms.mvp.model.IUrgentCaseListModel;
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 UrgentCaseListModelImpl implements IUrgentCaseListModel {

    private final OnGetUrgentCaseListListener urgentCaseListListener;

    public UrgentCaseListModelImpl(OnGetUrgentCaseListListener listener) {
        this.urgentCaseListListener = listener;
    }

    /**
     * {@link IUrgentCaseListModel}
     */
    @Override
    public Subscription sendUrgentCaseListRequest(String isSupervise, int offset) {
        Observable<UrgentCaseBean> observable = RetrofitServiceManager.getUrgentCaseListResult(isSupervise, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<UrgentCaseBean>() {
            @Override
            public void onCompleted() {

            }

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

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