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.common.utils.goneIf
import com.gh.gamecenter.databinding.CommonCollectionListCustomBinding
import com.gh.gamecenter.databinding.LayoutTitleCustomBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.ExposureLinkEntity
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.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.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.ui.CommonContentHorizontalSlideListUi

/**
 * 通用内容合集-横排滑动Banner
 * 通用内容合集-横排竖式卡片
 * 通用内容合集-横排图文列表
 * 通用链接合集
 */
class CustomCommonCollectionViewHolder(
    viewModel: CustomPageViewModel,
    val binding: CommonCollectionListCustomBinding,
) : BaseCustomViewHolder(viewModel, binding.root) {

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

    private val horizontalSlideUi by lazy {
        CommonContentHorizontalSlideListUi(binding,
            object : CommonContentHorizontalSlideListUi.OnHorizontalSlideListListener {
                override fun addExposureEvent(childPosition: Int, link: ExposureLinkEntity) {
                    (_item as? CustomCommonContentCollectionItem)?.let { item ->
                        val gameEntity =
                            if (link.type == "game") GameEntity(id = link.link, name = link.text) else GameEntity()
                        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 { link.exposureEvent = it }
                    }
                }

                override fun onChildItemClick(childPosition: Int, entity: CommonCollectionContentEntity) {
                    (_item as? CustomCommonContentCollectionItem)?.let { item ->
                        val subject = item.data
                        val linkEntity = entity.linkEntity

                        NewLogUtils.logCommonCollectionClick(
                            subject.id,
                            subject.name,
                            "blockData?.link",
                            "blockData?.name",
                            "板块内容列表",
                            "合集首页",
                            linkEntity.title ?: "",
                            entity.addedContent1 ?: "",
                            entity.addedContent2 ?: "",
                            linkEntity.type ?: "",
                            linkEntity.text ?: "",
                            childPosition + 1
                        )
                        NewLogUtils.logCommonCategoryHomeContentClick(
                            entity.title,
                            linkEntity.type ?: "",
                            linkEntity.link ?: "",
                            linkEntity.text ?: "",
                            subject.name,
                            subject.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(data: CustomPageData.CommonContentCollection) {
                    childEventHelper.navigateToCommonCollectionDetailPage(_item.link)
                }
            })
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomCommonContentCollectionItem) {
            horizontalSlideUi.bind(item.data)
        }
    }

    companion object {
        fun setTitle(
            data: CustomPageData.CommonContentCollection,
            layoutTitle: LayoutTitleCustomBinding,
            trackInvoke: () -> Unit
        ) {
            with(layoutTitle) {
                lavRefresh.goneIf(true)
                gUser.goneIf(true)
                tvTitle.text = data.name

                if (data.topRightText.isEmpty()) {
                    tvRight.goneIf(true)
                } else {
                    tvRight.goneIf(false)
                    tvRight.text = data.topRightText
                    tvRight.setOnClickListener {
                        trackInvoke()
                    }
                }
            }
        }
    }

}