package com.cooleshow.teacher.presenter.comment;

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.teacher.api.APIService;
import com.cooleshow.teacher.bean.CourseCommentListBean;
import com.cooleshow.teacher.contract.CourseCommentContract;

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

/**
 * Author by pq, Date on 2022/5/5.
 */
public class CourseCommentPresenter extends BasePresenter<CourseCommentContract.CourseCommentView> implements CourseCommentContract.Presenter {

    @Override
    public void queryCourseCommentList(boolean isShowLoading,int repliedStatus, String classMonth, String studentName, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("classMonth", classMonth);
            jsonObject.putOpt("replied", repliedStatus);
            jsonObject.putOpt("page", page);
            if (!TextUtils.isEmpty(studentName)) {
                jsonObject.putOpt("studentName", studentName);
            }
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getCourseCommentList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<CourseCommentListBean>(getView()) {
            @Override
            protected void onSuccess(CourseCommentListBean data) {
                if (getView() != null) {
                    getView().queryCourseCommentSuccess(page, data);
                }
            }

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