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

import com.casic.dcms.bean.ToiletListBean;
import com.casic.dcms.listener.OnGetToiletListListener;
import com.casic.dcms.mvp.model.IToiletListModel;
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 ToiletListModelImpl implements IToiletListModel {

    private final OnGetToiletListListener toiletListListener;

    public ToiletListModelImpl(OnGetToiletListListener listener) {
        this.toiletListListener = listener;
    }

    /**
     * {@link IToiletListModel}
     */
    @Override
    public Subscription sendToiletListRequest() {
        Observable<ToiletListBean> observable = RetrofitServiceManager.getToiletListResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ToiletListBean>() {
            @Override
            public void onCompleted() {

            }

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

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