package com.cooleshow.teacher.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.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.teacher.api.APIService;
import com.cooleshow.teacher.contract.ModifyPhoneNumContract;
import com.cooleshow.usercenter.bean.SetDetailBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建日期：2022/5/17 10:56
 *
 * @author Ryan
 * 类说明：
 */
public class ModifyPhoneNumPresenter extends BasePresenter<ModifyPhoneNumContract.ModifyPhoneNumView> implements ModifyPhoneNumContract.Presenter {
    public void updatePhone(String authCode,String newPhone, String oldPhone) {
        getView().showLoading();
        addSubscribe(create(APIService.class).updatePhone(authCode, newPhone,oldPhone), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().updatePhoneSuccess(data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }


    /**
     * 图片验证码验证
     *
     * @param phone
     * @param imgCode
     */
    public void verifyImgCode(String phone, String imgCode) {
        if (getView() != null) {
            getView().showLoading();
        }
        Map<String, String> params = new HashMap<>();
        params.put("code", imgCode);
        params.put("phone", phone);
        addSubscribe(create(APIService.class).verifyImgCode(params), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().verifyImgCodeSuccess(phone);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                if (getView() != null) {
                    getView().hideLoading();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }


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

            @Override
            public void onComplete() {
                super.onComplete();
                if (getView() != null) {
                    getView().hideLoading();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }
}
