package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.UserBean;
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 UserDataModelImpl implements IUserDataModel {

    private static final String TAG = "UserDataModelImpl";
    private OnObtainUserListener listener;

    public UserDataModelImpl(OnObtainUserListener obtainUserListener) {
        this.listener = obtainUserListener;
    }

    /**
     * 数据回调接口
     */
    public interface OnObtainUserListener {
        void onSuccess(UserBean userBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest() {
        Observable<UserBean> observable = RetrofitServiceManager.getUserData();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<UserBean>() {
            @Override
            public void onCompleted() {

            }

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

            @Override
            public void onNext(UserBean userBean) {
                if (userBean != null) {
                    listener.onSuccess(userBean);
                }
            }
        });
    }
}
