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

import com.casic.dcms.bean.LightListBean;
import com.casic.dcms.listener.OnGetLightListListener;
import com.casic.dcms.mvp.model.ILightListModel;
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 LightListModelImpl implements ILightListModel {

    private final OnGetLightListListener lightListListener;

    public LightListModelImpl(OnGetLightListListener listener) {
        this.lightListListener = listener;
    }

    /**
     * {@link com.casic.dcms.mvp.model.ILightListModel}
     */
    @Override
    public Subscription sendLightListRequest(int limit, int offset) {
        Observable<LightListBean> observable = RetrofitServiceManager.getLightListResult(limit, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<LightListBean>() {
            @Override
            public void onCompleted() {

            }

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

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