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

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
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.HomeSlideListCustomBinding
import com.gh.gamecenter.entity.FollowCommonContentCollection
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.OnViewHolderAttachListener
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.ui.CommonContentHomeSLideListUi

/**
 * 关注-通用内容合集-轮播Banner
 */
class FollowHomeSlideListViewHolder(
    val binding: HomeSlideListCustomBinding,
    lifecycleOwner: LifecycleOwner,
    listener: OnChildEventListener,
) :
    ViewHolder(binding.root), OnViewHolderAttachListener {

    private lateinit var data: FollowCommonContentCollection

    private val slideListUi by lazy {
        CommonContentHomeSLideListUi(
            binding,
            lifecycleOwner,
            object : CommonContentHomeSLideListUi.OnCommonHomeSlideListEventListener {
                override fun navigateToLinkPage(link: LinkEntity, text: String, exposureEvent: ExposureEvent?) {
                    listener.navigateToLinkPage(
                        bindingAdapterPosition,
                        link,
                        data.linkCommonCollection,
                        text,
                        exposureEvent
                    )
                }

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

                override fun updateImmersiveColor(color: Int) = Unit
            })
    }

    fun bindView(data: FollowCommonContentCollection) {
        this.data = data

        val slideList = data.linkCommonCollection.slides.slide

        slideListUi.bind(slideList, bindingAdapterPosition + 1)
        slideListUi.updateWhenTopVisibleItemCountChanged(false)

        itemView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(itemView.context))

    }

    override fun onViewAttach(parent: RecyclerView?) {
        slideListUi.onViewAttach(parent)
    }

    override fun onViewDetach(parent: RecyclerView?) {
        slideListUi.onViewDetach(parent)
    }

    interface OnChildEventListener {

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

        fun navigateToGameDetailPage(position: Int, game: GameEntity, text: String)
    }
}