package com.cooleshow.teacher.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.base.ui.video.VideoPlayActivity;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.LivePlayBackDataBean;
import com.cooleshow.teacher.widgets.VideoCourseListItemDecoration;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Size;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Author by pq, Date on 2022/7/7.
 */
public class LivePlaybackDialog extends Dialog {

    private Adapter mAdapter;

    public LivePlaybackDialog(@NonNull Context context) {
        super(context, com.cooleshow.base.R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_live_play_back_layout);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        VideoCourseListItemDecoration itemDecoration = new VideoCourseListItemDecoration(SizeUtils.dp2px(9), 0, 0, 0, SizeUtils.dp2px(7));
        recyclerView.addItemDecoration(itemDecoration);
        recyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new Adapter(R.layout.item_live_playback_layout);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position < mAdapter.getData().size()) {
                    LivePlayBackDataBean livePlayBackDataBean = mAdapter.getData().get(position);
                    VideoPlayActivity.start(getContext(), livePlayBackDataBean.url);
                }
            }
        });
    }

    public void setData(ArrayList<LivePlayBackDataBean> datas) {
        if (mAdapter != null) {
            mAdapter.setNewInstance(datas);
        }
    }

    private class Adapter extends BaseQuickAdapter<LivePlayBackDataBean, BaseViewHolder> {

        public Adapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, LivePlayBackDataBean livePlayBackDataBean) {
            ImageView iv_bg = holder.getView(R.id.iv_bg);
            GlideUtils.INSTANCE.loadVideoThumbnail(getContext(), livePlayBackDataBean.url, iv_bg);
            holder.setText(R.id.tv_time, UiUtils.getCourseTimeString(livePlayBackDataBean.startTime, livePlayBackDataBean.endTime));
        }
    }
}
