package com.cooleshow.teacher.presenter.mine;

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.RequestBodyUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.teacher.api.APIService;
import com.cooleshow.teacher.bean.ResponseBankCardBean;
import com.cooleshow.teacher.bean.ResponseBindCardBean;
import com.cooleshow.teacher.bean.request.UnBindCardEntry;
import com.cooleshow.teacher.contract.MyBankCardContract;

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

/**
 * 创建日期：2022/5/18 17:40
 *
 * @author Ryan
 * 类说明：
 */
public class MyBankCardPresenter extends BasePresenter<MyBankCardContract.MyBankCardView> implements MyBankCardContract.Presenter {
    public void queryUserBankCard(int page) {
        getView().showLoading();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).queryUserBankCard(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<ResponseBankCardBean>(getView()) {
            @Override
            protected void onSuccess(ResponseBankCardBean data) {
                if (getView() != null) {
                    getView().querySuccess(page, data);
                }
            }

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

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

    public void unBindBankCard(String id, String realName, String realIdCard) {
        UnBindCardEntry entry = new UnBindCardEntry();
        entry.name = realName;
        entry.id = id;
        entry.idCardNo = realIdCard;
        getView().showLoading();
        addSubscribe(create(APIService.class).unbindBankCard(entry), new BaseObserver<ResponseBindCardBean>(getView()) {
            @Override
            protected void onSuccess(ResponseBindCardBean data) {
                if (getView() != null) {
                    getView().unbindBankCard(true);
                }
            }

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

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (getView() != null) {
                    getView().unbindBankCardError(e);
                }
            }
        });
    }
}
