package com.cooleshow.usercenter.presenter;

import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.data.net.ApiException;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.usercenter.bean.UserLoginInfo;
import com.cooleshow.usercenter.constants.UserConstants;
import com.cooleshow.usercenter.data.api.UserApi;
import com.cooleshow.usercenter.presenter.contract.VerifyLoginContract;

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


/**
 * Author by pq, Date on 2022/4/19.
 */
public class VerifyLoginPresenter extends BasePresenter<VerifyLoginContract.VerifyLoginView> implements VerifyLoginContract.Presenter {

    @Override
    public void sendVerifyCode(String mobile) {
        if (getView() != null) {
            getView().showLoading();
        }
        if (BaseApplication.Companion.isTeacherClient()) {
            sendVerifyCodeForTeacher(mobile);
        } else {
            sendVerifyCodeForStu(mobile);
        }
    }


    /**
     * 学生端发送验证码
     *
     * @param mobile
     */
    private void sendVerifyCodeForStu(String mobile) {
        addSubscribe(create(UserApi.class).sendSmsCodeForStu(mobile, UserConstants.SEND_VERIFY_CODE_TYPE_LOGIN), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().sendVerifyCodeSuccess();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (e instanceof ApiException) {
                    ErrorParse.getInstance().parseError(e);
                    ApiException apiException = (ApiException) e;
                    ToastUtil.getInstance().showShort(apiException.getErrmsg());
                }
                if (getView() != null) {
                    getView().sendVerifyCodeFail();
                }
            }
        });
    }

    /**
     * 老师端发送验证码
     *
     * @param mobile
     */
    private void sendVerifyCodeForTeacher(String mobile) {
        addSubscribe(create(UserApi.class).sendSmsCode(mobile, UserConstants.SEND_VERIFY_CODE_TYPE_LOGIN), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().sendVerifyCodeSuccess();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (e instanceof ApiException) {
                    ErrorParse.getInstance().parseError(e);
                    ApiException apiException = (ApiException) e;
                    ToastUtil.getInstance().showShort(apiException.getErrmsg());
                }
                if (getView() != null) {
                    getView().sendVerifyCodeFail();
                }
            }
        });
    }

    public void checkVerifyCode(String mobile, String code) {
        if (getView() != null) {
            getView().showLoading();
        }
        addSubscribe(create(UserApi.class).checkVerifyCodeValidity(mobile, code), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
            }
        });
    }

    public void loginByVerifyCode(String mobile, String code) {
        if (getView() != null) {
            getView().showLoading();
        }
        Map<String, String> maps = new HashMap<>();
        maps.put("clientId", Constants.CLIENT);
        maps.put("clientSecret", Constants.CLIENT);
        maps.put("phone", mobile);
        maps.put("smsCode", code);
        maps.put("isSurportRegister", "true");
        addSubscribe(create(UserApi.class).loginByCode(maps), new BaseObserver<UserLoginInfo>(getView()) {
            @Override
            protected void onSuccess(UserLoginInfo data) {
                if (getView() != null) {
                    getView().loginByCodeSuccess(data);
                }
            }

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