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

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.stacklayoutmanager.StackLayoutManager
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.HomeGameCollectionItemCustomBinding
import com.gh.gamecenter.home.custom.BannerInRecyclerController
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.CustomHomeGameCollectionCarouselAdapter
import com.gh.gamecenter.home.custom.eventlistener.GameSubjectCollectionEventHelper
import com.gh.gamecenter.home.custom.fillExposureInSubjectCollection
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem
import com.gh.gamecenter.home.gamecollection.carousel.GameCollectionStackAnimation
import com.gh.gamecenter.home.gamecollection.carousel.GameCollectionStackLayout

/**
 * 专题合集-轮播图
 * 游戏单合集-轮播图
 */
class CustomHomeGameCollectionCarouselViewHolder(
    viewModel: CustomPageViewModel,
    licOwner: LifecycleOwner,
    val binding: HomeGameCollectionItemCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

    private val bannerController = BannerInRecyclerController {
        if (adapter.itemCount > 0) {
            binding.recyclerView.smoothScrollToPosition(layoutManager.getFirstVisibleItemPosition() + 1)
        }
    }

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

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

    private val layoutManager by lazy {
        StackLayoutManager(
            StackLayoutManager.ScrollOrientation.RIGHT_TO_LEFT,
            3,
            GameCollectionStackAnimation::class.java,
            GameCollectionStackLayout::class.java
        ).apply { setPagerFlingVelocity(DisplayUtils.getScreenWidth() / 2) }
    }

    init {
        binding.recyclerView.isNestedScrollingEnabled = false
        licOwner.lifecycle.addObserver(bannerController)
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSubjectCollectionItem) {
            fillExposureInSubjectCollection(item, pageConfigure.exposureSourceList, createTrackData(item))

            setSubjectCollectionTitle(item, binding.layoutTitle, childEventHelper)

            if (binding.recyclerView.adapter == null) {
                binding.recyclerView.layoutManager = layoutManager
                binding.recyclerView.adapter = adapter
                layoutManager.setItemOffset(4F.dip2px())
                binding.recyclerView.addOnScrollListener(object : OnScrollListener() {
                    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                        super.onScrollStateChanged(recyclerView, newState)
                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            bannerController.start()
                        } else {
                            bannerController.pause()
                        }
                    }
                })
            }
            adapter.setData(item)
            bannerController.start()
        }
    }

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

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

}