package com.cooleshow.usercenter.presenter;

import com.cooleshow.base.constanst.Constanst;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.usercenter.bean.UserLoginInfo;
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();
        }
        addSubscribe(create(UserApi.class).sendSmsCode(mobile), 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 (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", Constanst.CLIENT);
        maps.put("clientSecret", Constanst.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);
                }
            }
        });
    }
}
