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

import com.casic.dcms.bean.MapConfigBean;
import com.casic.dcms.listener.OnGetMapConfigListener;
import com.casic.dcms.mvp.model.IMapConfigModel;
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 MapConfigModelImpl implements IMapConfigModel {

    private final OnGetMapConfigListener obtainMapConfigListener;

    public MapConfigModelImpl(OnGetMapConfigListener listener) {
        this.obtainMapConfigListener = listener;
    }

    /**
     * {@link IMapConfigModel}
     */
    @Override
    public Subscription sendMapConfigRequest() {
        Observable<MapConfigBean> observable = RetrofitServiceManager.getMapConfig();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<MapConfigBean>() {
            @Override
            public void onCompleted() {

            }

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

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