package com.wd.workoffice.presenter;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.bean.DepBean;
import com.wd.workoffice.contract.UserRegisterThreeContract;
import com.wd.workoffice.model.UserRegisterThreeModel;
import com.wd.workoffice.retrofit.WorkObserver;

import java.util.Map;

import flexible.xd.android_base.mvpBase.BasePresenter;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class UserRegisterThreePresenter extends BasePresenter<UserRegisterThreeContract.View, UserRegisterThreeContract.Model>
        implements UserRegisterThreeContract.Presenter {
    @Override
    public void register(Map<String, Object> param) {
        mView.showLoading();
        mModel.register(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
                mDisposable.add(d);
            }

            @Override
            public void onFail(String errorMsg) {
                mView.hideLoading();
                mView.onError(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                mView.hideLoading();
                if (data.getCode() != 0) {
                    mView.onError(data.getMessage());
                    return;
                }
                mView.registerSuccess(data.getData().toString());
            }
        });
    }

    @Override
    public void depList() {
        mView.showLoading();
        mModel.depList().compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
                mDisposable.add(d);
            }

            @Override
            public void onFail(String errorMsg) {
                mView.hideLoading();
                mView.onError(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                mView.hideLoading();
                if (data.getCode() != 0) {
                    mView.onError(data.getMessage());
                    return;
                }
                mView.depSuccess(JSON.parseArray(data.getData().toString(), DepBean.class));
        }
        });
    }

    @Override
    public UserRegisterThreeContract.Model bindModel() {
        return UserRegisterThreeModel.newInstance();
    }
}
