package com.cooleshow.teacher.presenter.live;

import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.data.net.ApiException;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.teacher.api.APIService;
import com.cooleshow.teacher.bean.LiveListBean;
import com.cooleshow.teacher.bean.LivePlayBackDataBean;
import com.cooleshow.teacher.contract.LiveListContract;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Author by pq, Date on 2022/7/7.
 */
public class LiveListPresenter extends BasePresenter<LiveListContract.LiveListView> implements LiveListContract.Presenter {

    /**
     * 获取直播间列表数据
     *
     * @param liveState
     * @param page
     */
    public void getLiveListData(boolean isShowLoading, String liveState, int page) {
        if (isShowLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("liveState", liveState);
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("roomType", "TEMP");
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).getLiveListData(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<LiveListBean>(getView()) {
            @Override
            protected void onSuccess(LiveListBean data) {
                if (getView() != null) {
                    getView().getListDataSuccess(page, data);
                }
            }


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


    /**
     * 获取直播回放数据
     *
     * @param roomUid
     */
    public void getLivePlaybackData(String roomUid) {
        if (getView() != null) {
            getView().showLoading();
        }
        addSubscribe(create(APIService.class).getLivePlaybackData(roomUid), new BaseObserver<ArrayList<LivePlayBackDataBean>>(getView()) {
            @Override
            protected void onSuccess(ArrayList<LivePlayBackDataBean> data) {
                if (getView() != null) {
                    getView().getPlaybackDataSuccess(data);
                }
            }


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

}
