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

import com.casic.dcms.bean.OnlinePersonBean;
import com.casic.dcms.listener.OnGetOnlinePersonListener;
import com.casic.dcms.mvp.model.IPersonOnlineModel;
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 PersonOnlineModelImpl implements IPersonOnlineModel {

    private final OnGetOnlinePersonListener onlinePersonListener;

    public PersonOnlineModelImpl(OnGetOnlinePersonListener listener) {
        this.onlinePersonListener = listener;
    }

    /**
     * {@link IPersonOnlineModel}
     */
    @Override
    public Subscription sendPersonOnlineRequest(String deptid, String roleTips, String isOnLine) {
        Observable<OnlinePersonBean> observable = RetrofitServiceManager.getOnlinePersonResult(deptid, roleTips, isOnLine);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<OnlinePersonBean>() {
            @Override
            public void onCompleted() {

            }

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

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