package com.gh.gamecenter.search

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.databinding.GameCollectionBannerItemBinding
import com.gh.gamecenter.feature.entity.FirstSetting

class SearchGameFirstBannerAdapter(
    private val bannerClick: (Int, FirstSetting.Banner) -> Unit
) :
    BannerController.BannerAdapter<FirstSetting.Banner, SearchGameFirstBannerAdapter.SearchGameBannerViewHolder>() {

    override val isBanner: Boolean
        get() = dataCount > 1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchGameBannerViewHolder {
        return SearchGameBannerViewHolder.create(parent)
    }

    override fun onBindViewHolder(holder: SearchGameBannerViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
        holder.binding.bannerIv.setOnClickListener {
            bannerClick(getDataPosition(position), item)
        }
    }

    class SearchGameBannerViewHolder(val binding: GameCollectionBannerItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: FirstSetting.Banner) {
            ImageUtils.display(binding.bannerIv, item.icon)
        }

        companion object {

            fun create(parent: ViewGroup): SearchGameBannerViewHolder {
                val inflater = LayoutInflater.from(parent.context)
                val binding = GameCollectionBannerItemBinding.inflate(inflater, parent, false)
                return SearchGameBannerViewHolder(binding)
            }
        }
    }

}