package com.gh.gamecenter.game

import android.annotation.SuppressLint
import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.common.databind.BindingAdapters
import com.gh.common.exposure.IExposable
import com.gh.common.util.*
import com.gh.common.util.LogUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.BlockActivity
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.MainActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.ImagePagerAdapter
import com.gh.gamecenter.adapter.viewholder.GameHeadViewHolder
import com.gh.gamecenter.adapter.viewholder.GameImageViewHolder
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.adapter.viewholder.GameViewPagerViewHolder
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.callback.OnViewClickListener
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.FixGridLayoutManager
import com.gh.gamecenter.common.view.GridSpacingItemDecoration
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.databinding.GameViewpagerItemBinding
import com.gh.gamecenter.entity.CommonCollectionContentEntity
import com.gh.gamecenter.entity.GameNavigationEntity
import com.gh.gamecenter.eventbus.EBDownloadStatus
import com.gh.gamecenter.feature.databinding.GameItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureType
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.gh.gamecenter.feature.minigame.MiniGameItemHelper
import com.gh.gamecenter.game.bigimagerecommend.BigImageRecommendViewHolder
import com.gh.gamecenter.game.columncollection.GameColumnCollectionViewHolder
import com.gh.gamecenter.game.commoncollection.CommonCollection12ViewHolder
import com.gh.gamecenter.game.commoncollection.CommonCollectionViewHolder
import com.gh.gamecenter.game.commoncollection.detail.CommonCollectionDetailActivity
import com.gh.gamecenter.game.data.GameItemData
import com.gh.gamecenter.game.doublecard.DoubleCardViewHolder
import com.gh.gamecenter.game.gallery.GameGallerySlideViewHolder
import com.gh.gamecenter.game.gallery.GameGalleryViewHolder
import com.gh.gamecenter.game.horizontal.GameHorizontalAdapter
import com.gh.gamecenter.game.horizontal.GameHorizontalListViewHolder
import com.gh.gamecenter.game.horizontal.GameHorizontalSlideAdapter
import com.gh.gamecenter.game.horizontal.GameHorizontalSlideListViewHolder
import com.gh.gamecenter.game.imageslide.GameImageSlideViewHolder
import com.gh.gamecenter.game.rank.RankCollectionAdapter
import com.gh.gamecenter.game.rank.RankCollectionViewHolder
import com.gh.gamecenter.game.vertical.GameVerticalAdapter
import com.gh.gamecenter.game.vertical.GameVerticalSlideViewHolder
import com.gh.gamecenter.game.vertical.OnPagerSnapScrollListener
import com.gh.gamecenter.home.BlankDividerViewHolder
import com.gh.gamecenter.home.HomeDividerViewHolder
import com.gh.gamecenter.home.HomeGameItemViewHolder
import com.gh.gamecenter.home.LegacyHomeFragmentAdapterAssistant
import com.gh.gamecenter.home.discovercard.DiscoverCardGameAdapter
import com.gh.gamecenter.home.discovercard.HomeDiscoverCardViewHolder
import com.gh.gamecenter.home.gamecollection.carousel.HomeGameCollectionCarouselViewHolder
import com.gh.gamecenter.home.gamecollection.refresh.HomeGameCollectionRefreshAdapter
import com.gh.gamecenter.home.gamecollection.refresh.HomeGameCollectionRefreshViewHolder
import com.gh.gamecenter.home.gamecollection.slide.HomeGameCollectionSlideAdapter
import com.gh.gamecenter.home.gamecollection.slide.HomeGameCollectionSlideViewHolder
import com.gh.gamecenter.home.horizontalslidevideo.HomeHorizontalSlideVideoAdapter
import com.gh.gamecenter.home.horizontalslidevideo.HomeHorizontalSlideVideoListViewHolder
import com.gh.gamecenter.home.test_v2.HomeGameTestV2GameListRvAdapter
import com.gh.gamecenter.home.test_v2.HomeGameTestV2ViewModel
import com.gh.gamecenter.home.test_v2.HomeItemGameTestV2ViewHolder
import com.gh.gamecenter.home.video.ScrollCalculatorHelper
import com.gh.gamecenter.minigame.qq.QGameRecentlyPlayViewHolder
import com.gh.gamecenter.servers.gametest2.GameServerTestV2Activity
import com.gh.gamecenter.subject.SubjectActivity
import com.gh.vspace.HomeRecentVGameAdapter
import com.gh.vspace.HomeRecentVGameViewHolder
import com.gh.vspace.VHelper
import com.lightgame.adapter.BaseRecyclerAdapter
import com.lightgame.download.DownloadEntity

