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

import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
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.dip2px
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.common.view.divider.VerticalDividerItemDecoration
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.horizontal.GameHorizontalAdapter
import com.gh.gamecenter.game.horizontal.GameHorizontalListType
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailRecommendGameItemViewHolder(
    val binding: ItemGameDetailRecyclerViewBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkEveryonePlaying ?: 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

            val subjectEntity = entity.recommendedGames ?: return
            if (recyclerView.adapter !is GameHorizontalAdapter) {
                recyclerView.run {
                    isNestedScrollingEnabled = false
                    (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                    val exposureEventList = arrayListOf<ExposureEvent>()
                    val itemDecoration = VerticalDividerItemDecoration.Builder(context)
                        .size(8F.dip2px())
                        .color(com.gh.gamecenter.common.R.color.transparent.toColor(context))
                        .build()

                    for ((index, game) in subjectEntity.data!!.withIndex()) {
                        game.sequence = index
                        val event = ExposureEvent.createEvent(
                            gameEntity = game,
                            source = listOf(
                                ExposureSource("游戏详情", viewModel.game?.name ?: ""),
                                ExposureSource("大家都在玩", game.recommendType)
                            )
                        )
                        exposureEventList.add(event)
                        ExposureManager.log(event)
                    }

                    layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                    val subjectAdapter =
                        GameHorizontalAdapter(
                            context,
                            subjectEntity,
                            GameHorizontalListType.GameDetailHorizontalType,
                            false,
                            baseTrackData.apply { moduleName = "大家都在玩" }
                        ) { gameStatus }
                    subjectAdapter.gameName = viewModel.game?.name ?: ""
                    subjectAdapter.game = viewModel.game
                    subjectAdapter.gameId = viewModel.game?.id ?: ""
                    subjectAdapter.entrance = viewModel.entrance ?: ""
                    subjectAdapter.path = "大家都在玩"
                    subjectAdapter.exposureEventList = exposureEventList
                    (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                    addItemDecoration(itemDecoration)
                    adapter = subjectAdapter
                }
            } else {
                (recyclerView.adapter as GameHorizontalAdapter).run {
                    checkResetData(subjectEntity)
                }
            }
        }
    }
}