package com.cooleshow.teacher.presenter.homework;

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.PianoRoomCourseHomeworkBean;
import com.cooleshow.teacher.bean.PianoRoomCourseInfoBean;
import com.cooleshow.teacher.bean.SparringCourseHomeworkBean;
import com.cooleshow.teacher.contract.PianoRoomCourseHomeworkContract;

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

/**
 * Author by pq, Date on 2022/6/24.
 */
public class PianoRoomCourseHomeWorkPresenter extends BasePresenter<PianoRoomCourseHomeworkContract.PianoRoomCourseHomeworkView> implements PianoRoomCourseHomeworkContract.Presenter {

    /**
     * 获取琴房课作业详情信息
     *
     * @param courseId
     * @param studentId
     */
    @Override
    public void getCourseHomeworkInfo(String courseId, String studentId) {
        if (getView() != null) {
            getView().showLoading();
        }
        addSubscribe(create(APIService.class).getPianoRoomCourseHomework(courseId, studentId), new BaseObserver<PianoRoomCourseHomeworkBean>(getView()) {
            @Override
            protected void onSuccess(PianoRoomCourseHomeworkBean data) {
                if (getView() != null) {
                    getView().getCourseHomeworkInfoSuccess(data);
                }
            }

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

    /**
     * 提交老师布置的作业
     *
     * @param commentContent
     * @param courseScheduleId
     */
    @Override
    public void submitCourseHomework(String commentContent, String courseScheduleId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseScheduleId", courseScheduleId);
            jsonObject.putOpt("content", commentContent);
            jsonObject.putOpt("attachments", commentContent);
        } catch (JSONException e) {
            e.printStackTrace();
        }

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

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

    /**
     * 提交作业评价
     *
     * @param commentContent
     * @param courseScheduleId
     */
    @Override
    public void submitCourseHomeworkComment(String commentContent, String courseScheduleId,String studentId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseScheduleId", courseScheduleId);
            jsonObject.putOpt("review", commentContent);
            jsonObject.putOpt("studentId", studentId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

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

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