package com.cooleshow.teacher.presenter.course;

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.PianoRoomCourseInfoBean;
import com.cooleshow.teacher.bean.request.AssignHomeWorkEntry;
import com.cooleshow.teacher.contract.PianoRoomCourseDetailContract;

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

/**
 * Author by pq, Date on 2022/4/27.
 */
public class PianoRoomDetailPresenter extends BasePresenter<PianoRoomCourseDetailContract.PianoRoomCourseDetailView> implements PianoRoomCourseDetailContract.Presenter {

    @Override
    public void getCourseInfo(String courseId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseId", courseId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getPianoRoomCourseHomework(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<PianoRoomCourseInfoBean>(getView()) {
            @Override
            protected void onSuccess(PianoRoomCourseInfoBean data) {
                if (getView() != null) {
                    getView().getCourseInfoSuccess(data);
                }
            }

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

    /**
     * 提交布置作业
     * @param commentContent
     * @param courseScheduleId
     */
    @Override
    public void submitSparringCourseHomework(String commentContent, String courseScheduleId) {
        if (getView() != null) {
            getView().showLoading();
        }
        AssignHomeWorkEntry entry = new AssignHomeWorkEntry();
        entry.courseScheduleId = courseScheduleId;
        entry.content = commentContent;
        entry.attachments = commentContent;
        addSubscribe(create(APIService.class).submitSparringCourseHomework(entry), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object o) {
                if (getView() != null) {
                    getView().submitSparringCourseHomeworkSuccess();
                }
            }

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