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

import com.casic.dcms.bean.LightMapBean;
import com.casic.dcms.listener.OnGetLightMapListListener;
import com.casic.dcms.mvp.model.ILightMapModel;
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 LightMapModelImpl implements ILightMapModel {

    private final OnGetLightMapListListener lightMapListListener;

    public LightMapModelImpl(OnGetLightMapListListener listener) {
        this.lightMapListListener = listener;
    }

    /**
     * {@link ILightMapModel}
     */
    @Override
    public Subscription sendLightOnMapRequest() {
        Observable<LightMapBean> observable = RetrofitServiceManager.getLightListResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<LightMapBean>() {
            @Override
            public void onCompleted() {

            }

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

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