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

import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.LeftPagerSnapHelper
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.HomeHorizontalSlideVideoListCustomBinding
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.IGameChangedNotifier
import com.gh.gamecenter.home.custom.adapter.CustomHomeHorizontalSlideVideoAdapter
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectItem
import com.gh.gamecenter.home.video.ScrollCalculatorHelper

/**
 * 游戏专题-视频横屏滑动
 * 游戏专题-视频横屏滑动-v2
 */
class CustomHomeHorizontalSlideVideoListViewHolder(
    viewModel: CustomPageViewModel,
    private val scrollCalculatorHelper: ScrollCalculatorHelper,
    val binding: HomeHorizontalSlideVideoListCustomBinding,
) :
    BaseCustomViewHolder(viewModel, binding.root) {

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

    private val adapter by lazy {
        CustomHomeHorizontalSlideVideoAdapter(
            itemView.context,
            pageConfigure,
            childEventHelper,
        )
    }

    override val gameChangedNotifier: IGameChangedNotifier
        get() = adapter

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSubjectItem) {
            item.exposureEventList.clear()
            setSubjectTitle(binding.layoutTitle, item, childEventHelper)

            if (binding.recyclerView.adapter == null) {
                binding.recyclerView.layoutManager =
                    FixLinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false)
                binding.recyclerView.adapter = adapter
                val snapHelper = LeftPagerSnapHelper()
                snapHelper.attachToRecyclerView(binding.recyclerView)
                binding.recyclerView.itemAnimator = null

                binding.recyclerView.clearOnScrollListeners()
                binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                        super.onScrolled(recyclerView, dx, dy)
                        scrollCalculatorHelper.onScroll()
                    }

                    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                        super.onScrollStateChanged(recyclerView, newState)
                        scrollCalculatorHelper.onScrollStateChanged(newState)
                    }
                })
            }

            runOnIoThread(isLightWeightTask = true) {
                fillExposureInfoAndReportAd(item)
            }

            adapter.setNewData(item)

            fun isParentScrolling() = scrollCalculatorHelper.mListRv.scrollState != RecyclerView.SCROLL_STATE_IDLE
            fun isSelfScrolling() = binding.recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE
            // 如果外部列表未发生滚动，但是又调用到了 bindView 方法，则说明是当前组件在刷新数据，需要重新检测是否需要播放视频
            if (!isParentScrolling()) {
                // 暂停上一次视频播放
                scrollCalculatorHelper.forceStop()
                // 在下一桢检测 视频播放
                itemView.postOnAnimation {
                    if (!isParentScrolling() && !isSelfScrolling()) {
                        scrollCalculatorHelper.onScrollStateChanged(RecyclerView.SCROLL_STATE_IDLE)
                    }
                }
            }
        }
    }

    private fun fillExposureInfoAndReportAd(item: CustomSubjectItem) {
        if (item.data.data == null) return

        for ((index, game) in item.data.data!!.withIndex()) {
            fillExposureAndReportAd(game, index, item)
        }
    }

}