package com.gh.gamecenter.gamedetail.detail.viewholder

import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.ExposureManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.setDrawableEnd
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.ItemGameDetailRecyclerViewBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.game.vertical.SpanCountPagerSnapHelper
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.detail.adapter.GameDetailRelatedGameAdapter
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailRelatedGameItemViewHolder(
    val binding: ItemGameDetailRecyclerViewBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val gameList = data.linkRelatedGame ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            moreTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            moreTv.setDrawableEnd(R.drawable.ic_auxiliary_arrow_right_text_theme_12.toDrawable(context))
            titleTv.text = "相关游戏"
            moreTv.isVisible = false

            if (recyclerView.adapter !is GameDetailRelatedGameAdapter) {
                val gameDetailRelatedGameAdapter = GameDetailRelatedGameAdapter(context, viewModel.game, gameList, viewModel.entrance ?: "", baseTrackData) { gameStatus }
                recyclerView.run {
                    isNestedScrollingEnabled = false
                    (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                    if (gameList.size > 3) {
                        layoutManager = GridLayoutManager(context, 3, RecyclerView.HORIZONTAL, false)
                        clearOnScrollListeners()
                        (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                        onFlingListener = null
                        val snapHelper = SpanCountPagerSnapHelper(3, true)
                        snapHelper.attachToRecyclerView(this)
                    } else {
                        layoutManager = LinearLayoutManager(context)
                    }
                    adapter = gameDetailRelatedGameAdapter
                }
                // 曝光相关
                for ((index, game) in gameList.withIndex()) {
                    game.sequence = index
                    val event = ExposureEvent.createEvent(
                        gameEntity = game,
                        source = listOf(
                            ExposureSource("游戏详情", viewModel.game?.name ?: ""),
                            ExposureSource("相关游戏")
                        )
                    )
                    gameDetailRelatedGameAdapter.exposureEventList.add(event)
                    ExposureManager.log(event)
                }
            } else {
                recyclerView.adapter?.run {
                    notifyItemRangeChanged(0, itemCount)
                }
            }
        }
    }
}