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

import com.casic.dcms.bean.UserBean;
import com.casic.dcms.listener.OnGetUserListener;
import com.casic.dcms.mvp.model.IUserDataModel;
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 final OnGetUserListener obtainUserListener;

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

    /**
     * {@link IUserDataModel}
     */
    @Override
    public Subscription sendUserDataRequest() {
        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) {
                    obtainUserListener.onFailure(e);
                }
            }

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