package com.gh.gamecenter.home.custom

import com.gh.common.util.AdHelper
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.feature.entity.CustomPageTrackData
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureConstants
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem

fun createExposureEvent(
    game: GameEntity?,
    source: List<ExposureSource>,
    base: List<ExposureSource>,
    childPosition: Int,
    position: Int,
    customPageTrackData: CustomPageTrackData
): ExposureEvent {
    game?.sequence = childPosition
    game?.outerSequence = position
    game?.customPageTrackData = customPageTrackData
    game?.pageLevelString = customPageTrackData.pageLocation.pageLevelString
    game?.subPageCode = ExposureConstants.CUSTOM_PAGE
    game?.subPageId = customPageTrackData.pageLocation.pageId
    val event = ExposureEvent.createEventWithSourceConcat(
        gameEntity = game,
        basicSource = base,
        source = source
    )
    game?.exposureEvent = event
    return event
}

fun fillExposureInSubjectCollection(
    item: CustomSubjectCollectionItem,
    base: List<ExposureSource>,
    customPageTrackData: CustomPageTrackData
) {
    runOnIoThread(isLightWeightTask = true) {
        item.data.data.forEachIndexed { index, customSubject ->
            val source = if (item.isSubjectCollection) {
                listOf(
                    ExposureSource("专题合集", "${item.data.name}+${item.componentStyle}+${item.data.id}"),
                    ExposureSource(
                        "专题",
                        "${customSubject.title}+${customSubject.styleChinese}+${customSubject.id}"
                    )
                )
            } else {
                listOf(
                    ExposureSource("游戏单合集", "${item.data.name}+${item.componentStyle}+${item.data.id}"),
                    ExposureSource("游戏单", "${customSubject.title}+${customSubject.id}")
                )
            }
            customSubject.games.forEach { game ->
                game.pageLevelString = customPageTrackData.pageLocation.pageLevelString
                game.isAdData = customSubject.adIconActive
                game.exposureEvent = createExposureEvent(
                    game,
                    source,
                    base,
                    index,
                    item.componentPosition,
                    customPageTrackData
                )
                game.subPageCode = ExposureConstants.CUSTOM_PAGE
                game.subPageId = customPageTrackData.pageLocation.pageId
                game.exposureSource = game.exposureEvent?.source

                if (game.adGroupId.isNotEmpty() && !game.isAdRequestReported) {
                    AdHelper.reportAdRequest(game.exposureEvent!!)
                    game.isAdRequestReported = true
                }
            }
        }
    }
}
