package com.cooleshow.student.presenter.main;

import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.base.utils.TimeUtils;
import com.cooleshow.student.api.APIService;
import com.cooleshow.student.bean.CourseTableDataBean;
import com.cooleshow.student.constants.CourseTableContract;


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

import java.util.Date;
import java.util.List;

/**
 * Author by pq, Date on 2022/4/21.
 */
public class CourseTablePresenter extends BasePresenter<CourseTableContract.CourseTableView> implements CourseTableContract.Presenter {
    /**
     * 查询当月有课的日期
     *
     * @param month
     */
    @Override
    public void getCourseScheduleDateByMonth(String month) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("classMonth", month);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getCourseScheduleDateByMonth(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<String>>(getView()) {
            @Override
            protected void onSuccess(List<String> data) {
                if (getView() != null) {
                    getView().onGetCourseDateByMonthSuccess(data);
                }
            }

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

    /**
     * 查询当天的课程
     *
     * @param date
     */
    @Override
    public void getCourseSchedulesWithDate(Date date) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("classDate", TimeUtils.date2String(date, TimeUtils.getSafeDateFormat("yyyy-MM-dd")));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getCourseSchedulesWithDate(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<CourseTableDataBean>(getView()) {
            @Override
            protected void onSuccess(CourseTableDataBean data) {
                if (getView() != null) {
                    getView().onGetCourseSchedulesWithDateSuccess(data);
                }
            }
        });
    }
}
