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

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.HomeSlideWithCardsCustomBinding
import com.gh.gamecenter.entity.FollowCommonContentCollection
import com.gh.gamecenter.entity.HomeSubSlide
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.CommonContentCollectionUseCase
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.ui.CommonContentHomeSlideWithCardsUi

/**
 * 关注-通用内容合集-轮播Banner
 */
class FollowHomeSlideWithCardsViewHolder(
    val binding: HomeSlideWithCardsCustomBinding,
    private val useCase: CommonContentCollectionUseCase,
    lifecycleOwner: LifecycleOwner,
    private val listener: OnChildEventListener
) : ViewHolder(binding.root) {
    private lateinit var data: FollowCommonContentCollection

    private val slideWithCardsUi by lazy {
        CommonContentHomeSlideWithCardsUi(
            useCase,
            lifecycleOwner,
            binding,
            0,
            null,
            "",
            object : CommonContentHomeSlideWithCardsUi.HomeSLideWithCardsEventListener {
                override fun updateImmersiveColor(color: Int) = Unit

                override fun navigateToGameDetailPage(
                    childPosition: Int,
                    gameEntity: GameEntity,
                    text: String,
                    link: LinkEntity?
                ) {
                    listener.navigateToGameDetailPage(childPosition, gameEntity, text)
                }

                override fun navigateToLinkPageInSubSlide(game: GameEntity, homeSubSlide: HomeSubSlide, text: String) {
                    listener.navigateToLinkPage(
                        bindingAdapterPosition,
                        homeSubSlide.toLinkEntity(),
                        data.linkCommonCollection,
                        text,
                        null
                    )
                }

                override fun navigateToLinkPage(link: LinkEntity, text: String, exposureEvent: ExposureEvent?) {
                    listener.navigateToLinkPage(
                        bindingAdapterPosition,
                        link,
                        data.linkCommonCollection,
                        text,
                        exposureEvent
                    )
                }

            })
    }

    fun bindView(data: FollowCommonContentCollection) {
        this.data = data
        slideWithCardsUi.bind(data.linkCommonCollection, bindingAdapterPosition + 1)
        slideWithCardsUi.updateWhenTopVisibleItemCountChanged(false)
        itemView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(itemView.context))
    }

    interface OnChildEventListener {

        fun navigateToGameDetailPage(childPosition: Int, gameEntity: GameEntity, text: String)

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