package com.gh.gamecenter.forum.home.follow.viewholder

import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.common.exposure.ExposureManager
import com.gh.common.exposure.ExposureTraceUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.CommonCollectionListCustomBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.ExposureLinkEntity
import com.gh.gamecenter.entity.FollowCommonContentCollection
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureType
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.ui.CommonContentHorizontalSlideListUi

/**
 * 关注-通用内容合集-横排滑动Banner
 * 关注-通用内容合集-横排竖式卡片
 * 关注-通用内容合集-横排图文列表
 */
class FollowCommonCollectionViewHolder(
    val binding: CommonCollectionListCustomBinding,
    private val listener: OnChildEventListener
) : ViewHolder(binding.root) {

    private lateinit var _data: FollowCommonContentCollection

    private val horizontalSlideUi by lazy {
        CommonContentHorizontalSlideListUi(binding,
            object : CommonContentHorizontalSlideListUi.OnHorizontalSlideListListener {
                override fun addExposureEvent(childPosition: Int, link: ExposureLinkEntity) = Unit

                override fun onChildItemClick(childPosition: Int, entity: CommonCollectionContentEntity) {
                    val linkEntity = entity.linkEntity

                    NewLogUtils.logCommonCollectionClick(
                        _data.linkCommonCollection.id,
                        _data.linkCommonCollection.name,
                        "",
                        "",
                        "关注推荐列表",
                        "关注页",
                        linkEntity.title ?: "",
                        entity.addedContent1 ?: "",
                        entity.addedContent2 ?: "",
                        linkEntity.type ?: "",
                        linkEntity.text ?: "",
                        childPosition + 1
                    )
                    NewLogUtils.logCommonCategoryHomeContentClick(
                        entity.title,
                        linkEntity.type ?: "",
                        linkEntity.link ?: "",
                        linkEntity.text ?: "",
                        _data.linkCommonCollection.name,
                        _data.linkCommonCollection.id,
                        "",
                        ""
                    )
                    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)
                        }
                    }
                    listener.navigateToLinkPage(bindingAdapterPosition, linkEntity, _data.linkCommonCollection, "内容卡片", null)


                }

                override fun navigateToCommonCollectionDetailPage(data: CustomPageData.CommonContentCollection) {
                    listener.navigateToCommonCollectionDetailPage(data)
                }

            })
    }

    fun bindView(data: FollowCommonContentCollection) {
        _data = data
        horizontalSlideUi.bind(_data.linkCommonCollection)
        itemView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(itemView.context))
    }

    interface OnChildEventListener {

        fun navigateToLinkPage(
            position: Int,
            link: LinkEntity,
            data: CustomPageData.CommonContentCollection,
            text: String,
            exposureEvent: ExposureEvent?
        )

        fun navigateToCommonCollectionDetailPage(data: CustomPageData.CommonContentCollection)
    }
}