package com.gh.gamecenter.game.commoncollection

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.SpacingItemDecoration
import com.gh.gamecenter.databinding.CommonCollectionListBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.ExposureLinkEntity
import com.gh.gamecenter.entity.SubjectEntity

/**
 * 通用链接合集
 */
class CommonCollectionViewHolder(val binding: CommonCollectionListBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    private lateinit var mLayoutManager: LinearLayoutManager
    private var mCollection: SubjectEntity? = null

    fun bindCommonLinkCollection(
        collection: SubjectEntity,
        entrance: String,
        blockId: String,
        blockName: String,
        clickClosure: (position: Int, contentEntity: CommonCollectionContentEntity) -> Unit,
        exposureClosure: (position: Int, linkEntity: ExposureLinkEntity) -> Unit
    ) {
        mCollection = collection
        binding.collectionList.apply {
            isNestedScrollingEnabled = false
            if (adapter == null) {
                mLayoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                addItemDecoration(SpacingItemDecoration(notDecorateTheFirstItem = true, left = 8f.dip2px()))
                layoutManager = mLayoutManager
                addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                        super.onScrollStateChanged(recyclerView, newState)
                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            var position = mLayoutManager.findLastCompletelyVisibleItemPosition()
                            if (position == -1) position = mLayoutManager.findLastVisibleItemPosition() - 1
                            if (position < 0) return

                            val linkEntity = mCollection?.commonCollectionList?.get(position)
                            NewLogUtils.logSlideCommonCollection(
                                mCollection?.id ?: "",
                                mCollection?.name ?: "",
                                blockId,
                                blockName,
                                entrance,
                                "合集详情",
                                linkEntity?.title ?: "",
                                linkEntity?.addedContent1 ?: "",
                                linkEntity?.addedContent2 ?: "",
                                position + 1
                            )
                        }
                    }
                })
                adapter = CommonCollectionAdapter(context, mCollection!!).also {
                    it.setCallback(clickClosure, exposureClosure)
                }
            } else {
                mLayoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                if (itemDecorationCount > 0) {
                    removeItemDecorationAt(0)
                }
                addItemDecoration(SpacingItemDecoration(notDecorateTheFirstItem = true, left = 8F.dip2px()))
                layoutManager = mLayoutManager
                (adapter as CommonCollectionAdapter).apply {
                    setCallback(clickClosure, exposureClosure)
                    checkResetData(mCollection!!)
                }
            }
        }
    }
}