package com.gh.gamecenter.gamecollection.square

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.gh.common.exposure.IExposable
import com.gh.common.util.CheckLoginUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.ScrollEventListener
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.GameCollectionSquareItemBinding
import com.gh.gamecenter.databinding.ItemGameCollectionFilterBinding
import com.gh.gamecenter.databinding.ItemGameCollectionHeaderBinding
import com.gh.gamecenter.entity.AmwayCommentEntity
import com.gh.gamecenter.entity.CarouselEntity
import com.gh.gamecenter.entity.GamesCollectionEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureType
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailActivity
import com.gh.gamecenter.gamecollection.tag.GameCollectionTagSelectActivity
import com.lightgame.view.NoScrollableViewPager
import java.lang.ref.WeakReference

class GameCollectionSquareAdapter(
    context: Context,
    private val mLifecycleOwner: LifecycleOwner,
    private val mIsHome: Boolean = false,
    private val mFragment: GameCollectionSquareFragment,
    private val mViewModel: GameCollectionSquareViewModel,
    private var mBasicExposureSource: List<ExposureSource>,
    private val mOuterSequence: Int = -1,
    private val mRefreshCallback: () -> Unit
) :
    ListAdapter<GameCollectionListItemData>(context), IExposable {
    private val mPosterWidth = DisplayUtils.getScreenWidth() - 32F.dip2px()

    override fun setListData(updateData: MutableList<GameCollectionListItemData>?) {
        if (updateData == null) {
            mEntityList = ArrayList()
            notifyDataSetChanged()
            return
        }

        // 避免首页游戏单广场刷新列表后出现异常跳转位置
        if (mIsHome && mEntityList.size == 2 && updateData.size > mEntityList.size) {
            mEntityList = ArrayList(updateData)
            notifyItemRangeInserted(2, updateData.size - 1)
            return
        }

        // 避免手动刷新列表时出现异常动画
        if (mEntityList != null && mEntityList.size > updateData.size) {
            mEntityList = ArrayList(updateData)
            notifyDataSetChanged()
            return
        }

        calculateDiff(updateData)
    }

    fun setAmwayList(amwayList: List<AmwayCommentEntity>) {
        if (mIsHome && mEntityList.isNotEmpty() && mEntityList[0].amwayListItem.isNullOrEmpty()) {
            mEntityList[0].amwayListItem = amwayList
            notifyItemChanged(0)
        }
    }

    fun setBannerList(bannerList: List<CarouselEntity>) {
        if (mIsHome && mEntityList.isNotEmpty() && mEntityList[0].carouselListItem.isNullOrEmpty()) {
            mEntityList[0].carouselListItem = bannerList
            notifyItemChanged(0)
        }
    }

    fun setHotListTabName(tabName: String) {
        if (mIsHome && mEntityList.isNotEmpty() && mEntityList[0].hotListTabName.isEmpty()) {
            mEntityList[0].hotListTabName = tabName
            notifyItemChanged(0)
        }
    }

    override fun areItemsTheSame(oldItem: GameCollectionListItemData?, newItem: GameCollectionListItemData?): Boolean {
        if (oldItem?.isHeaderItem == newItem?.isHeaderItem) return true
        if (oldItem?.isFilterItem == newItem?.isFilterItem) return true
        if (oldItem?.isGameCollectionItem == newItem?.isGameCollectionItem) return true
        return false
    }

    override fun areContentsTheSame(
        oldItem: GameCollectionListItemData?,
        newItem: GameCollectionListItemData?
    ): Boolean {
        if (oldItem?.isHeaderItem == newItem?.isHeaderItem) {
            return (oldItem?.carouselListItem == newItem?.carouselListItem) && (oldItem?.amwayListItem == newItem?.amwayListItem)
        }
        if (oldItem?.isFilterItem == newItem?.isFilterItem) return true
        if (oldItem?.isGameCollectionItem == newItem?.isGameCollectionItem) {
            return oldItem?.gameCollectionItem == newItem?.gameCollectionItem
        }
        return false
    }


    override fun getItemViewType(position: Int): Int {
        if (mIsHome) {
            if (position == 0) return ItemViewType.ITEM_HEADER
            if (position == 1) return ITEM_FILTER
            if (position == itemCount - 1) return ItemViewType.ITEM_FOOTER
            return ItemViewType.ITEM_BODY
        } else {
            if (position == itemCount - 1) return ItemViewType.ITEM_FOOTER
            return ItemViewType.ITEM_BODY
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_HEADER -> GameCollectionHeaderItemViewHolder(parent.toBinding())
            ITEM_FILTER -> GameCollectionFilterItemViewHolder(parent.toBinding())
            ItemViewType.ITEM_BODY -> GameCollectionSquareItemViewHolder(parent.toBinding())
            ItemViewType.ITEM_FOOTER -> FooterViewHolder(
                mLayoutInflater.inflate(
                    com.gh.gamecenter.common.R.layout.refresh_footerview,
                    parent,
                    false
                )
            )

            else -> GameCollectionSquareItemViewHolder(parent.toBinding())
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is GameCollectionHeaderItemViewHolder -> {
                val itemData = mEntityList[position]
                holder.bindHeader(mLifecycleOwner, itemData, mBasicExposureSource, mViewModel)
            }

            is GameCollectionFilterItemViewHolder -> holder.bindFilter(mFragment, mViewModel, mRefreshCallback)
            is GameCollectionSquareItemViewHolder -> {
                val itemData = mEntityList[position]
                val filterTagName = mViewModel.selectedTagName
                val exposureEventList = arrayListOf<ExposureEvent>()
                val exposureSource = listOf(
                    ExposureSource(
                        "游戏单",
                        "${itemData.gameCollectionItem?.title} + ${itemData.gameCollectionItem?.id} + ${
                            GameCollectionSquareViewModel.getOrderChineseByName(
                                mViewModel.view
                            )
                        } + $filterTagName"
                    )
                )

                itemData.gameCollectionItem?.games?.take(3)?.forEachIndexed { index, game ->
                    exposureEventList.add(
                        ExposureEvent.createEventWithSourceConcat(
                            game.toGameEntity().apply {
                                isAdData = itemData.gameCollectionItem?.adIconActive ?: false
                                outerSequence = mOuterSequence
                                sequence = itemData.gameStartPosition + index + 1
                            },
                            mBasicExposureSource,
                            exposureSource
                        )
                    )
                }
                itemData.exposureEventList = exposureEventList
                itemData.gameCollectionItem?.let {
                    holder.bindGameCollection(mViewModel, it, itemData, ArrayList(mBasicExposureSource).apply {
                        addAll(exposureSource)
                    })
                }
            }
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
                holder.hint.setTextColor(ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.text_B3B3B3))
                val lp = holder.itemView.layoutParams as RecyclerView.LayoutParams
                lp.height = DisplayUtils.dip2px(48F)
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT
                holder.itemView.layoutParams = lp
            }
        }
    }

    override fun getItemCount() =
        if (mEntityList.isNullOrEmpty()) 0 else if (mIsHome && mEntityList.size == 2) mEntityList.size else mEntityList.size + 1

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder is GameCollectionHeaderItemViewHolder) {
            holder.onViewAttach(mLifecycleOwner.lifecycle)
        }
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if (holder is GameCollectionHeaderItemViewHolder) {
            holder.onViewDetach(mLifecycleOwner.lifecycle)
        }
    }

    class GameCollectionHeaderItemViewHolder(val binding: ItemGameCollectionHeaderBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private lateinit var mBannerAdapter: GameCollectionBannerAdapter
        private lateinit var mSnapHelper: PagerSnapHelper
        private lateinit var mBannerLayoutManager: LinearLayoutManager
        private val mLooperHandle = LooperHandle(this)
        private val mSlideLooperKey = 111
        private val mLifecycleObserver = object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                startAutoPlay()
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                stopAutoPlay()
            }
        }

        fun bindHeader(
            lifecycleOwner: LifecycleOwner,
            itemData: GameCollectionListItemData,
            mBasicExposureSource: List<ExposureSource>,
            viewModel: GameCollectionSquareViewModel
        ) {
            val context = binding.root.context
            val bannerList = itemData.carouselListItem ?: emptyList()
            val amwayListItem = itemData.amwayListItem

            if (::mBannerAdapter.isInitialized) {
                mBannerAdapter.submitList(bannerList, amwayListItem)
                binding.tabNameTv.text = itemData.hotListTabName
                itemData.exposureEventList = getExposureEventList(bannerList, mBasicExposureSource)

                if (mBannerAdapter.mBannerList.size != bannerList.size) {
                    binding.bannerIndicator.run {
                        pageSize = mBannerAdapter.getActualSize()
                        notifyDataChanged()
                    }
                    if (mBannerAdapter.getActualSize() > 1) {
                        binding.bannerRv.scrollToPosition(mBannerAdapter.getActualFirstPositionInCenter())
                    }
                    startAutoPlay()
                }
                return
            }
            mSnapHelper = PagerSnapHelper()
            mBannerLayoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            mBannerAdapter = GameCollectionBannerAdapter(
                context,
                lifecycleOwner,
                viewModel,
                bannerList,
                amwayListItem,
                "顶部tab-游戏单广场",
                mBasicExposureSource
            )
            binding.bannerIndicator.run {
                pageSize = mBannerAdapter.getActualSize()
                notifyDataChanged()
            }
            binding.bannerRv.run {
                adapter = mBannerAdapter
                layoutManager = mBannerLayoutManager
                mSnapHelper.attachToRecyclerView(this)
                addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
                    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                        val viewPager = parent.parent.parent.parent.parent.parent.parent.parent
                        when (e.action) {
                            MotionEvent.ACTION_DOWN -> {
                                if (viewPager is NoScrollableViewPager) {
                                    viewPager.setScrollable(false)
                                }
                            }
                            MotionEvent.ACTION_UP -> {
                                if (viewPager is NoScrollableViewPager) {
                                    viewPager.setScrollable(true)
                                }
                            }
                        }

                        val isStop = e.action == MotionEvent.ACTION_DOWN || e.action == MotionEvent.ACTION_MOVE
                        if (isStop) stopAutoPlay() else startAutoPlay()
                        return false
                    }
                })
                addOnScrollListener(ScrollEventListener(this).apply {
                    registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                            binding.bannerIndicator.onPageScrolled(
                                mBannerAdapter.getActualPosition(position),
                                positionOffset
                            )
                        }
                    })
                })
                if (mBannerAdapter.getActualSize() > 1) {
                    scrollToPosition(mBannerAdapter.getActualFirstPositionInCenter())
                }
                startAutoPlay()
            }
            binding.tabNameTv.text = itemData.hotListTabName
            binding.hotListCv.setOnClickListener {
                DirectUtils.directToGameCollectionHotList(context, "首页tab栏")
            }
            itemData.exposureEventList = getExposureEventList(bannerList, mBasicExposureSource)
        }

        private fun getExposureEventList(bannerList: List<CarouselEntity>, mBasicExposureSource: List<ExposureSource>) =
            arrayListOf<ExposureEvent>().apply {
                bannerList.forEachIndexed { index, entity ->
                    val exposureEvent = ExposureEvent(
                        payload = ExposureEntity(
                            carouselId = entity.id,
                            sequence = index
                        ),
                        source = ArrayList(mBasicExposureSource).apply {
                            add(ExposureSource(k = "轮播图"))
                        },
                        event = ExposureType.EXPOSURE
                    )
                    add(exposureEvent)
                }
            }

        private fun scrollToNextPage() {
            val snapView = mSnapHelper.findSnapView(mBannerLayoutManager)
            if (snapView != null) {
                mBannerLayoutManager.smoothScrollToPosition(
                    binding.bannerRv,
                    null,
                    mBannerLayoutManager.getPosition(snapView) + 1
                )
            }
        }

        private fun startAutoPlay() {
            if (mBannerAdapter.getActualSize() <= 1) return
            stopAutoPlay()
            mLooperHandle.sendEmptyMessageDelayed(mSlideLooperKey, GameCollectionSquareFragment.BANNER_LOOP_TIME)
        }

        private fun stopAutoPlay() {
            mLooperHandle.removeMessages(mSlideLooperKey)
        }

        fun onViewAttach(lifecycle: Lifecycle) {
            lifecycle.addObserver(mLifecycleObserver)
        }

        fun onViewDetach(lifecycle: Lifecycle) {
            lifecycle.removeObserver(mLifecycleObserver)
        }

        class LooperHandle(viewHolder: GameCollectionHeaderItemViewHolder) : Handler(Looper.getMainLooper()) {
            private val mWeakReference: WeakReference<GameCollectionHeaderItemViewHolder> = WeakReference(viewHolder)

            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                val viewHolder = mWeakReference.get()
                if (viewHolder != null && msg.what == viewHolder.mSlideLooperKey) {
                    viewHolder.scrollToNextPage()
                    viewHolder.startAutoPlay()
                }
            }
        }
    }

    class GameCollectionFilterItemViewHolder(val binding: ItemGameCollectionFilterBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindFilter(
            fragment: GameCollectionSquareFragment,
            viewModel: GameCollectionSquareViewModel,
            refreshCallback: () -> Unit
        ) {
            binding.orderSfv.run {
                setContainerBackground(com.gh.gamecenter.common.R.drawable.button_round_f5f5f5.toDrawable(context))
                setIndicatorBackground(R.drawable.bg_game_collection_sfv_indicator.toDrawable(context))
                setTextColor(
                    com.gh.gamecenter.common.R.color.text_secondary.toColor(context),
                    com.gh.gamecenter.common.R.color.text_tertiary.toColor(context)
                )
            }
            binding.orderSfv.post {
                // 防止标签文字过长
                binding.tagTv.maxWidth =
                    DisplayUtils.getScreenWidth() - 80F.dip2px() - binding.orderSfv.width
            }

            val orderPosition = GameCollectionSquareViewModel.getOrderPositionByName(viewModel.view)
            if (binding.orderSfv.getItemList().isEmpty()) {
                binding.orderSfv.setItemList(listOf("推荐", "热门", "最新"), orderPosition)
            } else if (binding.orderSfv.getCurrentPosition() != orderPosition) {
                binding.orderSfv.setChecked(orderPosition)
            }

            binding.orderSfv.setOnCheckedCallback { position ->
                viewModel.view = GameCollectionSquareViewModel.getOrderNameByPosition(position)
                SensorsBridge.trackEvent(
                    "GameCollectSquareSortClick",
                    "text",
                    GameCollectionSquareViewModel.getOrderChineseByName(viewModel.view)
                )
                refreshCallback()
            }

            binding.tagTv.text = viewModel.selectedTagEntity?.name ?: "全部标签"
            binding.tagFilter.background =
                if (viewModel.selectedTagEntity != null) R.drawable.bg_game_collection_tag_select.toDrawable() else R.drawable.bg_game_collection_tag_unselect.toDrawable()

            binding.tagFilter.setOnClickListener {
                NewLogUtils.logEnterGameCollectionTag("首页tab栏")
                SensorsBridge.trackEvent(
                    "GameCollectTagEnter",
                    "label_category",
                    viewModel.selectedTagCategory,
                    "label_name",
                    viewModel.selectedTagEntity?.name ?: "",
                    "label_id",
                    viewModel.selectedTagEntity?.id ?: ""
                )
                fragment.startActivityForResult(
                    GameCollectionTagSelectActivity.getIntent(
                        fragment.requireContext(),
                        true,
                        viewModel.selectedTagEntity
                    ),
                    GameCollectionSquareFragment.REQUEST_SELECT_TAG
                )
            }
        }
    }

    inner class GameCollectionSquareItemViewHolder(val binding: GameCollectionSquareItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bindGameCollection(
            viewModel: GameCollectionSquareViewModel,
            gamesCollectionEntity: GamesCollectionEntity,
            itemData: GameCollectionListItemData,
            exposureSource: List<ExposureSource>
        ) {
            binding.run {
                val context = root.context
                poster.setTag(ImageUtils.TAG_TARGET_WIDTH, mPosterWidth)
                ImageUtils.display(poster, gamesCollectionEntity.cover)
                ImageUtils.display(userIv, gamesCollectionEntity.user?.icon)
                titleTv.text = gamesCollectionEntity.title
                gamesCollectionEntity.games?.take(3)?.forEachIndexed { index, game ->
                    when (index) {
                        0 -> iconIvOne.displayGameIcon(game.toGameEntity())
                        1 -> iconIvTwo.displayGameIcon(game.toGameEntity())
                        2 -> iconIvThree.displayGameIcon(game.toGameEntity())
                    }
                }
                gamesCollectionEntity.games?.size?.let {
                    iconIvOne.goneIf(it == 0)
                    iconIvTwo.goneIf(it < 2)
                    iconIvThree.goneIf(it < 3)
                }
                gamesCollectionEntity.count?.game?.let {
                    countTv.goneIf(it < 4 || gamesCollectionEntity.games?.size == 0)
                    countTv.text = "+ ${it - 3}"
                }
                gamesCollectionEntity.count?.hot?.let {
                    hotTv.text = if (it > 10000) "10000+" else it.toString()
                }
                userTv.text = gamesCollectionEntity.user?.name
                playedGameProgress.max = gamesCollectionEntity.count?.game ?: 0
                playedGameProgress.progress = gamesCollectionEntity.count?.playedGame ?: 0
                playedGameTv.text =
                    "玩过 ${gamesCollectionEntity.count?.playedGame ?: 0} / ${gamesCollectionEntity.count?.game ?: 0} 款"
                stampIv.goneIf(gamesCollectionEntity.stamp.isEmpty())
                stampIv.setBackgroundResource(if (gamesCollectionEntity.stamp == "official") R.drawable.ic_official else R.drawable.ic_chosen)
                tagContainer.removeAllViews()
                if (!gamesCollectionEntity.tags.isNullOrEmpty()) {
                    for ((index, tagEntity) in gamesCollectionEntity.tags!!.withIndex()) {
                        // 添加分隔线
                        if (index != 0) tagContainer.addView(View(context).apply {
                            layoutParams = ViewGroup.LayoutParams(1F.dip2px(), 14F.dip2px())
                            setBackgroundColor(Color.parseColor("#33FFFFFF"))
                        })
                        // 添加标签
                        tagContainer.addView(TextView(context).apply {
                            layoutParams = LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT
                            )
                            setPadding(if (index == 0) 0 else 6F.dip2px(), 0, 6F.dip2px(), 0)
                            text = tagEntity.name
                            setTextColor(com.gh.gamecenter.common.R.color.white.toColor())
                            textSize = 10F
                        })
                    }
                }
                tagContainer.visibleIf(!CheckLoginUtils.isLogin() || gamesCollectionEntity.count?.game == 0)
                playedGamesContainer.visibleIf(CheckLoginUtils.isLogin() && gamesCollectionEntity.count?.game != 0)
                adLabelTv.goneIf(!gamesCollectionEntity.adIconActive)
                iconIvOne.setOnClickListener {
                    val game = gamesCollectionEntity.games?.firstOrNull()
                    NewLogUtils.logClickGameCollectionGameIcon(
                        gamesCollectionEntity.title,
                        gamesCollectionEntity.id,
                        game?.name ?: "",
                        game?.id ?: ""
                    )
                    game?.id?.let { id ->
                        GameDetailActivity.startGameDetailActivity(
                            context,
                            id,
                            BaseActivity.mergeEntranceAndPath(viewModel.entrance, "游戏单广场"),
                            itemData.exposureEventList?.safelyGetInRelease(0)
                        )
                    }
                }
                iconIvTwo.setOnClickListener {
                    val game = gamesCollectionEntity.games?.get(1)
                    NewLogUtils.logClickGameCollectionGameIcon(
                        gamesCollectionEntity.title,
                        gamesCollectionEntity.id,
                        game?.name ?: "",
                        game?.id ?: ""
                    )
                    game?.id?.let { id ->
                        GameDetailActivity.startGameDetailActivity(
                            context,
                            id,
                            BaseActivity.mergeEntranceAndPath(viewModel.entrance, "游戏单广场"),
                            itemData.exposureEventList?.safelyGetInRelease(1)
                        )
                    }
                }
                iconIvThree.setOnClickListener {
                    val game = gamesCollectionEntity.games?.get(2)
                    NewLogUtils.logClickGameCollectionGameIcon(
                        gamesCollectionEntity.title,
                        gamesCollectionEntity.id,
                        game?.name ?: "",
                        game?.id ?: ""
                    )
                    game?.id?.let { id ->
                        GameDetailActivity.startGameDetailActivity(
                            context,
                            id,
                            BaseActivity.mergeEntranceAndPath(viewModel.entrance, "游戏单广场"),
                            itemData.exposureEventList?.safelyGetInRelease(2)
                        )
                    }
                }
                userContainer.setOnClickListener {
                    NewLogUtils.logClickGameCollectionAuthor(gamesCollectionEntity.title, gamesCollectionEntity.id)
                    DirectUtils.directToHomeActivity(
                        context,
                        gamesCollectionEntity.user?.id,
                        0,
                        viewModel.entrance,
                        "游戏单广场"
                    )
                }
                root.setOnClickListener {
                    NewLogUtils.logEnterGameCollectionDetail(gamesCollectionEntity.title, gamesCollectionEntity.id)
                    context.startActivity(
                        GameCollectionDetailActivity.getIntent(
                            context,
                            gamesCollectionEntity.id,
                            isFromSquare = true,
                            exposureSourceList = ArrayList(exposureSource),
                            entrance = "游戏单广场"
                        )
                    )
                }
            }
        }
    }

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

    override fun getEventListByPosition(pos: Int): List<ExposureEvent>? {
        return mEntityList[pos].exposureEventList
    }

    override fun loadChange(status: LoadStatus?) {
        when (status) {
            LoadStatus.INIT_OVER, LoadStatus.LIST_OVER -> {
                mIsOver = true
                mIsNetworkError = false
                mIsLoading = false
            }
            LoadStatus.LIST_FAILED -> {
                mIsOver = false
                mIsNetworkError = true
                mIsLoading = false
            }
            LoadStatus.LIST_LOADED -> {
                mIsNetworkError = false
                mIsOver = false
                mIsLoading = false
            }
            LoadStatus.LIST_LOADING -> {
                mIsNetworkError = false
                mIsOver = false
                mIsLoading = true
            }
            LoadStatus.INIT -> {
                if (mEntityList != null) mEntityList.removeAll { it.isGameCollectionItem }
                mIsNetworkError = false
                mIsOver = false
                mIsLoading = false
                notifyDataSetChanged()
                return
            }

            else -> {
                //do nothing
            }
        }

        if (itemCount > 0 && status != LoadStatus.LIST_LOADED) notifyItemChanged(itemCount - 1)
    }

    companion object {
        private const val ITEM_FILTER = 103
    }
}