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

import com.casic.dcms.bean.PhoneBookBean;
import com.casic.dcms.listener.OnGetPhoneBookListener;
import com.casic.dcms.mvp.model.IPhoneBookModel;
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 PhoneBookModelImpl implements IPhoneBookModel {

    private final OnGetPhoneBookListener onGetPhoneBookListener;

    public PhoneBookModelImpl(OnGetPhoneBookListener listener) {
        this.onGetPhoneBookListener = listener;
    }

    /**
     * {@link IPhoneBookModel}
     */
    @Override
    public Subscription sendPhoneBookRequest(String deptid) {
        Observable<PhoneBookBean> observable = RetrofitServiceManager.getPhoneBookResult(deptid);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<PhoneBookBean>() {
            @Override
            public void onCompleted() {

            }

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

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