package com.cooleshow.teacher.presenter.course;

import com.cooleshow.base.constanst.Constanst;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.teacher.api.APIService;
import com.cooleshow.teacher.bean.SparringCourseListBean;
import com.cooleshow.teacher.contract.SparringCourseContract;

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

/**
 * 陪练课presenter
 * Author by pq, Date on 2022/4/20.
 */
public class SparringCoursePresenter extends BasePresenter<SparringCourseContract.SparringCourseView> implements SparringCourseContract.Presenter {

    @Override
    public void querySparringCourse(boolean isShowLoading, String month, String status, int subjectId, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("classMonth", month);
            jsonObject.putOpt("status", status);
            if (subjectId != 0) {
                jsonObject.putOpt("subjectId", subjectId);
            }
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constanst.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getSparringCourses(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<SparringCourseListBean>(getView()) {
            @Override
            protected void onSuccess(SparringCourseListBean data) {
                if (getView() != null) {
                    getView().onGetSparringCourseSuccess(page, data);
                }
            }

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