package com.cooleshow.teacher.presenter.course;

import com.cooleshow.base.constanst.Constants;
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.VideoCourseListBean;
import com.cooleshow.teacher.contract.VideoCourseContract;

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

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

    @Override
    public void queryVideoCourse(boolean isShowLoading, int subjectId, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            if (subjectId != 0) {
                jsonObject.putOpt("subjectId", subjectId);
            }
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
            jsonObject.putOpt("auditStatus", "PASS");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getVideoCourses(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<VideoCourseListBean>(getView()) {
            @Override
            protected void onSuccess(VideoCourseListBean data) {
                if (getView() != null) {
                    getView().onGetVideoCourseSuccess(page, data);
                }
            }

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