package com.cooleshow.student.presenter.homework;

import android.app.Activity;

import com.cooleshow.base.constanst.Constants;
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.HomeworkDetailBean;
import com.cooleshow.student.bean.HomeworkListBean;
import com.cooleshow.student.contract.HomeWorkActContract;
import com.cooleshow.student.contract.HomeWorkDetailContract;

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

import java.io.File;


/**
 * 创建日期：2022/5/23 10:49
 *
 * @author Ryan
 * 类说明：
 */
public class HomeWorkADetailPresenter extends BasePresenter<HomeWorkDetailContract.HomeWorkDetailView> implements HomeWorkDetailContract.Presenter {

    /**
     * 查询课后作业详情(陪练课和琴房课通用)
     *
     * @param courseId
     */
    public void queryHomeworkDetail(String courseId) {
        if (getView() != null) {
            getView().showLoading();
        }
        addSubscribe(create(APIService.class).queryHomeworkDetail(courseId), new BaseObserver<HomeworkDetailBean>(getView()) {
            @Override
            protected void onSuccess(HomeworkDetailBean data) {
                if (getView() != null) {
                    getView().getDetailInfoSuccess(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() {

            }
        });
    }


    /**
     * 提交作业
     * @param commentContent
     * @param courseScheduleId
     */
    public void submitCourseHomework(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().submitCourseHomeworkSuccess();
                }
            }

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