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

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.RecyclerContentLabelLaneCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.ContentLabelLaneAdapter
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.CommonContentCollectionEventHelper
import com.gh.gamecenter.home.custom.model.CustomCommonContentCollectionItem
import com.gh.gamecenter.home.custom.model.CustomPageData.CommonContentCollection.ContentTag
import com.gh.gamecenter.home.custom.model.CustomPageItem

/**
 * 通用内容合集-内容标签泳道
 */
class ContentLabelLaneViewHolder(
    viewModel: CustomPageViewModel,
    private val binding: RecyclerContentLabelLaneCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

    override val childEventHelper by lazy {
        CommonContentCollectionEventHelper(viewModel)
    }

    private val adapter by lazy {
        ContentLabelLaneAdapter(itemView.context) { index, contentTag ->
            childEventHelper.navigateToLinkPage(
                contentTag.link,
                "内容标签泳道",
                contentTag.exposureEvent
            )
        }
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomCommonContentCollectionItem) {
            val contentTags = item.data.contentTags
            if (binding.rvGames.adapter == null) {
                binding.rvGames.layoutManager = LinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false)
                binding.rvGames.adapter = adapter
            }

            runOnIoThread(isLightWeightTask = true) {
                fillExposureInfo(item.data.contentTags)
            }
            adapter.submitList(contentTags)
        }
    }

    private fun fillExposureInfo(contentTags: List<ContentTag>) {
        for ((index, contentTag) in contentTags.withIndex()) {
            val item = _item as CustomCommonContentCollectionItem
            val gameEntity = if (contentTag.linkType == "game") {
                GameEntity(id = contentTag.linkId, name = contentTag.linkText)
            } else {
                GameEntity()
            }
            contentTag.exposureEvent = createExposureEvent(
                gameEntity.also {
                    it.sequence = index
                    it.outerSequence = _item.position
                },
                listOf(
                    ExposureSource(
                        "通用内容合集",
                        "${item.data.name}+${item.data.layoutChinese}+${item.data.id}"
                    ),
                    ExposureSource(
                        item.data.layoutChinese,
                        "${contentTag.title}+${contentTag.id}"
                    )
                ),
                pageConfigure.exposureSourceList,
                index,
                _item.componentPosition,
                createTrackData(item)
            )
        }
    }
}