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

import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.common.util.AdHelper
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.RankCollectionListCustomBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.IGameChangedNotifier
import com.gh.gamecenter.home.custom.adapter.CustomRankCollectionAdapter
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.GameSubjectCollectionEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem

/**
 * 专题合集-排行榜
 */
class CustomRankCollectionViewHolder(
    viewModel: CustomPageViewModel,
    val binding: RankCollectionListCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

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

    private val _adapter by lazy {
        CustomRankCollectionAdapter(itemView.context, childEventHelper)
    }

    override val gameChangedNotifier: IGameChangedNotifier
        get() = _adapter

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSubjectCollectionItem) {
            if (binding.recyclerView.adapter == null) {
                binding.recyclerView.isNestedScrollingEnabled = false
                binding.recyclerView.layoutManager =
                    LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false)
                binding.recyclerView.adapter = _adapter
            }

            runOnIoThread(isLightWeightTask = true) {
                val collection = item.data
                collection.data.forEach { customSubject ->
                    customSubject.games.forEachIndexed { index, gameEntity ->
                        val source = listOf(
                            ExposureSource("专题合集", "${item.data.name}+${item.componentStyle}+${item.data.id}"),
                            ExposureSource(
                                "专题",
                                "${customSubject.title}+${customSubject.styleChinese}+${customSubject.id}"
                            )
                        )
                        gameEntity.pageLevelString = pageLocation.pageLevelString
                        gameEntity.exposureEvent = createExposureEvent(
                            gameEntity,
                            source,
                            pageConfigure.exposureSourceList,
                            index,
                            item.componentPosition,
                            createTrackData(item)
                        )

                        if (!gameEntity.adGroupId.isEmpty() && !gameEntity.isAdRequestReported) {
                            AdHelper.reportAdRequest(gameEntity.exposureEvent!!)
                            gameEntity.isAdRequestReported = true
                        }
                    }
                }
            }

            _adapter.setData(item)
        }
    }
}