package com.gh.gamecenter.gamedetail.libao

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.LibaoEntity
import com.gh.gamecenter.gamedetail.detail.adapter.GameLibaoAdapter.LibaoViewHolder

class LibaoListAdapter(
    context: Context,
    private val viewModel: LibaoListViewModel,
    private val game: GameEntity?
) : ListAdapter<LibaoEntity>(context) {
    override fun getItemCount() =
        if (mEntityList.isNullOrEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT

    override fun getItemViewType(position: Int) =
        when (position) {
            itemCount - 1 -> ItemViewType.ITEM_FOOTER
            else -> ItemViewType.ITEM_BODY
        }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_BODY -> LibaoViewHolder(parent.toBinding())
            else -> FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is LibaoViewHolder -> {
                val entity = mEntityList[position]
                holder.bindItem(mContext, this, game?.id ?: "", game?.name ?: "", entity)
            }
            is FooterViewHolder -> holder.initFooterViewHolder(viewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }
}