package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.LoginResultBean;
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 LoginActionModelImpl implements ILoginActionModel {

    private OnLoginListener loginListener;

    public LoginActionModelImpl(OnLoginListener listener) {
        this.loginListener = listener;
    }

    /**
     * 数据回调接口
     */
    public interface OnLoginListener {
        void onSuccess(LoginResultBean resultBean);

        void onFailure(Throwable throwable);
    }


    @Override
    public Subscription sendRetrofitRequest(String username, String key) {
        Observable<LoginResultBean> observable = RetrofitServiceManager.getTokenData(username, key);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<LoginResultBean>() {
            @Override
            public void onCompleted() {

            }

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

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