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.bean.ResponseUserBankCardBean;
import com.cooleshow.teacher.contract.BindBankCardContract;

import java.util.List;

/**
 * 创建日期：2022/5/19 9:27
 *
 * @author Ryan
 * 类说明：
 */
public class BindBankCardPresenter extends BasePresenter<BindBankCardContract.BindBankCardView> implements BindBankCardContract.Presenter {
    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());
                }
            }
        });
    }

    public void userBankCard() {
        getView().showLoading();
        addSubscribe(create(APIService.class).userBankCard(), new BaseObserver<List<ResponseUserBankCardBean>>(getView()) {
            @Override
            protected void onSuccess(List<ResponseUserBankCardBean> data) {
                if (getView() != null) {
                    getView().userBankCardSuccess(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());
                }
            }
        });
    }
}
