package com.cooleshow.student.presenter.course;

import android.text.TextUtils;

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.LiveCourseListBean;
import com.cooleshow.student.bean.QuerySubjectBean;
import com.cooleshow.student.contract.LiveCourseContract;
import com.cooleshow.usercenter.helper.UserHelper;


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

import java.util.List;

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

    @Override
    public void queryLiveCourse(boolean isShowLoading, String month, String status, int subjectId, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("classDate", month);
            if(!TextUtils.isEmpty(status)){
                jsonObject.putOpt("courseState", status);
            }
            if (subjectId != 0) {
                jsonObject.putOpt("subjectId", subjectId);
            }
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
            jsonObject.putOpt("studentId", UserHelper.getUserId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getLiveCourses(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<LiveCourseListBean>(getView()) {
            @Override
            protected void onSuccess(LiveCourseListBean data) {
                if (getView() != null) {
                    getView().onGetLiveCourseSuccess(page, data);
                }
            }

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