package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.NewVersionBean;
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 OnUpdateListener updateListener;

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

    public interface OnUpdateListener {
        void onSuccess(NewVersionBean resultBean);

        void onFailure(Throwable throwable);
    }


    @Override
    public Subscription sendRetrofitRequest() {
        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);
                }
            }
        });
    }
}
