package com.wisewoods.eduandroid.ui;

import static com.tencent.liteav.meeting.ui.CreateMeetingActivity.VIDEO_QUALITY_HD;
import static com.tencent.trtc.TRTCCloudDef.TRTC_AUDIO_QUALITY_SPEECH;

import android.text.TextUtils;

import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tencent.liteav.live.ui.MeetingMainActivity;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.bean.ScheduleInfo;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.page.BaseListActivity;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityLiveListBinding;
import com.wisewoods.eduandroid.ui.adapter.LiveListAdapter;

import retrofit2.Call;
import retrofit2.Response;

public class LiveListActivity extends BaseListActivity<ActivityLiveListBinding> {

    private LiveListAdapter liveListAdapter;

    @Override
    protected void initListData() {
        srf = binding.srf;
        rv = binding.rvLiveList;

        showBarBack(binding.bar.tvTitle, "直播列表", binding.bar.ivBack);

        binding.rvLiveList.setLayoutManager(new GridLayoutManager(this, 2));
        liveListAdapter = new LiveListAdapter();
        liveListAdapter.setOnItemClickListener((adapter, view, position) -> {
            ScheduleInfo.DataBean item = liveListAdapter.getItem(position);

            if (item.liveStatus == 1) {
                String userId = UserManager.getWorkid();
                String userAvatar = UserManager.getAvatar();
                String userName = TextUtils.isEmpty(UserManager.getUser().nickName) ? UserManager.getUser().userName
                        : UserManager.getUser().nickName;
                MeetingMainActivity.enterRoom(activity,
                        item.scheduleIdStr,
                        userId,
                        userName,
                        userAvatar,
                        false,
                        false,
                        TRTC_AUDIO_QUALITY_SPEECH,
                        VIDEO_QUALITY_HD,
                        true,
                        true
                );
            }

        });
        binding.rvLiveList.setAdapter(liveListAdapter);
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return liveListAdapter;
    }

    @Override
    protected void getData(long page, long size) {
        App.api.selectLiveCourseList(page, size)
                .enqueue(new JCallback<ScheduleInfo>() {
                    @Override
                    public void onS(Response<ScheduleInfo> response, ScheduleInfo scheduleInfo) {
                        getDataSuccess(scheduleInfo.data.rows, scheduleInfo.data.total);
                    }

                    @Override
                    public void onFailure(Call<ScheduleInfo> call, Throwable t) {
                        super.onFailure(call, t);
                        getDataFail();
                    }
                });
    }
}
