package com.cooleshow.teacher.presenter.income;

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.WithdrawalInfoBean;
import com.cooleshow.teacher.contract.WithdrawalContract;

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

/**
 * 创建日期：2022/5/23 17:30
 *
 * @author Ryan
 * 类说明：
 */
public class WithdrawalPresenter extends BasePresenter<WithdrawalContract.WithdrawalView> implements WithdrawalContract.Presenter {

    public void getWithdrawalInfo() {
        getView().showLoading();
        addSubscribe(create(APIService.class).getWithdrawalInfo(), new BaseObserver<WithdrawalInfoBean>(getView()) {
            @Override
            protected void onSuccess(WithdrawalInfoBean data) {
                if (getView() != null) {
                    getView().getWithdrawalInfoSuccess(data);
                }
            }

        });
    }

    public void userAccountPage(String amountWithdrawal, String bankCardId) {
        getView().showLoading();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("amountWithdrawal", amountWithdrawal);
            jsonObject.put("bankCardId", bankCardId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).doWithdrawal(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().doWithdrawalSuccess(data);
                }
            }

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

        });
    }

    /**
     * 检查是否签署过提现协议
     */
    public void checkContractSign() {
        if (getView() != null) {
            getView().showLoading();
        }
        String contractType = "WITHDRAW";
        addSubscribe(create(APIService.class).checkContractSign(contractType), new BaseObserver<Boolean>(getView()) {
            @Override
            protected void onSuccess(Boolean data) {
                if (getView() != null) {
                    getView().checkContractSignResult(data);
                }
            }

        });
    }

}
