package com.gh.gamecenter.home.custom.viewholder

import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.databinding.HomeGameCollectionSlideItemCustomBinding
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.IGameChangedNotifier
import com.gh.gamecenter.home.custom.adapter.CustomHomeGameCollectionSlideAdapter
import com.gh.gamecenter.home.custom.eventlistener.GameSubjectCollectionEventHelper
import com.gh.gamecenter.home.custom.fillExposureInSubjectCollection
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem

/**
 * 专题合集:横排滑动大卡片/横排滑动小卡片
 * 游戏单合集:横排滑动大卡片/横排滑动小卡片
 */
class CustomHomeGameCollectionSlideViewHolder(
    private val isBigSlide: Boolean,
    viewModel: CustomPageViewModel,
    val binding: HomeGameCollectionSlideItemCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

    override val childEventHelper by lazy(LazyThreadSafetyMode.NONE) {
        GameSubjectCollectionEventHelper(viewModel)
    }

    private val _adapter by lazy {
        CustomHomeGameCollectionSlideAdapter(
            binding.root.context,
            isBigSlide,
            pageConfigure.entrance,
            childEventHelper
        )
    }

    override val gameChangedNotifier: IGameChangedNotifier?
        get() = if (isBigSlide) _adapter else null

    override fun bindView(item: CustomPageItem) {
        if (item is CustomSubjectCollectionItem) {
            super.bindView(item)

            fillExposureInSubjectCollection(item, pageConfigure.exposureSourceList, createTrackData(item))

            setSubjectCollectionTitle(item, binding.layoutTitle, childEventHelper)

            if (binding.recyclerView.adapter == null) {
                binding.recyclerView.layoutManager =
                    FixLinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false)
                binding.recyclerView.adapter = _adapter
            }
            _adapter.setData(item)
        }
    }

}