package com.cooleshow.student.presenter.mine;

import com.cooleshow.base.data.net.ApiException;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.student.api.APIService;
import com.cooleshow.student.contract.ModifyPasswordContract;
import com.cooleshow.usercenter.bean.SetDetailBean;

import java.util.Map;

/**
 * 创建日期：2022/5/17 13:36
 *
 * @author Ryan
 * 类说明：
 */
public class ModifyPasswordPresenter extends BasePresenter<ModifyPasswordContract.ModifyPasswordView> implements ModifyPasswordContract.Presenter {
    public void updatePassword(Map<String, String> requestData) {
        getView().showLoading();
        addSubscribe(create(APIService.class).updatePassword(requestData), new BaseObserver<SetDetailBean>(getView()) {
            @Override
            protected void onSuccess(SetDetailBean data) {
                if (getView() != null) {
                    getView().updatePasswordSuccess(data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (e instanceof ApiException) {
                    ErrorParse.getInstance().parseError(e);
                    ApiException apiException = (ApiException) e;
                    ToastUtils.showShort(apiException.getErrmsg());
                }
            }
        });

    }
    public void sendSmsCode(String jsonStr){
        getView().showLoading();
        addSubscribe(create(APIService.class).sendSmsCode(RequestBodyUtil.convertToRequestBodyJson(jsonStr)), new BaseObserver<SetDetailBean>(getView()) {
            @Override
            protected void onSuccess(SetDetailBean data) {
                if (getView() != null) {
                    getView().sendSmsCodeSuccess(data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (e instanceof ApiException) {
                    ErrorParse.getInstance().parseError(e);
                    ApiException apiException = (ApiException) e;
                    ToastUtils.showShort(apiException.getErrmsg());
                }
            }
        });
    }
}
