package com.cooleshow.teacher.presenter.comment;

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.SparringCourseCommentBean;
import com.cooleshow.teacher.contract.CourseCommentDetailContract;

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

/**
 * Author by pq, Date on 2022/5/5.
 */
public class CourseCommentDetailPresenter extends BasePresenter<CourseCommentDetailContract.CourseCommentDetailView> implements CourseCommentDetailContract.Presenter {
    @Override
    public void queryCourseCommentDetail(String courseScheduleId, String courseGroupId, String studentId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseScheduleId", courseScheduleId);
            jsonObject.putOpt("courseGroupId", courseGroupId);
            jsonObject.putOpt("studentId", studentId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        addSubscribe(create(APIService.class).getSparringCourseComment(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<SparringCourseCommentBean>(getView()) {
            @Override
            protected void onSuccess(SparringCourseCommentBean data) {
                if (getView() != null) {
                    getView().getSparringCourseCommentSuccess(data);
                }
            }

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

    /**
     * 提交陪练课老师评价
     *
     * @param commentContent
     * @param courseScheduleId
     * @param courseGroupId
     * @param studentId
     */
    @Override
    public void submitSparringCourseComment(String commentContent, String courseScheduleId, String courseGroupId, String studentId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseScheduleId", courseScheduleId);
            jsonObject.putOpt("teacherReplied", commentContent);
            jsonObject.putOpt("courseGroupId", courseGroupId);
            jsonObject.putOpt("studentId", studentId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        addSubscribe(create(APIService.class).submitSparringCourseComment(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object o) {
                if (getView() != null) {
                    getView().submitSparringCourseTeacherCommentSuccess();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

}
