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

import com.gh.common.exposure.ExposureManager
import com.gh.common.exposure.ExposureTraceUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.databinding.CommonCollection12ItemCustomBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureType
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.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSplitCommonContentCollectionItem
import com.gh.gamecenter.home.custom.ui.CommonContentCollection12Ui

/**
 * 双列banner  双列竖式卡片 竖排图文列表
 * 通用链接合集(1-2样式)
 */
class CustomCommonCollection12ViewHolder(
    viewModel: CustomPageViewModel,
    val binding: CommonCollection12ItemCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root), IExposureListProvider {

    private var exposureEventList = arrayListOf<ExposureEvent>()

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

    private val collection12Ui by lazy {
        CommonContentCollection12Ui(binding, object : CommonContentCollection12Ui.OnCollection12Listener {
            override fun onChildItemClick(childPosition: Int, contentEntity: CommonCollectionContentEntity) {
                val item = _item as CustomSplitCommonContentCollectionItem
                val collection = item.data
                val linkEntity = contentEntity.linkEntity
                NewLogUtils.logCommonCollectionClick(
                    collection.id,
                    collection.name,
                    "blockData?.link",
                    "blockData?.name",
                    "板块内容列表",
                    "合集首页",
                    linkEntity.title ?: "",
                    contentEntity.addedContent1 ?: "",
                    contentEntity.addedContent2 ?: "",
                    linkEntity.type ?: "",
                    linkEntity.text ?: "",
                    childPosition + 1
                )
                NewLogUtils.logCommonCategoryHomeContentClick(
                    contentEntity.title,
                    linkEntity.type ?: "",
                    linkEntity.link ?: "",
                    linkEntity.text ?: "",
                    collection.name,
                    collection.id,
                    "板块",
                    "blockData?.name"
                )
                linkEntity.exposureEvent?.let {
                    val clickEvent = ExposureEvent(
                        payload = it.payload,
                        source = it.source,
                        eTrace = ExposureTraceUtils.appendTrace(it),
                        event = ExposureType.CLICK
                    )
                    if (linkEntity.type != "game") {
                        ExposureManager.log(clickEvent)
                    }
                }
                childEventHelper.navigateToLinkPage(
                    linkEntity,
                    "内容卡片",
                    linkEntity.exposureEvent
                )
            }

            override fun navigateToCommonCollectionDetailPage(collection: CustomPageData.CommonContentCollection) {
                childEventHelper.navigateToCommonCollectionDetailPage(_item.link)
            }
        })
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSplitCommonContentCollectionItem) {
            val data = item.data.data
            val leftPosition = item.leftPosition
            val firstExposureEvent = addExposureEvent(leftPosition, data[leftPosition])
            firstExposureEvent?.let {
                exposureEventList.add(0, firstExposureEvent)
            }

            val rightPosition = item.leftPosition + 1
            if (rightPosition < data.size) {
                val secondExposureEvent = addExposureEvent(rightPosition, data[rightPosition])
                secondExposureEvent?.let {
                    exposureEventList.add(1, secondExposureEvent)
                }
            }
            collection12Ui.bind(item.data, item.leftPosition)
        }
    }

    private fun addExposureEvent(childPosition: Int, contentEntity: CommonCollectionContentEntity): ExposureEvent? {
        (_item as? CustomSplitCommonContentCollectionItem)?.let { item ->
            val gameEntity = if (contentEntity.linkEntity.type == "game") {
                GameEntity(id = contentEntity.linkEntity.link, name = contentEntity.linkEntity.text)
            } else {
                GameEntity()
            }
            return createExposureEvent(
                gameEntity.also {
                    it.sequence = childPosition
                    it.outerSequence = item.position
                },
                listOf(
                    ExposureSource(
                        "通用内容合集",
                        "${item.data.name}+${item.data.layoutChinese}+${item.data.id}"
                    )
                ),
                pageConfigure.exposureSourceList,
                childPosition,
                item.componentPosition,
                createTrackData(item)
            ).also { contentEntity.linkEntity.exposureEvent = it }
        }
        return null
    }

    override fun provideExposureData(): ExposureEvent? = null
    override fun provideExposureDataList(): List<ExposureEvent>? {
        return exposureEventList
    }

}