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

import com.casic.dcms.bean.DeptTreeBean;
import com.casic.dcms.listener.OnGetDeptTreeListener;
import com.casic.dcms.mvp.model.IDeptTreeModel;
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 DeptTreeModelImpl implements IDeptTreeModel {

    private final OnGetDeptTreeListener deptTreeListener;

    public DeptTreeModelImpl(OnGetDeptTreeListener listener) {
        this.deptTreeListener = listener;
    }

    /**
     * {@link com.casic.dcms.mvp.model.IDeptTreeModel}
     */
    @Override
    public Subscription sendDeptTreeRequest(String pid) {
        Observable<DeptTreeBean> observable = RetrofitServiceManager.getDeptTreeResult(pid);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<DeptTreeBean>() {
            @Override
            public void onCompleted() {

            }

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

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