package com.wd.workoffice.presenter;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.bean.ActivationUserBean;
import com.wd.workoffice.contract.BatSaleContract;
import com.wd.workoffice.contract.ErpActivationContract;
import com.wd.workoffice.model.ErpActivationModel;
import com.wd.workoffice.retrofit.WorkObserver;

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 ErpActivationPresenter  extends BasePresenter<ErpActivationContract.View, ErpActivationContract.Model>  implements ErpActivationContract.Presenter {

    @Override
    public void getInfo(String phone) {
        mView.showLoading();
        mModel.getInfo(phone).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.getInfo(JSON.parseObject(data.getData().toString(), ActivationUserBean.class));
            }
        });
    }

    @Override
    public void getCode(String phone, String type) {
        mView.showLoading();
        mModel.getCode(phone, type).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.getCode();
            }
        });
    }
    @Override
    public void verifyCode(String phone, String verifyCode, String type) {
        mView.showLoading();
        mModel.verifyCode(phone, verifyCode,type).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.verifySuccess();
            }
        });
    }
    @Override
    public ErpActivationContract.Model bindModel() {
        return ErpActivationModel.newInstance();
    }
}
