package com.gh.gamecenter.video.detail

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.feature.entity.SettingsEntity

class AdBannerAdapter(
    private var mDatas: ArrayList<SettingsEntity.Advertisement> = arrayListOf(),
    private var mOnItemClick: ((position: Int) -> Unit)
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return object :
            RecyclerView.ViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_ad_banner, parent, false)
            ) {}
    }

    override fun getItemCount(): Int = if (mDatas.size <= 1) mDatas.size else Int.MAX_VALUE

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val ad = mDatas[position % mDatas.size]
        val view = holder.itemView as SimpleDraweeView
        ImageUtils.display(view, ad.image)
        holder.itemView.setOnClickListener {
            mOnItemClick.invoke(position % mDatas.size)
        }
    }

}