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.PracticeCourseListBean;
import com.cooleshow.student.bean.QuerySubjectBean;
import com.cooleshow.student.contract.MinePracticeCourseContract;

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

import java.util.List;

/**
 * 创建日期：2022/5/30 10:33
 *
 * @author Ryan
 * 类说明：
 */
public class MinePracticeCoursePresenter extends BasePresenter<MinePracticeCourseContract.MinePracticeCourseView> implements MinePracticeCourseContract.Presenter {

    @Override
    public void queryPracticeCourse(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", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).queryStudentPracticeCourse(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<PracticeCourseListBean>(getView()) {
            @Override
            protected void onSuccess(PracticeCourseListBean data) {
                if (getView() != null) {
                    getView().onGetPracticeCourseSuccess(page, data);
                }
            }

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

}
