package com.gh.gamecenter.search

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.game.horizontal.GameHorizontalSimpleItemViewHolder
import com.lightgame.adapter.BaseRecyclerAdapter

class SearchSubjectAdapter(
    context: Context,
    private val mList: List<GameEntity>,
    private val mEntrance: String,
    private val clickCallback: (() -> Unit)
) : BaseRecyclerAdapter<GameHorizontalSimpleItemViewHolder>(context) {

    override fun getItemCount() = mList.size

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): GameHorizontalSimpleItemViewHolder {
        return GameHorizontalSimpleItemViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: GameHorizontalSimpleItemViewHolder, position: Int) {
        val gameEntity = mList[position]
        holder.binding.run {
            val padR = if (position == itemCount - 1) 16F.dip2px() else 0
            root.setPadding(16F.dip2px(), 0, padR, 0)
            gameIcon.displayGameIcon(gameEntity)
            GameHorizontalSimpleItemViewHolder.setHorizontalNameAndGravity(gameName, gameEntity.name)
            root.setOnClickListener {
                clickCallback.invoke()
                GameDetailActivity.startGameDetailActivity(mContext, gameEntity.id, mEntrance, gameEntity.exposureEvent)
            }
        }
    }
}