package com.cooleshow.teacher.presenter.homework;

import com.cooleshow.base.constanst.Constants;
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.HomeworkListBean;
import com.cooleshow.teacher.bean.MusicSheetListBean;
import com.cooleshow.teacher.contract.HomeWorkContract;

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

/**
 * 创建日期：2022/5/23 10:59
 *
 * @author Ryan
 * 类说明：
 */
public class HomeWorkPresenter extends BasePresenter<HomeWorkContract.HomeWorkView> implements HomeWorkContract.Presenter {
    public void queryHomeworkList(boolean isLoading,int page,String decorate,String date){
        if (isLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("decorate",decorate);
            jsonObject.put("date",date);
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).queryHomeworkList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<HomeworkListBean>(getView()) {
            @Override
            protected void onSuccess(HomeworkListBean data) {
                if (getView() != null) {
                    getView().queryHomeworkListSuccess(page, data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }

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


}
