package com.gh.gamecenter.game.columncollection

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.SpacingItemDecoration
import com.gh.gamecenter.databinding.GameColumnCollectionListBinding
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.feature.entity.GameEntity

class GameColumnCollectionViewHolder(val binding: GameColumnCollectionListBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindColumnCollection(
        columnCollection: SubjectEntity,
        clickClosure: (position: Int, gameEntity: GameEntity) -> Unit
    ) {
        val context = binding.root.context
        val dataList = columnCollection.data!!

        binding.columnCollectionList.isNestedScrollingEnabled = false
        if (dataList.size == 1) {
            val data = dataList[0]
            binding.columnCollectionImage.visibility = View.VISIBLE
            binding.columnCollectionList.visibility = View.GONE
            ImageUtils.display(binding.columnCollectionImage, data.image)
        } else {
            binding.columnCollectionImage.visibility = View.GONE
            binding.columnCollectionList.visibility = View.VISIBLE
            var slideAdapter = binding.columnCollectionList.adapter
            if (slideAdapter == null) {
                slideAdapter = GameColumnCollectionAdapter(context, columnCollection, clickClosure)
                binding.columnCollectionList.layoutManager =
                    LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                binding.columnCollectionList.addItemDecoration(
                    SpacingItemDecoration(
                        notDecorateTheFirstItem = true,
                        left = 12F.dip2px()
                    )
                )
                binding.columnCollectionList.adapter = slideAdapter
            } else {
                (slideAdapter as GameColumnCollectionAdapter).checkResetData(columnCollection)
            }
        }
    }
}