package com.gh.gamecenter.discovery.interestedgame

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.databinding.InterestedGameTypeItemBinding
import com.lightgame.adapter.BaseRecyclerAdapter

class InterestedGameTypeAdapter(
    context: Context,
    private val mViewModel: InterestedGameViewModel,
    private val mTagClickCallback: EmptyCallback
) : BaseRecyclerAdapter<InterestedGameTypeAdapter.InterestedGameTypeViewHolder>(context) {

    override fun getItemCount() = mViewModel.getTypeTags().size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        InterestedGameTypeViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: InterestedGameTypeViewHolder, position: Int) {
        holder.binding.run {
            val padTop = if (position == 0) 16F.dip2px() else 0F.dip2px()
            root.setPadding(16F.dip2px(), padTop, 16F.dip2px(), 16F.dip2px())
            val entity = mViewModel.getTypeTags()[position]
            typeTv.text = entity.name
            subTagRv.run {
                if (adapter is InterestGameSubTagAdapter) {
                    (adapter as InterestGameSubTagAdapter).checkResetData(entity.tags)
                } else {
                    layoutManager = GridLayoutManager(mContext, 4)
                    adapter = InterestGameSubTagAdapter(mContext, entity.tags, mViewModel, mTagClickCallback)
                    addItemDecoration(GridSpacingItemColorDecoration(mContext, 8, 8, R.color.transparent))
                }
            }
        }
    }

    inner class InterestedGameTypeViewHolder(val binding: InterestedGameTypeItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}