class GameFragmentAdapter(
    context: Context,
    model: BaseGameViewModel,
    private val mLifecycleOwner: LifecycleOwner,
    private val mHomeGameTestV2ViewModel: HomeGameTestV2ViewModel,
    private val mBasicExposureSource: ArrayList<ExposureSource>,
    private val mLayoutManager: LinearLayoutManager?,
    private val mScrollCalculatorHelper: ScrollCalculatorHelper? = null
) : BaseRecyclerAdapter<ViewHolder>(context), IExposable {

    private val mViewModel: BaseGameViewModel = model

    private var mLoadStatus: LoadStatus? = null

    private var mItemDataList: MutableList<GameItemData> = ArrayList()

    var isAutoScroll: Boolean = true

    private var mTopViewExposureEventList: ArrayList<ExposureEvent>? = null

    fun setItemDataList(itemDataList: MutableList<GameItemData>) {
        val currentSize = mItemDataList.size
        mItemDataList = itemDataList.toMutableList()
        if (currentSize > itemDataList.size) {
            notifyDataSetChanged()
        } else {
            notifyItemRangeChanged(0, itemDataList.size)
        }
    }

    fun setLoadStatus(status: LoadStatus) {
        this.mLoadStatus = status
        notifyItemChanged(itemCount - 1)
    }

    override fun getItemViewType(position: Int): Int {
        if (itemCount == position + 1) return ItemViewType.LOADING

        val itemData = mItemDataList[position]
        if (itemData.slideList != null || itemData.navigationList != null || itemData.subjectRecommend != null) return ItemViewType.GAME_SLIDE
        if (itemData.columnHead != null) return ItemViewType.COLUMN_HEADER
        if (itemData.game != null) return ItemViewType.GAME_NORMAL
        if (itemData.image != null) return ItemViewType.GAME_IMAGE
        if (itemData.horizontalColumn != null) return ItemViewType.GAME_SUBJECT
        if (itemData.horizontalSlide != null) return ItemViewType.GAME_SUBJECT_SLIDE
        if (itemData.qqHorizontalSlide != null) return ItemViewType.QQ_GAME_SUBJECT_SLIDE
        if (itemData.imageSlide != null) return ItemViewType.IMAGE_SLIDE_ITEM
        if (itemData.verticalSlide != null) return ItemViewType.VERTICAL_SLIDE_ITEM
        if (itemData.columnCollection != null) return ItemViewType.COLUMN_COLLECTION
        if (itemData.commonLinkCollection != null) return ItemViewType.COMMON_LINK_COLLECTION
        if (itemData.commonLinkCollection12 != null) return ItemViewType.COMMON_LINK_COLLECTION12
        if (itemData.gallery != null) return ItemViewType.GALLERY
        if (itemData.gallerySlide != null) return ItemViewType.GALLERY_SLIDE
        if (itemData.blankDivider != null) return ItemViewType.BLANK_DIVIDER
        if (itemData.rankCollection != null) return ItemViewType.RANK_COLLECTION
        if (itemData.gameCollection != null) {
            return when (itemData.gameCollection!!.style) {
                LegacyHomeFragmentAdapterAssistant.GAME_COLLECTION_CAROUSEL_STYLE -> ItemViewType.GAME_COLLECTION_CAROUSEL
                LegacyHomeFragmentAdapterAssistant.GAME_COLLECTION_BIG_SLIDE_STYLE -> ItemViewType.GAME_COLLECTION_BIG_SLIDE
                LegacyHomeFragmentAdapterAssistant.GAME_COLLECTION_SMALL_SLIDE_STYLE -> ItemViewType.GAME_COLLECTION_SMALL_SLIDE
                LegacyHomeFragmentAdapterAssistant.GAME_COLLECTION_HORIZONTAL_REFRESH_STYLE -> ItemViewType.GAME_COLLECTION_HORIZONTAL_REFRESH
                LegacyHomeFragmentAdapterAssistant.GAME_COLLECTION_VERTICAL_REFRESH_STYLE -> ItemViewType.GAME_COLLECTION_VERTICAL_REFRESH
                else -> ItemViewType.GAME_COLLECTION_CAROUSEL // style为空时默认展示轮播图
            }
        }
        if (itemData.doubleCardColumn != null) return ItemViewType.DOUBLE_CARD_COLUMN
        if (itemData.bigImageRecommend != null) return ItemViewType.BIG_IMAGE_RECOMMEND
        if (itemData.attachGame != null) return ItemViewType.GAME_ITEM
        if (itemData.lineDivider != null) return ItemViewType.DIVIDER_ITEM
        if (itemData.discoverCard != null) return ItemViewType.DISCOVER_CARD
        if (itemData.gameTestV2Entity != null) return ItemViewType.COLUMN_TEST_V2
        if (itemData.horizontalSlideVideo != null) return ItemViewType.HORIZONTAL_SLIDE_VIDEO
        if (itemData.recentVGame != null) return ItemViewType.RECENT_V_GAME

        return ItemViewType.LOADING
    }

    override fun onViewAttachedToWindow(holder: ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder is QGameRecentlyPlayViewHolder) {
            holder.onAttached()
        }
    }

    override fun onViewDetachedFromWindow(holder: ViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if (holder is QGameRecentlyPlayViewHolder) {
            holder.onDetached()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return when (viewType) {
            // 轮播图+导航栏+专题入口
            ItemViewType.GAME_SLIDE -> {
                val displayMetrics = mContext.resources.displayMetrics.widthPixels
                val view =
                    GameViewpagerItemBinding.bind(mLayoutInflater.inflate(R.layout.game_viewpager_item, parent, false))
                GameViewPagerViewHolder(view, displayMetrics)
            }

            ItemViewType.LOADING -> {
                FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false))
            }

            // 游戏专题-横屏
            ItemViewType.GAME_SUBJECT -> GameHorizontalListViewHolder(parent.toBinding())
            // 游戏专题-横屏滑动
            ItemViewType.GAME_SUBJECT_SLIDE -> GameHorizontalSlideListViewHolder(parent.toBinding())
            ItemViewType.QQ_GAME_SUBJECT_SLIDE -> QGameRecentlyPlayViewHolder(mLifecycleOwner, parent.toBinding())
            // 正常游戏
            ItemViewType.GAME_NORMAL -> GameItemViewHolder(parent.toBinding())
            // 游戏专题-大图-显示/只显示
            ItemViewType.GAME_IMAGE -> GameImageViewHolder(parent.toBinding())
            // 专题头部
            ItemViewType.COLUMN_HEADER -> GameHeadViewHolder(parent.toBinding())
            // 游戏专题-大图-只显示(滑动)
            ItemViewType.IMAGE_SLIDE_ITEM -> GameImageSlideViewHolder(parent.toBinding())
            // 游戏专题-竖屏滑动
            ItemViewType.VERTICAL_SLIDE_ITEM -> GameVerticalSlideViewHolder(parent.toBinding())
            // 专题合集
            ItemViewType.COLUMN_COLLECTION -> GameColumnCollectionViewHolder(parent.toBinding())
            // 游戏专题-图集滚动
            ItemViewType.GALLERY_SLIDE -> GameGallerySlideViewHolder(parent.toBinding())
            // 游戏专题-显示图集
            ItemViewType.GALLERY -> GameGalleryViewHolder(
                GameGalleryViewHolder.GameGalleryItemCell(mContext).apply { inflate() }
            )
            // 空白补充区域
            ItemViewType.BLANK_DIVIDER -> BlankDividerViewHolder(parent.toBinding())
            // 通用链接合集
            ItemViewType.COMMON_LINK_COLLECTION -> CommonCollectionViewHolder(parent.toBinding())
            // 通用链接合集(1-2样式)
            ItemViewType.COMMON_LINK_COLLECTION12 -> CommonCollection12ViewHolder(parent.toBinding())
            // 排行榜
            ItemViewType.RANK_COLLECTION -> RankCollectionViewHolder(parent.toBinding())
            // 游戏单合集-轮播图
            ItemViewType.GAME_COLLECTION_CAROUSEL -> HomeGameCollectionCarouselViewHolder(parent.toBinding())
            // 游戏专题-双列卡片
            ItemViewType.DOUBLE_CARD_COLUMN -> DoubleCardViewHolder(parent.toBinding())
            // 提问、帖子、视频帖、文章
            ItemViewType.BIG_IMAGE_RECOMMEND -> BigImageRecommendViewHolder(parent.toBinding())
            // 游戏-首页大图/顶部视频
            ItemViewType.GAME_ITEM -> HomeGameItemViewHolder(parent.toBinding())
            // 分割线
            ItemViewType.DIVIDER_ITEM -> HomeDividerViewHolder(parent.toBinding())
            // 发现页
            ItemViewType.DISCOVER_CARD -> HomeDiscoverCardViewHolder(parent.toBinding())
            // 新游开测
            ItemViewType.COLUMN_TEST_V2 -> HomeItemGameTestV2ViewHolder(
                parent.toBinding(),
                mHomeGameTestV2ViewModel,
                mLifecycleOwner
            )
            // 游戏专题-视频横屏滑动
            ItemViewType.HORIZONTAL_SLIDE_VIDEO -> HomeHorizontalSlideVideoListViewHolder(parent.toBinding())
            // 游戏单合集-横排滑动-大卡片/小卡片
            ItemViewType.GAME_COLLECTION_BIG_SLIDE,
            ItemViewType.GAME_COLLECTION_SMALL_SLIDE -> HomeGameCollectionSlideViewHolder(parent.toBinding())
            // 游戏单合集-刷新轮换-竖排列表/横排列表
            ItemViewType.GAME_COLLECTION_HORIZONTAL_REFRESH,
            ItemViewType.GAME_COLLECTION_VERTICAL_REFRESH -> HomeGameCollectionRefreshViewHolder(parent.toBinding())
            // 最近在玩
            ItemViewType.RECENT_V_GAME -> HomeRecentVGameViewHolder(parent.toBinding())

            else -> GameItemViewHolder(GameItemBinding.bind(mLayoutInflater.inflate(com.gh.gamecenter.feature.R.layout.game_item, parent, false)))
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (holder) {
            is GameHeadViewHolder -> bindHeadView(holder, position)
            is GameItemViewHolder -> bindGameItemView(holder, position)
            is GameViewPagerViewHolder -> bindGameViewPagerView(holder, position)
            is FooterViewHolder -> bindFooterView(holder)
            is GameImageViewHolder -> bindGameImageView(holder, position)
            is GameHorizontalListViewHolder -> bindGameHorizontalListView(holder, position)
            is QGameRecentlyPlayViewHolder -> bindQGameHorizontalSlideListView(holder, position)
            is GameHorizontalSlideListViewHolder -> bindGameHorizontalSlideListView(holder, position)
            is GameImageSlideViewHolder -> bindGameImageSlide(holder, position)
            is GameVerticalSlideViewHolder -> bindVerticalSlide(holder, position)
            is GameColumnCollectionViewHolder -> bindColumnCollection(holder, position)
            is GameGallerySlideViewHolder -> bindGallerySlide(holder, position)
            is GameGalleryViewHolder -> bindGallery(holder, position)
            is BlankDividerViewHolder -> holder.bindView(mItemDataList[position].blankDivider!!)
            is CommonCollectionViewHolder -> bindCommonCollection(holder, position)
            is CommonCollection12ViewHolder -> bindCommonCollection(holder, position)
            is RankCollectionViewHolder -> bindRankCollection(holder, position)
            is HomeGameCollectionCarouselViewHolder -> bindGameCollectionCarousel(holder, position)
            is DoubleCardViewHolder -> bindGameDoubleCard(holder, position)
            is BigImageRecommendViewHolder -> bindBigImageRecommend(holder, position)
            is HomeGameItemViewHolder -> bindAttachGame(holder, position)
            is HomeDividerViewHolder -> holder.bindView(mItemDataList[position].lineDivider ?: 1F)
            is HomeDiscoverCardViewHolder -> bindDiscoverCard(holder, position)
            is HomeItemGameTestV2ViewHolder -> bindGameTestV2ViewHolder(holder, position)
            is HomeHorizontalSlideVideoListViewHolder -> bindHomeHorizontalSlideVideo(holder, position)
            is HomeGameCollectionSlideViewHolder -> bindGameCollectionSlide(holder, position)
            is HomeGameCollectionRefreshViewHolder -> bindGameCollectionRefresh(holder, position)
            is HomeRecentVGameViewHolder -> bindRecentVGame(holder, position)
        }
    }

    fun notifyFirstItem() {
        if (mItemDataList.isNullOrEmpty()) return
        val itemData = mItemDataList[0]
        if (!itemData.slideList.isNullOrEmpty() || itemData.navigationList.isNullOrEmpty()) {
            notifyItemChanged(0)
        }
    }

    /**
     * 游戏专题-显示图集
     */
    private fun bindGallery(holder: GameGalleryViewHolder, position: Int) {
        val itemData = mItemDataList[position]
        itemData.exposureEventList = arrayListOf()

        (holder.itemView as GameGalleryViewHolder.GameGalleryItemCell).bindWhenInflated {
            val gallery = itemData.gallery
            holder.bindView(gallery!!, holder.cell.binding!!, mBasicExposureSource) {
                itemData.exposureEventList?.add(it)
            }

            holder.cell.binding?.root?.setOnClickListener {
                setPageSwitchData()
                DirectUtils.directToSubject(
                    it.context, gallery.id ?: "", gallery.name, "(游戏-专题)", null, gallery.subjectType
                )
                NewLogUtils.logColumnPictureClick(
                    "显示图集", gallery.name ?: "", gallery.id ?: "", gallery.id ?: "", "column",
                    gallery.name ?: "", "板块", mViewModel.blockData?.name ?: ""
                )
            }
        }
    }

    /**
     * 游戏专题-图集滚动
     */
    private fun bindGallerySlide(holder: GameGallerySlideViewHolder, position: Int) {
        val itemData = mItemDataList[position]
        itemData.exposureEventList = arrayListOf()

        holder.bindView(itemData.gallerySlide!!, mBasicExposureSource, "(游戏-专题)") {
            itemData.exposureEventList?.add(it)
        }
    }

    /**
     * 专题合集
     */
    private fun bindColumnCollection(holder: GameColumnCollectionViewHolder, position: Int) {
        val itemData = mItemDataList[position]
        val columnCollection = mItemDataList[position].columnCollection!!

        // 重置曝光列表
        mItemDataList[position].exposureEventList = arrayListOf()

        val clickClosure: (position: Int, gameEntity: GameEntity) -> Unit = { prefixedPosition, gameEntity ->
            // 类型为排行榜时点击处理不一样
            if (columnCollection.style == "top") {
                DirectUtils.directToColumnCollection(
                    mContext,
                    columnCollection.id
                        ?: "",
                    prefixedPosition,
                    "(首页游戏)",
                    "",
                    mViewModel.blockData?.link ?: "",
                    mViewModel.blockData?.text ?: ""
                )
            } else {
                setPageSwitchData()
                DirectUtils.directToLinkPage(
                    mContext,
                    LinkEntity(
                        link = gameEntity.link,
                        type = gameEntity.type,
                        blockId = mViewModel.blockData?.link ?: "",
                        blockName = mViewModel.blockData?.text ?: ""
                    ),
                    "(首页游戏)",
                    "游戏-专题",
                    gameEntity.exposureEvent
                )
            }
            NewLogUtils.logColumnCategoryHomeContentClick(
                gameEntity.name ?: "", gameEntity.link ?: "",
                columnCollection.name ?: "", columnCollection.id ?: "", "板块",
                mViewModel.blockData?.name ?: ""
            )
            SensorsBridge.trackColumnCollectionClick(
                location = "版块",
                blockName = mViewModel.blockData?.text ?: "",
                blockId = mViewModel.blockData?.link ?: "",
                columnCollectionName = columnCollection.name ?: "",
                columnCollectionId = columnCollection.id ?: "",
                position = prefixedPosition,
                gameColumnName = gameEntity.name ?: "",
                gameColumnId = gameEntity.link ?: "",
                text = "游戏专题",
                adGroupId = gameEntity.adGroupId
            )
        }

        holder.bindColumnCollection(columnCollection, mBasicExposureSource, clickClosure) {
            it.payload.outerSequence = itemData.columnCollection?.outerSequence
            itemData.exposureEventList?.add(it)
        }

        val dataList = columnCollection.data!!
        if (dataList.size == 1) {
            val data = dataList[0]
            holder.binding.columnCollectionImage.setOnClickListener {
                if (columnCollection.style == "top") {
                    DirectUtils.directToColumnCollection(
                        mContext,
                        columnCollection.id
                            ?: "",
                        0, "(首页游戏)",
                        "",
                        mViewModel.blockData?.link ?: "",
                        mViewModel.blockData?.text ?: ""
                    )
                } else {
                    MtaHelper.onEvent("游戏专题合集", "首页合集图片", data.name)
                    setPageSwitchData()
                    DirectUtils.directToLinkPage(
                        mContext,
                        LinkEntity(
                            link = data.link,
                            type = data.type,
                            blockId = mViewModel.blockData?.link ?: "",
                            blockName = mViewModel.blockData?.text ?: ""
                        ),
                        "(首页游戏)",
                        "游戏-专题"
                    )
                }
            }
        } else {
            holder.binding.columnCollectionList.doOnScrolledSpecificDistance(
                distanceX = DisplayUtils.dip2px(24f),
                singleTimeEvent = true
            ) {
                MtaHelper.onEvent("游戏专题合集", "滑动", columnCollection.name)
            }
        }
    }

    /**
     * 通用内容合集
     */
    private fun bindCommonCollection(holder: ViewHolder, position: Int) {
        val commonLinkCollection = mItemDataList[position].commonLinkCollection
            ?: mItemDataList[position].commonLinkCollection12 ?: return
        val blockData = mViewModel.blockData
        mItemDataList[position].exposureEventList = arrayListOf()

        val clickClosure: (position: Int, contentEntity: CommonCollectionContentEntity) -> Unit =
            { sequence, contentEntity ->
                val linkEntity = contentEntity.linkEntity
                DirectUtils.directToLinkPage(
                    mContext,
                    linkEntity,
                    "(首页游戏)",
                    "通用链接合集",
                    linkEntity.exposureEvent
                )
                NewLogUtils.logCommonCollectionClick(
                    commonLinkCollection.id ?: "",
                    commonLinkCollection.name ?: "",
                    blockData?.link ?: "",
                    blockData?.name ?: "",
                    "板块内容列表",
                    "合集首页",
                    linkEntity.title ?: "",
                    contentEntity.addedContent1 ?: "",
                    contentEntity.addedContent2 ?: "",
                    linkEntity.type ?: "",
                    linkEntity.text ?: "",
                    sequence + 1
                )
                NewLogUtils.logCommonCategoryHomeContentClick(
                    contentEntity.title,
                    linkEntity.type ?: "",
                    linkEntity.link ?: "",
                    linkEntity.text ?: "",
                    commonLinkCollection.name ?: "",
                    commonLinkCollection.id ?: "",
                    "板块",
                    blockData?.name ?: ""
                )
                SensorsBridge.trackLinkContentCollectionClick(
                    location = "版块",
                    blockName = mViewModel.blockData?.text ?: "",
                    blockId = mViewModel.blockData?.link ?: "",
                    linkContentCollectionId = commonLinkCollection.id ?: "",
                    linkContentCollectionName = commonLinkCollection.name ?: "",
                    position = sequence,
                    linkType = linkEntity.type ?: "",
                    linkId = linkEntity.link ?: "",
                    linkText = linkEntity.text ?: "",
                    text = "通用内容"
                )
            }

        if (holder is CommonCollectionViewHolder) {
            holder.bindCommonLinkCollection(
                commonLinkCollection,
                "板块内容列表",
                blockData?.link ?: "",
                blockData?.name ?: "",
                clickClosure
            ) { subPosition, linkEntity ->
                mItemDataList[position].exposureEventList?.add(
                    ExposureEvent.createEventWithSourceConcat(
                        GameEntity(
                            id = linkEntity.link,
                            name = linkEntity.name
                        ).also {
                            it.sequence = subPosition
                            it.outerSequence = commonLinkCollection.outerSequence
                        },
                        mBasicExposureSource,
                        listOf(ExposureSource("内容合集", commonLinkCollection.name ?: ""))
                    ).also { linkEntity.exposureEvent = it }
                )
            }
        } else if (holder is CommonCollection12ViewHolder) {
            holder.bindView(commonLinkCollection, mItemDataList[position].offset, clickClosure)

            for (i in mItemDataList[position].offset until (mItemDataList[position].offset + 1)) {
                if (commonLinkCollection.commonCollectionList!!.size > i) {
                    mItemDataList[position].exposureEventList?.add(
                        ExposureEvent.createEventWithSourceConcat(
                            GameEntity(
                                id = commonLinkCollection.commonCollectionList?.get(i)?.linkEntity?.link,
                                name = commonLinkCollection.commonCollectionList?.get(i)?.linkEntity?.name
                            ).also {
                                it.sequence = mItemDataList[position].offset
                                it.outerSequence = commonLinkCollection.outerSequence
                            },
                            mBasicExposureSource,
                            listOf(ExposureSource("内容合集", commonLinkCollection.name ?: ""))
                        ).also { commonLinkCollection.commonCollectionList?.get(i)?.linkEntity?.exposureEvent = it }
                    )
                }
            }
        }
    }

    /**
     * 排行榜
     */
    private fun bindRankCollection(holder: RankCollectionViewHolder, position: Int) {
        holder.itemView.layoutParams =
            (holder.itemView.layoutParams as ViewGroup.MarginLayoutParams).apply {
                topMargin = 0
            }

        val rankCollection = mItemDataList[position].rankCollection
        val rankCollectionAdapter = holder.bindRankCollection(rankCollection!!) { subject, sequence ->
            NewLogUtils.logColumnCategoryHomeContentClick(
                subject.name ?: "", subject.id ?: "", rankCollection.name ?: "",
                rankCollection.id ?: "", "版块", mViewModel.blockData?.name ?: ""
            )
            SensorsBridge.trackColumnCollectionClick(
                location = "版块",
                blockName = mViewModel.blockData?.text ?: "",
                blockId = mViewModel.blockData?.link ?: "",
                columnCollectionName = rankCollection.name ?: "",
                columnCollectionId = rankCollection.id ?: "",
                position = sequence,
                gameColumnName = subject.name ?: "",
                gameColumnId = subject.id ?: "",
                text = "游戏专题",
            )
        }

        val exposureEventList = arrayListOf<ExposureEvent>()
        rankCollection.columns.let {
            runOnIoThread(true) {
                rankCollection.columns.forEach {
                    it.data?.forEachIndexed { index, gameEntity ->
                        if (index > 4) return@forEach

                        val event = ExposureEvent.createEventWithSourceConcat(
                            gameEntity = gameEntity,
                            basicSource = mBasicExposureSource,
                            source = listOf(
                                ExposureSource("排行榜", ""),
                                ExposureSource("专题", it.name ?: "")
                            )
                        )
                        exposureEventList.add(event)
                    }
                }
            }
            mItemDataList[position].exposureEventList = exposureEventList
            rankCollectionAdapter.exposureEventList = exposureEventList
        }
    }

    /**
     * 游戏专题-竖屏滑动
     */
    private fun bindVerticalSlide(holder: GameVerticalSlideViewHolder, position: Int) {
        val listExposureEventList = arrayListOf<ExposureEvent>()
        val clickClosure: (Int, GameEntity) -> Unit = { _, gameEntity ->
            val subjectData = gameEntity.subjectData
            DataCollectionUtils.uploadClick(mContext, subjectData?.name + "-列表", "游戏-专题", gameEntity.name)
            if (gameEntity.isMiniGame()) {
                MiniGameItemHelper.launchMiniGame(gameEntity)
            } else {
                GameDetailActivity.startGameDetailActivity(
                    mContext, gameEntity,
                    StringUtils.buildString(
                        "(游戏-专题:",
                        subjectData?.name,
                        "-列表[",
                        (subjectData?.position).toString(),
                        "])"
                    ),
                    traceEvent = gameEntity.exposureEvent
                )
            }
        }

        val verticalSlide = mItemDataList[position].verticalSlide!!
        val binding = holder.binding
        val snapHelper = holder.bindVerticalSlide(verticalSlide, clickClosure, mContext !is MainActivity)

        val exposureEventList = arrayListOf<ExposureEvent>()
        val exposureClosure: (Int) -> Unit = {
            val adapter = binding.recyclerView.adapter as GameVerticalAdapter
            val gameList = verticalSlide.data!!
            listExposureEventList.clear()

            val startOffset = it * verticalSlide.list + adapter.getIndex()
            val endOffset = if (startOffset + verticalSlide.list >= gameList.size) {
                gameList.size
            } else {
                startOffset + verticalSlide.list
            }
            for (i in startOffset until endOffset) {
                val event = ExposureEvent.createEventWithSourceConcat(
                    gameEntity = gameList[i].apply {
                        isAdData = verticalSlide.adIconActive
                    },
                    basicSource = mBasicExposureSource,
                    source = listOf(ExposureSource("专题", verticalSlide.name!!)),
                    eTrace = null,
                    event = ExposureType.EXPOSURE
                )
                gameList[i].exposureEvent = event
                listExposureEventList.add(event)
            }
            exposureEventList.addAll(listExposureEventList)
        }

        exposureClosure(0)
        binding.recyclerView.addOnScrollListener(OnPagerSnapScrollListener(snapHelper) {
            MtaHelper.onEvent("游戏专题", "滑动", verticalSlide.name)
            exposureClosure(it)
        })

        mItemDataList[position].exposureEventList = exposureEventList
    }

    /**
     * 游戏专题-大图-只显示(滑动)
     */
    private fun bindGameImageSlide(holder: GameImageSlideViewHolder, position: Int) {
        val binding = holder.binding
        val entity = mItemDataList[position].imageSlide

        if (binding.columnList.adapter == null) {
            binding.columnList.doOnScrolledSpecificDistance(
                distanceX = DisplayUtils.dip2px(24f),
                singleTimeEvent = true
            ) {
                MtaHelper.onEvent("游戏专题", "滑动", entity?.name)
            }
        }

        val exposureEventList = arrayListOf<ExposureEvent>()
        exposureEventList.clear()
        for (gameEntity in entity?.games ?: arrayListOf()) {
            val exposureEvent = ExposureEvent.createEventWithSourceConcat(
                gameEntity,
                mBasicExposureSource,
                listOf(ExposureSource("专题", entity?.name + "-大图"))
            )
            gameEntity.exposureEvent = exposureEvent
            exposureEventList.add(exposureEvent)
        }
        mItemDataList[position].exposureEventList = exposureEventList

        val imageClickListener = View.OnClickListener {
            DataCollectionUtils.uploadClick(mContext, entity?.name + "-大图", "游戏-专题")
            MtaHelper.onEvent("游戏专题", "底部图片", entity?.name)
            val linkEntity = LinkEntity()
            linkEntity.link = entity?.link
            linkEntity.type = entity?.type
            PageSwitchDataHelper.pushCurrentPageData(
                hashMapOf(
                    Pair(PageSwitchDataHelper.PAGE_BUSINESS_TYPE, "游戏专题-大图"),
                    Pair(PageSwitchDataHelper.PAGE_BUSINESS_NAME, entity?.subjectName ?: "")
                )
            )
            DirectUtils.directToLinkPage(
                mContext,
                linkEntity,
                "(游戏-专题:" + entity?.name + "-大图)",
                "首页游戏",
                entity?.exposureEvent
            )
            NewLogUtils.logColumnPictureClick(
                "大图", entity?.name ?: "", entity?.id ?: "", entity?.id ?: "", "column",
                entity?.name ?: "", "板块", mViewModel.blockData?.name ?: ""
            )
        }

        holder.bindImageSlide(entity!!, imageClickListener, "游戏-专题", mViewModel)
    }

    /**
     * 游戏专题-横屏
     */
    private fun bindGameHorizontalListView(holder: GameHorizontalListViewHolder, position: Int) {
        val subjectEntity = mItemDataList[position].horizontalColumn
        val subjectAdapter = holder.bindHorizontalList(subjectEntity!!)

        if (subjectEntity.type != "game_horizontal") {
            holder.binding.recyclerView.doOnScrolledSpecificDistance(
                distanceX = DisplayUtils.dip2px(24f),
                singleTimeEvent = true
            ) {
                MtaHelper.onEvent("游戏专题", "滑动", subjectEntity.name)
            }
        }

        val exposureEventList = arrayListOf<ExposureEvent>()
        subjectEntity.data?.let {
            val positionOffset = subjectAdapter.getIndex()
            tryCatchInRelease {
                for (i in positionOffset until subjectAdapter.itemCount + positionOffset) {
                    it[i].sequence = i
                    it[i].isAdData = subjectEntity.adIconActive
                    val event = ExposureEvent.createEventWithSourceConcat(
                        gameEntity = it[i],
                        basicSource = mBasicExposureSource,
                        source = listOf(ExposureSource("专题", subjectEntity.name!!)),
                        eTrace = null,
                        event = ExposureType.EXPOSURE
                    )
                    exposureEventList.add(event)
                }
                mItemDataList[position].exposureEventList = exposureEventList
                subjectAdapter.exposureEventList = exposureEventList
            }
        }
    }

    /**
     * 游戏专题-横屏滑动
     */
    private fun bindGameHorizontalSlideListView(holder: GameHorizontalSlideListViewHolder, position: Int) {
        val subjectEntity = mItemDataList[position].horizontalSlide
        val subjectAdapter = holder.bindHorizontalSlideList(subjectEntity!!, mViewModel)

        subjectEntity.data?.let {
            val positionOffset = subjectAdapter.getIndex()
            tryCatchInRelease {
                val exposureEventList = arrayListOf<ExposureEvent>()
                runOnIoThread(true) {
                    for (i in positionOffset until subjectAdapter.itemCount + positionOffset) {
                        if (i >= it.size) break

                        it[i].sequence = i
                        it[i].isAdData = subjectEntity.adIconActive

                        val exposureKey = if (subjectEntity.remark != null) "开测表" else "专题"
                        val exposureValue =
                            if (!subjectEntity.remark.isNullOrBlank()) "${subjectEntity.name!!}-${subjectEntity.remark}" else subjectEntity.name!!

                        val event = ExposureEvent.createEventWithSourceConcat(
                            gameEntity = it[i],
                            basicSource = mBasicExposureSource,
                            source = listOf(ExposureSource(exposureKey, exposureValue)),
                        )
                        exposureEventList.add(event)
                    }
                }
                mItemDataList[position].exposureEventList = exposureEventList
                subjectAdapter.exposureEventList = exposureEventList
            }
        }
    }

    private fun bindQGameHorizontalSlideListView(holder: QGameRecentlyPlayViewHolder, position: Int) {
        val subject = mItemDataList[position].qqHorizontalSlide
        holder.bindView(subject!!)

        holder.binding.moreTv.setOnClickListener {
            val buttonType = when (subject.home) {
                "change" -> "换一批"
                "more" -> "更多"
                else -> "全部"
            }
            val moreLink = subject.moreLink
            NewLogUtils.logColumnListClickButton(
                buttonType, subject.name ?: "", subject.id ?: "", moreLink?.type ?: "",
                moreLink?.name ?: "", "版块", mViewModel.blockData?.name ?: ""
            )
            when (subject.home) {
                "change" -> {
                    MtaHelper.onEvent("游戏专题", "换一批", subject.name)
                    holder.binding.moreTv.visibility = View.VISIBLE
                    mViewModel.changeSubjectGame(subject.id!!)
                }

                "more" -> {
                    setPageSwitchData()
                    subject.moreLink?.let { link ->
                        DirectUtils.directToLinkPage(
                            it.context,
                            link,
                            "(板块)",
                            "(游戏-专题:" + subject.name + "-全部)"
                        )
                    }
                }

                else -> {
                    setPageSwitchData()
                    if (subject.indexRightTopLink != null) {
                        val link = subject.indexRightTopLink!!

                        // ugly ugly ugly as hell
                        if (holder.binding.moreTv.text.contains("更多")) {
                            LogUtils.logServerTestClickMoreEvent(link.text, subject.remark, link.type, link.link)
                        } else {
                            LogUtils.logServerTestClickAllEvent(link.text, subject.remark)
                        }
                        DirectUtils.directToLinkPage(mContext, link, "(游戏-专题:" + subject.name + "-全部)", "")
                    } else {
                        SubjectActivity.startSubjectActivity(
                            mContext,
                            subject.id,
                            subject.getFilterName(),
                            subject.isOrder,
                            mBasicExposureSource,
                            "(游戏-专题:" + subject.name + "-全部)",
                            subject.subjectType
                        )
                    }
                    MtaHelper.onEvent("游戏专题", "全部", subject.name)
                }
            }
        }
    }

    /**
     * 游戏专题-大图-显示/只显示
     */
    private fun bindGameImageView(holder: GameImageViewHolder, position: Int) {
        val entity = mItemDataList[position].image
        holder.bindImage(entity!!)

        val name = entity.subjectData?.name
        val link = entity.link

        if (entity.type == "game") {
            mItemDataList[position].exposureEvent = ExposureEvent.createEventWithSourceConcat(
                gameEntity = entity,
                basicSource = mBasicExposureSource,
                source = listOf(ExposureSource("专题", entity.subjectData?.name + "-大图")),
                eTrace = null,
                event = ExposureType.EXPOSURE
            )
            BindingAdapters.setDownloadButton(
                holder.binding.progressBar,
                entity,
                mItemDataList[position].exposureEvent,
                null,
                null,
                ""
            )
        }
        holder.binding.gameImageIcon.setOnClickListener {
            DataCollectionUtils.uploadClick(mContext, "$name-大图", "游戏-专题")
            MtaHelper.onEvent("游戏专题", "大图", name + ":" + entity.name)
            val linkEntity = LinkEntity()
            linkEntity.link = link
            linkEntity.type = entity.type
            linkEntity.text = entity.text
            linkEntity.name = entity.name
            linkEntity.community = entity.community
            linkEntity.display = entity.display
            setPageSwitchData()
            DirectUtils.directToLinkPage(
                mContext, linkEntity, "(游戏-专题:$name-大图)", mViewModel.blockData?.name
                    ?: "", mItemDataList[position].exposureEvent
            )
            NewLogUtils.logColumnPictureClick(
                "大图", entity.name ?: "", entity.id, entity.id, "column",
                entity.name ?: "", "板块", mViewModel.blockData?.name ?: ""
            )
        }
    }

    private fun bindFooterView(holder: FooterViewHolder) {
        holder.initFooterViewHolder(
            mLoadStatus == LoadStatus.LIST_LOADING,
            mLoadStatus == LoadStatus.LIST_FAILED,
            mLoadStatus == LoadStatus.LIST_OVER,
            R.string.load_over_with_click_hint
        ) {
            if (mLoadStatus == LoadStatus.LIST_OVER) {
                MtaHelper.onEvent("首页_点击", "底部", "点击回到顶部")
                mViewModel.commandScrollTop.postValue(null)
            } else {
                mViewModel.getSubjectList()
                notifyItemChanged(itemCount - 1)
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun bindGameViewPagerView(holder: GameViewPagerViewHolder, position: Int) {
        val subjectDigestList = mItemDataList[position].subjectRecommend
        val binding = holder.binding

        val blockData = mViewModel.blockData
        val clickListener = OnViewClickListener<Int> { v, clickedPosition ->
            if (clickedPosition is Int) {
                if (clickedPosition < subjectDigestList?.size!!) {

                    val entity = subjectDigestList[clickedPosition]
                    val exposureEvent = entity.exposureEvent

                    setPageSwitchData(false)

                    SensorsBridge.trackEvent("RecommendedLocationClick", json {
                        "position" to clickedPosition
                        "Recommended_location_name" to entity.name
                        "location" to "版块"
                        "block_name" to blockData?.text
                        "block_id" to blockData?.link
                    })

                    LogUtils.logRecommendClick(
                        "版块:${blockData?.text ?: ""}",
                        entity.name, entity.type, entity.text, entity.link, clickedPosition
                    )

                    val entrance = "(推荐入口)"
                    when (entity.type) {
                        "column_collection" -> DirectUtils.directToColumnCollection(
                            mContext,
                            entity.link!!,
                            -1,
                            entrance,
                            "",
                            mViewModel.blockData?.link ?: "",
                            mViewModel.blockData?.text ?: "",
                            "",
                            exposureEvent
                        )

                        "block" -> mContext.startActivity(
                            BlockActivity.getIntent(
                                mContext,
                                entity,
                                exposureEvent?.source?.toArrayList(),
                                entrance
                            )
                        )

                        "column" -> {
                            SubjectActivity.startSubjectActivity(
                                mContext,
                                entity.link,
                                entity.text,
                                entity.order,
                                exposureEvent?.source?.toArrayList(),
                                StringUtils.buildString(
                                    "(游戏-专题:",
                                    entity.name,
                                    "[1-",
                                    (clickedPosition + 1).toString(),
                                    "]",
                                    ")"
                                )
                            )
                        }

                        "game" -> GameDetailActivity.startGameDetailActivity(
                            mContext,
                            entity.link ?: "",
                            entrance,
                            exposureEvent
                        )

                        EntranceConsts.HOST_COMMUNITY -> DirectUtils.directToCommunity(
                            mContext,
                            CommunityEntity(entity.link!!, entity.text!!)
                        )

                        "top_game_comment" -> DirectUtils.directToAmway(mContext, null, entrance, "")
                        "common_collection" -> {
                            val linkEntity =
                                LinkEntity(
                                    name = entity.name,
                                    link = entity.link,
                                    text = entity.text,
                                    type = entity.type,
                                    display = entity.display,
                                    blockId = blockData?.link ?: "",
                                    blockName = blockData?.text ?: ""
                                )
                            DirectUtils.directToLinkPage(mContext, linkEntity, "板块推荐入口", "", exposureEvent)
                            NewLogUtils.logAccessToCommonCollectionDetail(
                                entity.link ?: "",
                                entity.text ?: "",
                                "板块推荐入口",
                                blockData?.link ?: "",
                                blockData?.name ?: ""
                            )
                        }

                        "game_list_square" -> {
                            DirectUtils.directToGameCollectionSquare(
                                mContext,
                                "版块推荐入口",
                                blockData?.name ?: "",
                            )
                        }

                        else -> {
                            val linkEntity = LinkEntity(
                                name = entity.name,
                                link = entity.link,
                                text = entity.text,
                                type = entity.type,
                                display = entity.display
                            )
                            DirectUtils.directToLinkPage(
                                mContext,
                                linkEntity,
                                entrance,
                                "",
                                exposureEvent = exposureEvent
                            )
                        }
                    }
                }
            } else if (v.id == R.id.home_subject_failure) {
                mViewModel.initData()
            } else if (v.id == R.id.viewpager_tv_failure) {
                mViewModel.initData()
            }
        }
        binding.run {
            mTopViewExposureEventList = arrayListOf()

            subjectDigestList?.forEachIndexed { index, entity ->
                val event = ExposureEvent.createEventWithSourceConcat(
                    gameEntity = GameEntity(
                        containerId = blockData?.link ?: "",
                        containerType = ExposureEntity.BLOCK_ID
                    ).also {
                        it.sequence = index
                        it.outerSequence = position
                    },
                    basicSource = mBasicExposureSource,
                    source = listOf(
                        ExposureSource(
                            "推荐入口",
                            entity.name ?: ""
                        )
                    )
                )
                event.payload.controlType = "推荐入口"
                event.payload.controlName = entity.name
                event.payload.controlLinkType = entity.type
                event.payload.controlLinkName = entity.text
                entity.exposureEvent = event
                mTopViewExposureEventList?.add(event)
            }
            val slideList = mItemDataList[position].slideList
            slideList?.forEachIndexed { index, entity ->
                val event = ExposureEvent.createEventWithSourceConcat(
                    gameEntity = GameEntity(
                        containerId = blockData?.link ?: "",
                        containerType = ExposureEntity.BLOCK_ID
                    ).also {
                        it.sequence = index
                        it.outerSequence = position
                    },
                    basicSource = mBasicExposureSource,
                    source = listOf(ExposureSource("轮播图"))
                )
                event.payload.controlType = "轮播图"
                event.payload.controlName = entity.name
                event.payload.controlLinkType = entity.type
                event.payload.controlLinkName = entity.text
                mTopViewExposureEventList?.add(event)
            }

            mItemDataList[position].exposureEventList = mTopViewExposureEventList

            viewpagerTvFailure.goneIf(subjectDigestList != null)
            viewpagerTvFailure.setOnClickListener {
                clickListener.onClick(it, null)
            }
            homeSubjectFailure.goneIf(subjectDigestList != null)
            homeSubjectFailure.setOnClickListener {
                clickListener.onClick(it, null)
            }
            entranceContainer1.setOnClickListener { clickListener.onClick(it, 0) }
            entranceContainer2.setOnClickListener { clickListener.onClick(it, 1) }
            entranceContainer3.setOnClickListener { clickListener.onClick(it, 2) }
            entranceContainer4.setOnClickListener { clickListener.onClick(it, 3) }
            entranceContainer5.setOnClickListener { clickListener.onClick(it, 4) }
            subjectDigestList?.let {
                ImageUtils.display(entranceIcon1, if (subjectDigestList.isNotEmpty()) subjectDigestList[0].icon else "")
                ImageUtils.display(entranceIcon2, if (subjectDigestList.size > 1) subjectDigestList[1].icon else "")
                ImageUtils.display(entranceIcon3, if (subjectDigestList.size > 2) subjectDigestList[2].icon else "")
                ImageUtils.display(entranceIcon4, if (subjectDigestList.size > 3) subjectDigestList[3].icon else "")
                ImageUtils.display(entranceIcon5, if (subjectDigestList.size > 4) subjectDigestList[4].icon else "")
                entranceName1.text = if (subjectDigestList.isNotEmpty()) {
                    subjectDigestList[0].name
                } else "推荐入口"
                entranceName2.text = if (subjectDigestList.size > 1) {
                    subjectDigestList[1].name
                } else "推荐入口"
                entranceName3.text = if (subjectDigestList.size > 2) {
                    subjectDigestList[2].name
                } else "推荐入口"
                entranceName4.text = if (subjectDigestList.size > 3) {
                    subjectDigestList[3].name
                } else "推荐入口"
                entranceName5.text = if (subjectDigestList.size > 4) {
                    subjectDigestList[4].name
                } else "推荐入口"
            }

            entranceName1.setTextColor(com.gh.gamecenter.common.R.color.text_3a3a3a.toColor(root.context))
            entranceName2.setTextColor(com.gh.gamecenter.common.R.color.text_3a3a3a.toColor(root.context))
            entranceName3.setTextColor(com.gh.gamecenter.common.R.color.text_3a3a3a.toColor(root.context))
            entranceName4.setTextColor(com.gh.gamecenter.common.R.color.text_3a3a3a.toColor(root.context))
            entranceName5.setTextColor(com.gh.gamecenter.common.R.color.text_3a3a3a.toColor(root.context))
        }

        val itemData = mItemDataList[position]
        val slideList = itemData.slideList
        val pagerAdapter = binding.viewPager.adapter
        if (slideList != null
            && slideList.isNotEmpty()
            && (pagerAdapter == null
                    || (pagerAdapter is ImagePagerAdapter && pagerAdapter.dataSize != slideList.size))
        ) {
            binding.viewpagerLlHint.removeAllViews()
            val margin = DisplayUtils.dip2px(mContext, 6f)
            var i = 0
            val size = slideList.size
            while (i < size) {
                val imageView = ImageView(mContext)
                val lparams = LinearLayout.LayoutParams(margin, margin)
                if (i == 0) {
                    imageView.setImageResource(R.drawable.oval_hint_up)
                } else {
                    imageView.setImageResource(R.drawable.oval_hint_dn)
                }
                if (i != 0) {
                    lparams.leftMargin = margin
                }
                imageView.layoutParams = lparams
                binding.viewpagerLlHint.addView(imageView)
                i++
            }

            binding.viewPager.adapter =
                ImagePagerAdapter(mContext, itemData, itemData.slideList?.size != 1, mBasicExposureSource, blockData)
            binding.viewPager.interval = 3000
            binding.viewPager.startAutoScroll()
            binding.viewPager.doOnPageSelected {
                var j = 0
                val childCount = binding.viewpagerLlHint.childCount
                while (j < childCount) {
                    if (j == it % childCount) {
                        (binding.viewpagerLlHint.getChildAt(j) as ImageView)
                            .setImageResource(R.drawable.oval_hint_up)
                    } else {
                        (binding.viewpagerLlHint.getChildAt(j) as ImageView)
                            .setImageResource(R.drawable.oval_hint_dn)
                    }
                    j++
                }

                SensorsBridge.trackEvent("BannerSlide", json {
                    "position" to it
                    "location" to "版块"
                    "block_name" to blockData?.text
                    "block_id" to blockData?.link
                })
            }
            binding.viewPager.setOnTouchListener { _, event ->
                if (isAutoScroll) {
                    if (event.action == MotionEvent.ACTION_DOWN || event.action == MotionEvent.ACTION_MOVE) {
                        binding.viewPager.stopAutoScroll()
                    } else {
                        binding.viewPager.startAutoScroll()
                    }
                }
                false
            }
        } else if (slideList != null && slideList.isNotEmpty() && pagerAdapter != null) {
            (pagerAdapter as ImagePagerAdapter).checkResetData(itemData)
        } else if (slideList == null || slideList.isEmpty()) {
            binding.viewPager.adapter = null
        }
        binding.viewpagerContainer.visibility = if (slideList!!.isEmpty()) View.GONE else View.VISIBLE
        binding.entranceContainer.visibility = if (subjectDigestList!!.isEmpty()) View.GONE else View.VISIBLE
        if (isAutoScroll) {
            binding.viewPager.startAutoScroll()
        } else {
            binding.viewPager.stopAutoScroll()
        }

        val navigationList = itemData.navigationList
        var navigationAdapter = binding.navigationRv.adapter
        if (!navigationList.isNullOrEmpty()) {
            binding.navigationRv.run {
                layoutManager = FixGridLayoutManager(mContext, 4)
                if (itemDecorationCount == 0) {
                    addItemDecoration(GridSpacingItemDecoration(4, 8f.dip2px(), false, 16f.dip2px()))
                }
                if (navigationAdapter == null) {
                    navigationAdapter = GameNavigationAdapter(mContext, blockData?.link ?: "", blockData?.text ?: "")
                }

                val exposureEventList = arrayListOf<ExposureEvent>()
                navigationList.forEachIndexed { index, entity ->
                    val event = ExposureEvent.createEventWithSourceConcat(
                        gameEntity = GameEntity(
                            containerId = entity.id,
                            containerType = ExposureEntity.NAVIGATION_ID
                        ).also {
                            it.sequence = index
                            it.outerSequence = position
                        },
                        basicSource = mBasicExposureSource,
                        source = listOf(
                            ExposureSource("导航栏", entity.entryName),
                        )
                    )
                    event.payload.blockId = blockData?.link ?: ""
                    exposureEventList.add(event)
                }
                mItemDataList[position].exposureEventList = exposureEventList
                (navigationAdapter as GameNavigationAdapter).exposureEventList = exposureEventList
                adapter = navigationAdapter
                (navigationAdapter as GameNavigationAdapter).setListData(navigationList as MutableList<GameNavigationEntity>?)
            }
        }
        binding.navigationRv.goneIf(navigationList.isNullOrEmpty())
    }

    private fun bindGameItemView(holder: GameItemViewHolder, position: Int) {
        val itemData = mItemDataList[position]
        val gameEntity = itemData.game
        val subjectData = gameEntity?.subjectData!!

        if (mItemDataList.size == position + 1 || mItemDataList[position + 1].game == null) {
            holder.itemView.setPadding(
                DisplayUtils.dip2px(16F), DisplayUtils.dip2px(8F),
                DisplayUtils.dip2px(16F), DisplayUtils.dip2px(16F)
            )
        } else {
            holder.itemView.setPadding(
                DisplayUtils.dip2px(16F), DisplayUtils.dip2px(8F),
                DisplayUtils.dip2px(16F), DisplayUtils.dip2px(8F)
            )
        }

        holder.bindGameItem(gameEntity, subjectData.briefStyle)
        holder.initServerType(gameEntity)

        runOnIoThread(true) {
            itemData.exposureEvent = ExposureEvent.createEventWithSourceConcat(
                gameEntity = gameEntity,
                basicSource = mBasicExposureSource,
                source = listOf(ExposureSource("专题", gameEntity.subjectName ?: subjectData.name!!)),
                eTrace = null,
                event = ExposureType.EXPOSURE
            )
        }

        DownloadItemUtils.setOnClickListener(
            mContext,
            holder.binding.downloadBtn,
            gameEntity,
            position,
            this@GameFragmentAdapter,
            StringUtils.buildString("(游戏-专题:", subjectData.name, "-列表[", (position + 1).toString(), "])"),
            location = StringUtils.buildString("游戏-专题-", subjectData.name, ":", gameEntity.name),
            traceEvent = itemData.exposureEvent
        )
        DownloadItemUtils.updateItem(
            mContext,
            gameEntity,
            GameViewHolder(holder.binding),
            subjectData.briefStyle
        )

        holder.itemView.setOnClickListener {

            DataCollectionUtils.uploadClick(mContext, subjectData.name + "-列表", "游戏-专题", gameEntity.name)

            if (gameEntity.isMiniGame()) {
                MiniGameItemHelper.launchMiniGame(gameEntity)
            } else if (gameEntity.isPluggable) {
                GameDetailActivity.startGameDetailActivity(
                    mContext,
                    gameEntity.id,
                    StringUtils.buildString("(游戏-专题:插件化-列表[", (subjectData.position).toString(), "])"),
                    itemData.exposureEvent
                )
            } else {
                GameDetailActivity.startGameDetailActivity(
                    mContext,
                    gameEntity,
                    StringUtils.buildString(
                        "(游戏-专题:",
                        subjectData.name,
                        "-列表[",
                        (subjectData.position).toString(),
                        "])"
                    ),
                    traceEvent = itemData.exposureEvent
                )
            }
        }
    }

    private fun bindHeadView(holder: GameHeadViewHolder, position: Int) {
        val column = mItemDataList[position].columnHead
        holder.bindHead(column!!)

        holder.binding.headMore.setOnClickListener {
            val buttonType = when (column.home) {
                "change" -> "换一批"
                "more" -> "更多"
                "game_list_square" -> "游戏单广场"
                else -> "全部"
            }
            val moreLink = column.moreLink
            NewLogUtils.logColumnListClickButton(
                buttonType, column.name ?: "", column.id ?: "", moreLink?.type ?: "",
                moreLink?.name ?: "", "版块", mViewModel.blockData?.name ?: ""
            )
            when (column.home) {
                "change" -> {
                    MtaHelper.onEvent("游戏专题", "换一批", column.name)
                    holder.binding.headPb.visibility = View.VISIBLE
                    mViewModel.changeSubjectGame(column.id!!)
                }

                "more" -> {
                    if (column.type == "game_list_collection") {
                        NewFlatLogUtils.logGameListCollectionClick(
                            "版块内容列表",
                            mViewModel.blockData?.text ?: "",
                            mViewModel.blockData?.link ?: "",
                            column.name ?: "",
                            column.id ?: "",
                            "更多",
                            column.moreLink?.type ?: "",
                            column.moreLink?.link ?: "",
                            column.moreLink?.text ?: ""
                        )
                        SensorsBridge.trackEvent("GameListCollectionClick", json {
                            "location" to "版块"
                            "block_name" to mViewModel.blockData?.text
                            "block_id" to mViewModel.blockData?.link
                            "game_list_collection_name" to column.name
                            "game_list_collection_id" to column.id
                            "text" to "更多"
                            "link_type" to column.moreLink?.type
                            "link_id" to column.moreLink?.link
                            "link_text" to column.moreLink?.text
                        })
                    }
                    setPageSwitchData()
                    column.moreLink?.let { link ->
                        DirectUtils.directToLinkPage(it.context, link, "(板块)", "(游戏-专题:" + column.name + "-全部)")
                    }
                }

                "game_list_square" -> {
                    NewFlatLogUtils.logGameListCollectionClick(
                        "版块内容列表",
                        mViewModel.blockData?.name ?: "",
                        mViewModel.blockData?.link ?: "",
                        column.name ?: "",
                        column.id ?: "",
                        "游戏单广场"
                    )
                    SensorsBridge.trackEvent("GameListCollectionClick", json {
                        "location" to "版块"
                        "block_name" to mViewModel.blockData?.text
                        "block_id" to mViewModel.blockData?.link
                        "game_list_collection_name" to column.name
                        "game_list_collection_id" to column.id
                        "text" to "游戏单广场"
                    })
                    DirectUtils.directToGameCollectionSquare(
                        mContext,
                        "版块内容列表",
                        mViewModel.blockData?.name ?: "",
                        collectionName = column.name ?: "",
                        collectionId = column.id ?: ""
                    )
                }

                else -> {
                    setPageSwitchData()
                    when (column.type) {
                        "column_collection" -> {
                            val blockData = mViewModel.blockData
                            MtaHelper.onEvent("游戏专题合集", "全部", column.name)
                            DirectUtils.directToColumnCollection(
                                mContext,
                                column.id!!,
                                -1,
                                "(推荐入口)",
                                "",
                                blockData?.link ?: "",
                                blockData?.text ?: "",
                            )
                            NewLogUtils.logColumnCategoryHomeButtonClick(
                                buttonType, column.name ?: "", column.id ?: "", "板块", mViewModel.blockData?.name ?: ""
                            )
                            SensorsBridge.trackColumnCollectionClick(
                                location = "版块",
                                blockName = mViewModel.blockData?.text ?: "",
                                blockId = mViewModel.blockData?.link ?: "",
                                columnCollectionName = column.name ?: "",
                                columnCollectionId = column.id ?: "",
                                position = -1,
                                gameColumnName = "",
                                gameColumnId = "",
                                text = "全部"
                            )
                        }

                        "common_collection" -> {
                            val blockData = mViewModel.blockData
                            mContext.startActivity(
                                CommonCollectionDetailActivity.getIntent(
                                    mContext,
                                    column.id ?: "",
                                    blockData?.link ?: "",
                                    blockData?.text ?: "",
                                    "板块内容列表"
                                )
                            )
                            NewLogUtils.logAccessToCommonCollectionDetail(
                                column.id ?: "",
                                column.name ?: "",
                                "板块内容列表",
                                blockData?.link ?: "",
                                blockData?.name ?: ""
                            )
                            NewLogUtils.logCommonCategoryHomeButtonClick(
                                buttonType,
                                column.name ?: "",
                                column.id ?: "",
                                "板块",
                                blockData?.name ?: ""
                            )
                            SensorsBridge.trackLinkContentCollectionClick(
                                location = "版块",
                                blockName = blockData?.text ?: "",
                                blockId = blockData?.link ?: "",
                                linkContentCollectionId = column.id ?: "",
                                linkContentCollectionName = column.name ?: "",
                                position = -1,
                                linkType = "",
                                linkId = "",
                                linkText = "",
                                text = "全部"
                            )
                        }

                        "game_list_collection" -> {
                            DirectUtils.directToGameCollectionListDetail(
                                mContext,
                                column.id ?: "",
                                column.name ?: "",
                                column.explain,
                                "版块内容列表"
                            )
                        }

                        "column_test_v2" -> {
                            //跳转到新游开测页面
                            val link = column.indexRightTopLink
                            val text = if (column.indexRightTop == "all") {
                                "全部"
                            } else {
                                "更多"
                            }
                            val location = "板块"
                            //埋点上报
                            if (text == "全部") {
                                NewFlatLogUtils.logGameTestV2MoreClick(text, location)
                                //点击进入[新游开测]详情页面
                                mContext.startActivity(GameServerTestV2Activity.getIntent(mContext, "none", "新游开测"))
                            } else {
                                //点击跳转通用链接
                                if (link != null) {
                                    val blockData = mViewModel.blockData
                                    DirectUtils.directToLinkPage(mContext, link, "新游开测", "")
                                    NewFlatLogUtils.logGameTestV2MoreClick(
                                        text,
                                        location,
                                        blockId = blockData?.link ?: "",
                                        blockName = blockData?.name ?: "",
                                        linkType = link.type ?: "",
                                        linkId = link.link ?: "",
                                        linkText = link.text ?: ""
                                    )
                                }
                            }
                        }

                        else -> {
                            if (column.indexRightTopLink != null) {
                                val link = column.indexRightTopLink!!

                                // ugly ugly ugly as hell
                                if (holder.binding.headMore.text.contains("更多")) {
                                    LogUtils.logServerTestClickMoreEvent(link.text, column.remark, link.type, link.link)
                                } else {
                                    LogUtils.logServerTestClickAllEvent(link.text, column.remark)
                                }
                                DirectUtils.directToLinkPage(mContext, link, "(游戏-专题:" + column.name + "-全部)", "")
                            } else {
                                SubjectActivity.startSubjectActivity(
                                    mContext,
                                    column.id,
                                    column.getFilterName(),
                                    column.isOrder,
                                    mBasicExposureSource,
                                    "(游戏-专题:" + column.name + "-全部)",
                                    column.subjectType
                                )
                            }
                            MtaHelper.onEvent("游戏专题", "全部", column.name)
                        }
                    }
                }
            }
        }
    }

    private fun getHomeGameCollectionExposureEventList(
        itemData: GameItemData,
        refreshCurrentPosition: Int = -1
    ): ArrayList<ExposureEvent> {
        val exposureList = arrayListOf<ExposureEvent>()
        runOnIoThread(true) {
            itemData.gameCollection?.gameCollectionItemDataList?.run {
                val currentIndex = if (itemData.gameCollection?.style?.contains("refresh") == true) {
                    refreshCurrentPosition % size
                } else -1
                forEachIndexed { index, gameCollectionItemData ->
                    if (itemData.gameCollection?.style?.contains("refresh") == false || currentIndex == index) {
                        gameCollectionItemData.homeGameCollectionItem?.let {
                            val gameCollectionSource =
                                listOf(
                                    ExposureSource("游戏单合集", itemData.gameCollection?.id ?: ""),
                                    ExposureSource("游戏单", "${it.title} + ${it.id}")
                                )
                            val gameExposureList = arrayListOf<ExposureEvent>()
                            it.games?.forEach { game ->
                                game.isAdData = it.adIconActive
                                game.outerSequence = gameCollectionItemData.outerSequence
                                game.sequence = index

                                val exposureEvent = ExposureEvent.createEventWithSourceConcat(
                                    gameEntity = game,
                                    basicSource = mBasicExposureSource,
                                    source = gameCollectionSource
                                )
                                game.exposureEvent = exposureEvent
                                gameExposureList.add(exposureEvent)
                            }
                            exposureList.addAll(gameExposureList)
                            gameCollectionItemData.exposureEvent = ExposureEvent.createEventWithSourceConcat(
                                gameEntity = GameEntity().apply {
                                    adIconActive = it.adIconActive
                                    outerSequence = gameCollectionItemData.outerSequence
                                    sequence = index
                                },
                                basicSource = mBasicExposureSource,
                                source = gameCollectionSource
                            )
                        }
                    }
                }
            }
        }
        return exposureList
    }

    private fun bindGameCollectionCarousel(holder: HomeGameCollectionCarouselViewHolder, position: Int) {
        val gameItemData = mItemDataList[position]
        gameItemData.exposureEventList = getHomeGameCollectionExposureEventList(gameItemData)
        gameItemData.gameCollection?.let {
            holder.bindGameCollectionList(
                it,
                "版块内容列表",
                mViewModel.blockData?.link ?: "",
                mViewModel.blockData?.text ?: ""
            )
        }
    }

    /**
     * 游戏单合集-横排滑动-大卡片/小卡片
     */
    private fun bindGameCollectionSlide(holder: HomeGameCollectionSlideViewHolder, position: Int) {
        val gameItemData = mItemDataList[position]
        gameItemData.exposureEventList = getHomeGameCollectionExposureEventList(gameItemData)
        gameItemData.gameCollection?.let {
            holder.bindGameCollectionSlide(
                it,
                "版块内容列表",
                mViewModel.blockData?.link ?: "",
                mViewModel.blockData?.text ?: ""
            )
        }
    }

    /**
     * 游戏单合集-刷新轮换-横排列表/竖排列表
     */
    private fun bindGameCollectionRefresh(holder: HomeGameCollectionRefreshViewHolder, position: Int) {
        val gameItemData = mItemDataList[position]
        gameItemData.exposureEventList = getHomeGameCollectionExposureEventList(gameItemData, holder.currentPosition)
        gameItemData.gameCollection?.let {
            holder.bindGameCollectionRefresh(
                it,
                "版块内容列表",
                mViewModel.blockData?.link ?: "",
                mViewModel.blockData?.text ?: ""
            ) {
                mViewModel.changeGameCollectionRefresh(it.id, true)
            }
        }
    }

    /**
     * 游戏专题-双列卡片
     */
    private fun bindGameDoubleCard(holder: DoubleCardViewHolder, position: Int) {
        val item = mItemDataList[position]
        mItemDataList[position].doubleCardColumn?.data?.run {
            val subjectEntity = item.doubleCardColumn!!
            val startPosition = item.offset
            val endPosition = item.offset + 1

            holder.bindView(this, subjectEntity.name ?: "", startPosition)

            val exposureEventList = arrayListOf<ExposureEvent>()
            runOnIoThread(true) {
                for (i in startPosition until endPosition) {
                    if (i >= size) break

                    this[i].sequence = i
                    val event = ExposureEvent.createEventWithSourceConcat(
                        gameEntity = this[i],
                        basicSource = mBasicExposureSource,
                        source = listOf(ExposureSource("专题", subjectEntity.name ?: ""))
                    )
                    this[i].exposureEvent = event
                    exposureEventList.add(event)
                }
            }
            item.exposureEventList = exposureEventList
        }
    }

    /**
     * 提问、帖子、视频帖、文章
     */
    private fun bindBigImageRecommend(holder: BigImageRecommendViewHolder, position: Int) {
        mItemDataList[position].bigImageRecommend?.run {
            holder.bindBigImageRecommend(this, "(板块)") {
                mViewModel.blockData?.let { blockData ->
                    NewFlatLogUtils.logBlockGameContentCardClick(
                        blockData.link ?: "", blockData.name ?: "",
                        it.type ?: "", it.link ?: "", it.linkText ?: ""
                    )
                }
            }
        }
    }

    /**
     * 游戏-首页大图/顶部视频
     */
    private fun bindAttachGame(holder: HomeGameItemViewHolder, position: Int) {
        mItemDataList[position].attachGame?.run {
            holder.bindAttachGame(holder, this, this@GameFragmentAdapter, mBasicExposureSource, position, "板块") {
                mItemDataList[position].exposureEvent = it
            }
        }
    }

    /**
     * 发现页
     */
    private fun bindDiscoverCard(holder: HomeDiscoverCardViewHolder, position: Int) {
        val item = mItemDataList[position]
        val discoverCard = item.discoverCard!!
        val binding = holder.binding
        val exposureEventList = arrayListOf<ExposureEvent>()
        val listExposureEventList = arrayListOf<ExposureEvent>()
        val spanCount = 3

        val snapHelper = holder.bindView(item.discoverCard!!, "版块", position, mViewModel.blockData?.name ?: "")

        val exposureClosure: (Int) -> Unit = {
            val gameList = discoverCard.games
            runOnIoThread(true) {
                listExposureEventList.clear()

                val startOffset = it * spanCount
                val endOffset = if (startOffset + spanCount >= gameList.size) {
                    gameList.size
                } else {
                    startOffset + spanCount
                }
                for (i in startOffset until endOffset) {
                    val event = ExposureEvent.createEventWithSourceConcat(
                        gameEntity = gameList[i].apply {
                            outerSequence = position
                            sequence = i
                        },
                        basicSource = mBasicExposureSource,
                        source = listOf(ExposureSource("发现", gameList[i].getTypeName()))
                    )
                    gameList[i].exposureEvent = event
                    listExposureEventList.add(event)
                }
                exposureEventList.addAll(listExposureEventList)
            }
        }
        exposureClosure(0)

        binding.recyclerView.addOnScrollListener(OnPagerSnapScrollListener(snapHelper) {
            exposureClosure(it)
        })

        item.exposureEventList = exposureEventList

    }

    /**
     * 新游开测
     */
    private fun bindGameTestV2ViewHolder(holder: HomeItemGameTestV2ViewHolder, position: Int) {
        val exposureItemData = mItemDataList.getOrNull(position)
        val data = exposureItemData?.gameTestV2Entity ?: return
        holder.bindData(data, exposureItemData, mBasicExposureSource, position)
    }

    /**
     * 游戏专题-视频横屏滑动
     */
    private fun bindHomeHorizontalSlideVideo(holder: HomeHorizontalSlideVideoListViewHolder, position: Int) {
        val item = mItemDataList[position]
        item.horizontalSlideVideo?.run {
            holder.bindView(this, "版块内容列表", mBasicExposureSource, mScrollCalculatorHelper) {
                item.exposureEventList?.add(it)
            }
        }
    }

    /**
     * 最近在玩
     */
    private fun bindRecentVGame(holder: HomeRecentVGameViewHolder, position: Int) {
        val gameItemData = mItemDataList[position]

        val exposureEventList = arrayListOf<ExposureEvent>()
        runOnIoThread(true) {
            gameItemData.recentVGame?.forEach {
                val event = ExposureEvent.createEventWithSourceConcat(
                    gameEntity = VHelper.toGameEntity(it.downloadEntity),
                    basicSource = mBasicExposureSource,
                    source = listOf(ExposureSource("最近在玩", ""))
                )
                exposureEventList.add(event)
            }
        }
        gameItemData.exposureEventList = exposureEventList

        if (!gameItemData.recentVGame.isNullOrEmpty()) {
            holder.bindView(gameItemData.recentVGame!!, false)
        }
    }

    override fun getItemCount(): Int {
        return if (mItemDataList.size > 0) mItemDataList.size + 1 else mItemDataList.size
    }

    fun notifyItemByDownload(download: DownloadEntity) {
        val data = getGameEntityByPackage(download.packageName)
        for (gameAndPosition in data) {
            if (gameAndPosition.entity != null && gameAndPosition.entity.name == download.name) {
                val entryMap = gameAndPosition.entity.getEntryMap()
                entryMap[download.platform] = download
            }
            if (getItemViewType(gameAndPosition.position) == ItemViewType.VERTICAL_SLIDE_ITEM
                || getItemViewType(gameAndPosition.position) == ItemViewType.DISCOVER_CARD
                || getItemViewType(gameAndPosition.position) == ItemViewType.COLUMN_TEST_V2
                || getItemViewType(gameAndPosition.position) == ItemViewType.HORIZONTAL_SLIDE_VIDEO
                || getItemViewType(gameAndPosition.position) == ItemViewType.GAME_SUBJECT
                || getItemViewType(gameAndPosition.position) == ItemViewType.GAME_SUBJECT_SLIDE
                || getItemViewType(gameAndPosition.position) == ItemViewType.GAME_COLLECTION_BIG_SLIDE
                || getItemViewType(gameAndPosition.position) == ItemViewType.GAME_COLLECTION_VERTICAL_REFRESH
                || getItemViewType(gameAndPosition.position) == ItemViewType.RECENT_V_GAME
            ) {
                val view = mLayoutManager?.findViewByPosition(gameAndPosition.position)
                val recyclerView = view?.findViewById<RecyclerView>(R.id.recycler_view)
                when (val adapter = recyclerView?.adapter) {
                    is GameVerticalAdapter -> adapter.notifyItemByDownload(download)
                    is RankCollectionAdapter -> adapter.notifyItemByDownload(download)
                    is DiscoverCardGameAdapter -> adapter.notifyItemByDownload(download)
                    is HomeGameTestV2GameListRvAdapter -> adapter.notifyItemByDownload(download)
                    is HomeHorizontalSlideVideoAdapter -> adapter.notifyItemByDownload(download)
                    is GameHorizontalAdapter -> adapter.notifyItemByDownload(download)
                    is GameHorizontalSlideAdapter -> adapter.notifyItemByDownload(download)
                    is HomeGameCollectionSlideAdapter -> adapter.notifyItemByDownload(download)
                    is HomeGameCollectionRefreshAdapter -> adapter.notifyItemByDownload(download)
                    is HomeRecentVGameAdapter -> adapter.notifyItemByDownload(download)
                }
            } else {
                notifyItemChanged(gameAndPosition.position)
            }
        }
    }

    fun notifyItemAndRemoveDownload(status: EBDownloadStatus) {
        val data = getGameEntityByPackage(status.packageName)
        for (gameAndPosition in data) {
            if (gameAndPosition.entity != null && gameAndPosition.entity.name == status.name) {
                gameAndPosition.entity.getEntryMap().remove(status.platform)
            }

            notifyChildItem(gameAndPosition.position, status.packageName)
        }
    }

    fun notifyChildItem(position: Int, packageName: String) {
        if (getItemViewType(position) == ItemViewType.VERTICAL_SLIDE_ITEM
            || getItemViewType(position) == ItemViewType.RANK_COLLECTION
            || getItemViewType(position) == ItemViewType.DISCOVER_CARD
            || getItemViewType(position) == ItemViewType.COLUMN_TEST_V2
            || getItemViewType(position) == ItemViewType.HORIZONTAL_SLIDE_VIDEO
            || getItemViewType(position) == ItemViewType.GAME_SUBJECT
            || getItemViewType(position) == ItemViewType.GAME_SUBJECT_SLIDE
            || getItemViewType(position) == ItemViewType.GAME_COLLECTION_BIG_SLIDE
            || getItemViewType(position) == ItemViewType.GAME_COLLECTION_VERTICAL_REFRESH
        ) {
            val view = mLayoutManager?.findViewByPosition(position)
            val recyclerView = view?.findViewById<RecyclerView>(R.id.recycler_view)
            when (val adapter = recyclerView?.adapter) {
                is RankCollectionAdapter -> adapter.notifyChildItem()
                is DiscoverCardGameAdapter -> adapter.notifyChildItem(packageName)
                is HomeGameTestV2GameListRvAdapter -> adapter.notifyChildItem(packageName)
                is HomeHorizontalSlideVideoAdapter -> adapter.notifyChildItem(packageName)
                is HomeGameCollectionSlideAdapter -> adapter.notifyChildItem()
                is HomeGameCollectionRefreshAdapter -> adapter.notifyChildItem(packageName)
                is GameHorizontalAdapter -> adapter.notifyChildItem(packageName)
                is GameHorizontalSlideAdapter -> adapter.notifyChildItem(packageName)
                else -> recyclerView?.adapter?.notifyDataSetChanged()
            }
        } else {
            notifyItemChanged(position)
        }
    }

    fun getGameEntityByPackage(packageName: String): List<GameAndPosition> {
        val positionList = ArrayList<GameAndPosition>()
        val positionMap = mViewModel.positionAndPackageMap
        for (key in positionMap.keys) {
            if (key.contains(packageName)) {
                val position = positionMap[key]!!
                if (position >= mItemDataList.size) return arrayListOf()
                val game = mItemDataList[position].game
                if (game != null) {
                    positionList.add(GameAndPosition(game, position))
                    continue
                }

                val horizontalColumn = mItemDataList[position].horizontalColumn
                if (horizontalColumn != null) {
                    for (i in horizontalColumn.data!!.indices) {
                        val entity = horizontalColumn.data!![i]
                        if (!entity.image.isNullOrEmpty()) continue
                        for (apkEntity in entity.getApk()) {
                            if (apkEntity.packageName == packageName) {
                                positionList.add(GameAndPosition(entity, position))
                            }
                        }
                    }
                    continue
                }

                val horizontalSlide = mItemDataList[position].horizontalSlide
                if (horizontalSlide != null) {
                    for (i in horizontalSlide.data!!.indices) {
                        val entity = horizontalSlide.data!![i]
                        if (!entity.image.isNullOrEmpty()) continue
                        for (apkEntity in entity.getApk()) {
                            if (apkEntity.packageName == packageName) {
                                positionList.add(GameAndPosition(entity, position))
                            }
                        }
                    }
                    continue
                }

                val verticalSlide = mItemDataList[position].verticalSlide
                if (verticalSlide != null) {
                    for (i in verticalSlide.data!!.indices) {
                        val entity = verticalSlide.data!![i]
                        if (!entity.image.isNullOrEmpty()) continue
                        for (apkEntity in entity.getApk()) {
                            if (apkEntity.packageName == packageName) {
                                positionList.add(GameAndPosition(entity, position))
                            }
                        }
                    }
                    continue
                }

                val rankCollection = mItemDataList[position].rankCollection
                if (rankCollection != null) {
                    rankCollection.columns.forEach { subjectEntity ->
                        subjectEntity.data?.forEach {
                            for (apkEntity in it.getApk()) {
                                if (apkEntity.packageName == packageName) {
                                    positionList.add(GameAndPosition(it, position))
                                }
                            }
                        }
                    }
                    continue
                }

                val discoverCard = mItemDataList[position].discoverCard
                if (discoverCard != null) {
                    discoverCard.games.forEach {
                        for (apkEntity in it.getApk()) {
                            if (apkEntity.packageName == packageName) {
                                positionList.add(GameAndPosition(it, position))
                            }
                        }
                    }
                    continue
                }
                //新游开测相关条目
                val gameTestV2 = mItemDataList[position].gameTestV2Entity
                val gameTestV2List = mHomeGameTestV2ViewModel.getDataListLiveData().value
                gameTestV2List?.forEach { gameDataWrapper ->
                    val gameEntity = gameDataWrapper.gameData
                    if (gameEntity != null) {
                        for (apkEntity in gameEntity.getApk()) {
                            if (apkEntity.packageName == packageName) {
                                positionList.add(GameAndPosition(gameEntity, position))
                            }
                        }
                    }
                }
                if (gameTestV2 != null) continue

                val horizontalSlideVideo = mItemDataList[position].horizontalSlideVideo
                if (horizontalSlideVideo != null) {
                    horizontalSlideVideo.data?.forEach {
                        for (apkEntity in it.getApk()) {
                            if (apkEntity.packageName == packageName) {
                                positionList.add(GameAndPosition(it, position))
                            }
                        }
                    }
                    continue
                }

                val gameListCollection = mItemDataList[position].gameCollection
                if (gameListCollection != null) {
                    gameListCollection.gameCollectionItemDataList.forEach { gameCollectionItemData ->
                        gameCollectionItemData.homeGameCollectionItem?.games?.forEach {
                            for (apkEntity in it.getApk()) {
                                if (apkEntity.packageName == packageName) {
                                    positionList.add(GameAndPosition(it, position))
                                }
                            }
                        }
                    }
                    continue
                }

                val attachGame = mItemDataList[position].attachGame?.linkGame
                val recentVGame = mItemDataList[position].recentVGame
                if (attachGame != null || recentVGame != null) {
                    positionList.add(GameAndPosition(null, position))
                    continue
                }

                val image = mItemDataList[position].image
                if (image != null) positionList.add(GameAndPosition(image, position))
            }
        }
        return positionList
    }

    private fun setPageSwitchData(isContent: Boolean = true) {
        PageSwitchDataHelper.pushCurrentPageData(
            hashMapOf(
                Pair(
                    PageSwitchDataHelper.PAGE_BUSINESS_TYPE,
                    if (isContent) "版块-内容列表" else "版块-推荐位"
                ),
                Pair(PageSwitchDataHelper.PAGE_BUSINESS_NAME, mViewModel.blockData?.name ?: ""),
                Pair(PageSwitchDataHelper.PAGE_BUSINESS_ID, mViewModel.blockData?.link ?: "")
            )
        )
    }

    override fun getEventByPosition(pos: Int): ExposureEvent? {
        return mItemDataList[pos].exposureEvent
    }

    override fun getEventListByPosition(pos: Int): List<ExposureEvent>? {
        if (mViewModel.blockData?.display?.slide == true || mViewModel.blockData?.display?.recommend == true) {
            return if (pos == 0) mTopViewExposureEventList else mItemDataList[pos].exposureEventList
        }
        return mItemDataList[pos].exposureEventList
    }

    fun getItemDataByPosition(position: Int): GameItemData? {
        if (position >= mItemDataList.size) return null
        return mItemDataList[position]
    }

}

data class GameAndPosition(val entity: GameEntity?, val position: Int, val index: Int = 0)
