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.SparringCourseCommentBean;
import com.cooleshow.teacher.bean.SparringCourseHomeworkBean;
import com.cooleshow.teacher.bean.SparringCourseListBean;
import com.cooleshow.teacher.bean.request.AssignHomeWorkEntry;
import com.cooleshow.teacher.bean.request.CommentHomeWorkEntry;
import com.cooleshow.teacher.contract.SparringCourseDetailContract;

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

/**
 * Author by pq, Date on 2022/4/27.
 */
public class SparringCourseDetailPresenter extends BasePresenter<SparringCourseDetailContract.SparringCourseDetailView> implements SparringCourseDetailContract.Presenter {

    @Override
    public void getSparringCourseHomework(String courseId,String studentId) {
        if (getView() != null) {
            getView().showLoading();
        }
        addSubscribe(create(APIService.class).getSparringCourseHomework(courseId,studentId), new BaseObserver<SparringCourseHomeworkBean>(getView()) {
            @Override
            protected void onSuccess(SparringCourseHomeworkBean data) {
                if (getView() != null) {
                    getView().getSparringCourseHomeworkSuccess(data);
                }
            }

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

    @Override
    public void getSparringCourseComment(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);
            }
        });
    }

    @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);
            }
        });
    }

    @Override
    public void submitSparringCourseHomeworkComment(String commentContent, String courseScheduleId,String studentId ) {
        if (getView() != null) {
            getView().showLoading();
        }
        CommentHomeWorkEntry entry = new CommentHomeWorkEntry();
        entry.courseScheduleId = courseScheduleId;
        entry.review = commentContent;
        entry.studentId = studentId;

        addSubscribe(create(APIService.class).submitSparringCourseHomeworkComment(entry), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object o) {
                if (getView() != null) {
                    getView().submitSparringCourseHomeworkCommentSuccess();
                }
            }

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