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

import com.casic.dcms.bean.OvertimeCaseBean;
import com.casic.dcms.listener.OnGetOvertimeCaseListListener;
import com.casic.dcms.mvp.model.IOvertimeCaseListModel;
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 OvertimeCaseListModelImpl implements IOvertimeCaseListModel {

    private final OnGetOvertimeCaseListListener overtimeCaseListListener;

    public OvertimeCaseListModelImpl(OnGetOvertimeCaseListListener listener) {
        this.overtimeCaseListListener = listener;
    }

    /**
     * {@link IOvertimeCaseListModel}
     */
    @Override
    public Subscription sendOvertimeCaseListRequest(String isOvertime, int offset) {
        Observable<OvertimeCaseBean> observable = RetrofitServiceManager.getOvertimeCaseListResult(isOvertime, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<OvertimeCaseBean>() {
            @Override
            public void onCompleted() {

            }

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

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