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

import com.casic.dcms.bean.NewVersionBean;
import com.casic.dcms.listener.OnUpdateVersionListener;
import com.casic.dcms.mvp.model.IUpdateVersionModel;
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 UpdateVersionModelImpl implements IUpdateVersionModel {

    private final OnUpdateVersionListener updateListener;

    public UpdateVersionModelImpl(OnUpdateVersionListener listener) {
        this.updateListener = listener;
    }

    /**
     * {@link IUpdateVersionModel}
     */
    @Override
    public Subscription sendUpdateVersionRequest() {
        Observable<NewVersionBean> observable = RetrofitServiceManager.getVersionResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NewVersionBean>() {
            @Override
            public void onCompleted() {

            }

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

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