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.bean.ResponseBindCardBean;
import com.cooleshow.teacher.contract.InputBankVCodeContract;
import com.cooleshow.usercenter.bean.SetDetailBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * 创建日期：2022/5/19 14:12
 *
 * @author Ryan
 * 类说明：
 */
public class InputBankVCodePresenter extends BasePresenter<InputBankVCodeContract.InputBankVCodeView> implements InputBankVCodeContract.Presenter {
    public void sendSmsCode(Map<String,String> map){
        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();
                getView().hideLoading();
            }
            @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());
                }
            }
        });
    }

    public void verifySmsCode(String phone,String type,String code){
        getView().showLoading();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("phone", phone);
            jsonObject.putOpt("type", type);
            jsonObject.putOpt("code", code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).verifySmsCode(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().verifySmsCodeSuccess(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;
                    ToastUtil.getInstance().showShort(apiException.getErrmsg());
                }
            }
        });
    }

    public void bindBankCard(String jsonStr) {
        getView().showLoading();
        addSubscribe(create(APIService.class).bindBankCard(RequestBodyUtil.convertToRequestBodyJson(jsonStr)), new BaseObserver<ResponseBindCardBean>(getView()) {
            @Override
            protected void onSuccess(ResponseBindCardBean data) {
                if (getView() != null) {
                    getView().bindBankCardSuccess(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;
                    ToastUtil.getInstance().showShort(apiException.getErrmsg());
                }
            }
        });
    }
}
