package com.cooleshow.teacher.presenter.income;

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.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.teacher.api.APIService;
import com.cooleshow.teacher.bean.AccountDetailBean;
import com.cooleshow.teacher.bean.UserAccountBean;
import com.cooleshow.teacher.contract.MineIncomeContract;

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

/**
 * 创建日期：2022/5/23 14:21
 *
 * @author Ryan
 * 类说明：
 */
public class MineIncomePresenter extends BasePresenter<MineIncomeContract.MineIncomeView> implements MineIncomeContract.Presenter {
    @Override
    public void getUserAccountDetail() {
        getView().showLoading();
        addSubscribe(create(APIService.class).getUserAccountDetail(), new BaseObserver<AccountDetailBean>(getView()) {
            @Override
            protected void onSuccess(AccountDetailBean data) {
                if (getView() != null) {
                    getView().userAccountDetailSuccess(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 userAccountPage(boolean isLoading,int page,String bizType,String searchDate){
        if (isLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bizType",bizType);
            jsonObject.put("searchDate",searchDate);
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).userAccountPage(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<UserAccountBean>(getView()) {
            @Override
            protected void onSuccess(UserAccountBean data) {
                if (getView() != null) {
                    getView().userAccountPageSuccess(page, data);
                }
            }

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

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


}
