package com.cooleshow.student.presenter.course;

import android.app.Activity;

import com.cooleshow.base.constanst.UploadConstants;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.base.utils.helper.upload.UploadHelper;
import com.cooleshow.student.api.APIService;
import com.cooleshow.student.bean.SparringCourseCommentBean;
import com.cooleshow.student.bean.SparringCourseHomeworkBean;
import com.cooleshow.student.contract.PracticeCourseDetailContract;

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

import java.io.File;

/**
 * 创建日期：2022/5/31 9:52
 *
 * @author Ryan
 * 类说明：
 */
public class PracticeCourseDetailPresenter extends BasePresenter<PracticeCourseDetailContract.PracticeCourseDetailView> implements PracticeCourseDetailContract.Presenter {
    public void getSparringCourseHomework(String courseId) {
        if (getView() != null) {
            getView().showLoading();
        }
        addSubscribe(create(APIService.class).getSparringCourseHomework(courseId), 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);

            }
        });
    }

    public void getSparringCourseComment(String courseScheduleId, String courseGroupId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseScheduleId", courseScheduleId);
            jsonObject.putOpt("courseGroupId", courseGroupId);
        } 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);

            }
        });
    }

    public void upLoadVideo(Activity activity, String filePath) {
        UploadHelper uploadHelper = new UploadHelper(activity, UploadConstants.UPLOAD_TYPE_HOMEWORK);
        uploadHelper.uploadFile(new File(filePath));
        uploadHelper.setUpLoadCallBack(new UploadHelper.UpLoadCallBack() {
            @Override
            public void onSuccess(String url) {
                if (getView() != null) {
                    getView().upLoadVideoSuccess(url);
                }
            }

            @Override
            public void onFailure() {

            }
        });
    }

    public void submitSparringCourseHomework(String commentContent, String courseScheduleId) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("courseScheduleId", courseScheduleId);
            jsonObject.putOpt("submit", commentContent);
        } catch (JSONException e) {
            e.printStackTrace();
        }

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

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