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

import com.casic.dcms.bean.AlarmDataBean;
import com.casic.dcms.listener.OnGetAlarmListListener;
import com.casic.dcms.mvp.model.IAlarmListModel;
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 AlarmListModelImpl implements IAlarmListModel {

    private final OnGetAlarmListListener alarmListListener;

    public AlarmListModelImpl(OnGetAlarmListListener listener) {
        this.alarmListListener = listener;
    }

    /**
     * {@link IAlarmListModel}
     */
    @Override
    public Subscription sendAlarmListRequest(int limit, int offset) {
        Observable<AlarmDataBean> observable = RetrofitServiceManager.getAlarmListResult(limit, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<AlarmDataBean>() {
            @Override
            public void onCompleted() {

            }

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

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