package com.cooleshow.student.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.student.api.APIService;
import com.cooleshow.student.bean.QuerySubjectBean;
import com.cooleshow.student.bean.SelectMyGroupBean;
import com.cooleshow.student.bean.VideoCourseListBean;
import com.cooleshow.student.contract.VideoCourseContract;

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

import java.util.List;

/**
 * 陪练课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, String studentId,int subjectId, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            if (subjectId != 0) {
                jsonObject.putOpt("subjectId", subjectId);
            }
            jsonObject.putOpt("platform", "android");
            jsonObject.putOpt("version", "1");
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).selectMyGroup(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<SelectMyGroupBean>(getView()) {
            @Override
            protected void onSuccess(SelectMyGroupBean data) {
                if (getView() != null) {
                    getView().onGetVideoCourseSuccess(page, data);
                }
            }

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