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

import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.RecyclerNavigationCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.CustomPageViewModel
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.CustomPageItem
import com.gh.gamecenter.home.custom.ui.CommonContentNavigationUi

/**
 * 通用内容合集-导航栏
 */
class CustomNavigationViewHolder(
    viewModel: CustomPageViewModel,
    val binding: RecyclerNavigationCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

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

    private val navigationUi by lazy {
        CommonContentNavigationUi(binding, object : CommonContentNavigationUi.OnNavigationListener {
            override fun navigateToLinkPage(link: LinkEntity, text: String, exposureEvent: ExposureEvent?) {
                childEventHelper.navigateToLinkPage(link, text, exposureEvent)
            }
        })
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomCommonContentCollectionItem) {
            val navigationList = item.data.navigations

            if (!navigationList.isNullOrEmpty()) {
                val exposureEventList = arrayListOf<ExposureEvent>()
                runOnIoThread(true) {
                    navigationList.forEachIndexed { index, entity ->
                        val event = createExposureEvent(
                            GameEntity(
                                containerId = entity.id,
                                containerType = ExposureEntity.NAVIGATION_ID
                            ).also {
                                it.sequence = index
                                it.outerSequence = item.position
                            },
                            listOf(
                                ExposureSource(
                                    "通用内容合集",
                                    "${item.data.name}+${item.data.layoutChinese}+${item.data.id}"
                                ),
                                ExposureSource("导航栏", entity.entryName)
                            ),
                            pageConfigure.exposureSourceList,
                            index,
                            item.componentPosition,
                            createTrackData(item)
                        )
                        exposureEventList.add(event)
                        createTrackData(item)
                    }
                }
                navigationUi.bind(navigationList, exposureEventList)
            }
        }
    }
}