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

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.RecyclerAnnouncementBannerBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.home.custom.BannerInRecyclerController
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.AnnouncementBannerAdapter
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

/**
 * 通用内容合集-公告横幅
 */
class CustomAnnouncementBannerViewHolder(
    viewModel: CustomPageViewModel,
    lifecycleOwner: LifecycleOwner,
    val binding: RecyclerAnnouncementBannerBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

    private val adapter by lazy {
        AnnouncementBannerAdapter(itemView.context, object : AnnouncementBannerAdapter.OnChildEventListener {
            override fun onItemClick(childPosition: Int, announcement: CustomPageData.Announcement) {
                childEventHelper.navigateToLinkPage(
                    announcement.link,
                    "公告横幅",
                    announcement.exposureEvent
                )
            }

            override fun getCurrentPosition(): Int {
                return binding.vpBanner.currentItem
            }
        })
    }

    private val bannerInRecyclerController = BannerInRecyclerController {
        if (adapter.isBanner) {
            binding.vpBanner.setCurrentItem(binding.vpBanner.currentItem + 1, true)
        }
    }

    override val childEventHelper by lazy {
        CommonContentCollectionEventHelper(viewModel)
    }

    init {
        lifecycleOwner.lifecycle.addObserver(bannerInRecyclerController)
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomCommonContentCollectionItem) {
            if (binding.vpBanner.adapter == null) {
                binding.vpBanner.adapter = adapter
                binding.vpBanner.registerOnPageChangeCallback(object : OnPageChangeCallback() {
                    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                        binding.bannerIndicator.onPageScrolled(adapter.getDataPosition(position), positionOffset)
                    }

                    override fun onPageSelected(position: Int) {
                        // 页面切换，重置轮播等待时间
                        (_item as CustomCommonContentCollectionItem).selectedPosition = position
                        bannerInRecyclerController.start()
                    }
                })
            }

            runOnIoThread(isLightWeightTask = true) {
                fillExposureInfo(item.data.announcements)
            }

            adapter.submitList(item.data.announcements)
            if (item.selectedPosition != 0) {
                binding.vpBanner.setCurrentItem(item.selectedPosition, false)
            }
            with(binding.bannerIndicator) {
                pageSize = adapter.dataList.size
                notifyDataChanged()
            }
        }
    }

    private fun fillExposureInfo(dataList: List<CustomPageData.Announcement>) {
        for ((index, announcement) in dataList.withIndex()) {
            val item = _item as CustomCommonContentCollectionItem
            val gameEntity = if (announcement.linkType == "game") {
                GameEntity(id = announcement.linkId, name = announcement.linkText)
            } else {
                GameEntity()
            }
            announcement.exposureEvent = createExposureEvent(
                gameEntity.also {
                    it.sequence = index
                    it.outerSequence = _item.position
                },
                listOf(
                    ExposureSource("通用内容合集", "${item.data.name}+${item.data.layoutChinese}+${item.data.id}"),
                    ExposureSource(item.data.layoutChinese)
                ),
                pageConfigure.exposureSourceList,
                index,
                _item.componentPosition,
                createTrackData(item)
            )
        }
    }

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

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