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

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.ItemHomeRecommendListCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureListProvider
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.CommonContentRecommendUi

/**
 * 通用内容合集-金刚区
 * 首页推荐入口
 */
class CustomHomeRecommendItemViewHolder(
    viewModel: CustomPageViewModel,
    val binding: ItemHomeRecommendListCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root), IExposureListProvider {

    private var exposureEventList: ArrayList<ExposureEvent>? = null

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

    private val recommendUi by lazy {
        CommonContentRecommendUi(binding, object : CommonContentRecommendUi.OnRecommendListener {
            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 recommends = item.data.recommends
            runOnIoThread(true) {
                exposureEventList = arrayListOf()
                item.data.recommends.forEachIndexed { index, homeRecommend ->
                    val exposureEvent = createExposureEvent(
                        GameEntity().also { it.sequence = index },
                        listOf(
                            ExposureSource(
                                "通用内容合集",
                                "${item.data.name}+${item.data.layoutChinese}+${item.data.id}"
                            ),
                            ExposureSource("金刚区", homeRecommend.name)
                        ),
                        pageConfigure.exposureSourceList,
                        index,
                        _item.componentPosition,
                        createTrackData(item)
                    )
                    exposureEvent.payload.controlType = "推荐入口"
                    exposureEvent.payload.controlName = homeRecommend.name
                    exposureEvent.payload.controlLinkName = homeRecommend.linkText
                    exposureEvent.payload.controlLinkType = homeRecommend.linkType
                    homeRecommend.exposureEvent = exposureEvent

                    exposureEventList?.add(exposureEvent)
                }
            }
            recommendUi.bind(recommends, exposureEventList)
        }
    }

    override fun provideExposureData(): ExposureEvent? = null

    override fun provideExposureDataList(): List<ExposureEvent>? {
        exposureEventList?.forEach {
            it.getFreshExposureEvent()
        }
        return exposureEventList
    }

    companion object {
        //图标最多列数
        const val MAX_SPAN_COUNT = 5
    }
}