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

import com.casic.dcms.bean.WellListBean;
import com.casic.dcms.listener.OnGetWellListListener;
import com.casic.dcms.mvp.model.IWellListModel;
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 WellListModelImpl implements IWellListModel {

    private final OnGetWellListListener wellListListener;

    public WellListModelImpl(OnGetWellListListener listener) {
        this.wellListListener = listener;
    }

    /**
     * {@link IWellListModel}
     */
    @Override
    public Subscription sendWellListRequest(int offset) {
        Observable<WellListBean> observable = RetrofitServiceManager.getWellListResult(offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WellListBean>() {
            @Override
            public void onCompleted() {

            }

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

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