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.PianoRoomCourseListBean;
import com.cooleshow.student.contract.PianoRoomCourseContract;

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

/**
 * 琴房课presenter
 *
 * Author by pq, Date on 2022/4/20.
 */
public class PianoRoomCoursePresenter extends BasePresenter<PianoRoomCourseContract.PianoRoomCourseView> implements PianoRoomCourseContract.Presenter {

    @Override
    public void queryPianoRoomCourseList(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).getPianoRoomCourseList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<PianoRoomCourseListBean>(getView()) {
            @Override
            protected void onSuccess(PianoRoomCourseListBean data) {
                if (getView() != null) {
                    getView().onGetPianoRoomCourseListSuccess(page, data);
                }
            }

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