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

import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.RecyclerHorizontalSlideListCustomBinding
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.IGameChangedNotifier
import com.gh.gamecenter.home.custom.adapter.CustomGameHorizontalSlideAdapter
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectItem

/**
 * 游戏专题-图标泳道
 * 游戏专题-横向时间轴
 * 游戏专题-横屏滑动-v2
 */
class CustomGameHorizontalSlideListViewHolder(
    viewModel: CustomPageViewModel,
    val binding: RecyclerHorizontalSlideListCustomBinding
) :
    BaseCustomViewHolder(viewModel, binding.root) {

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

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

    override val gameChangedNotifier: IGameChangedNotifier
        get() = adapter

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSubjectItem) {
            setSubjectTitle(binding.layoutTitle, item, childEventHelper)
            val subjectEntity = item.data
            if (binding.recyclerView.adapter == null) {
                binding.recyclerView.layoutManager =
                    FixLinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false)
                binding.recyclerView.itemAnimator = null
                binding.recyclerView.setScrollingTouchSlop(RecyclerView.TOUCH_SLOP_PAGING)
                binding.recyclerView.isNestedScrollingEnabled = false
                binding.recyclerView.adapter = adapter

                binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                        super.onScrollStateChanged(recyclerView, newState)

                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            item.scrolledOffset = recyclerView.computeHorizontalScrollOffset()
                        }
                    }
                })
            }

            if (item.scrolledOffset == 0 && item.firstPositionInHorizontalTimeline != -1) {
                binding.recyclerView.scrollToPosition(item.firstPositionInHorizontalTimeline)
            } else {
                if (item.scrolledOffset != 0) {
                    binding.recyclerView.scrollBy(item.scrolledOffset, 0)
                }
            }

            runOnIoThread(isLightWeightTask = true) {
                subjectEntity.data?.let {
                    it.forEachIndexed { index, game ->
                        fillExposureAndReportAd(game, index, item)
                    }
                }
            }

            adapter.setData(item)
        }
    }

    /**
     * 获取游戏专题中 "今天/最靠近今天的一天（未来优先于过去）" 游戏的位置
     */
    private fun getTopOfLineContainer(vg: ViewGroup): Int {
        var height = 0
        for (i in 0 until vg.childCount) {
            val v: View = vg.getChildAt(i)
            if (v is ConstraintLayout && v.id == R.id.lineContainer) {
                return v.top
            } else if (v is ViewGroup) {
                height = this.getTopOfLineContainer(v) + v.top
            }
        }
        return height
    }

}