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.ReceivedCommentListBean;
import com.cooleshow.teacher.contract.ReceivedCommentContract;

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

/**
 * Author by pq, Date on 2022/5/5.
 */
public class ReceivedCommentPresenter extends BasePresenter<ReceivedCommentContract.ReceivedCommentView> implements ReceivedCommentContract.Presenter {

    @Override
    public void queryReceivedComment(boolean isShowLoading, String month, String studentName, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("classDate", month);
            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).getReceivedComment(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<ReceivedCommentListBean>(getView()) {
            @Override
            protected void onSuccess(ReceivedCommentListBean data) {
                if (getView() != null) {
                    getView().queryCommentSuccess(page, data);
                }
            }

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