package com.gh.gamecenter.home.custom.tracker

import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.dsp.DspReportHelper
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.home.custom.model.CustomGameItem
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSplitSubjectItem
import com.gh.gamecenter.home.custom.model.CustomSubjectItem
import com.gh.gamecenter.feature.minigame.MiniGameItemHelper
import org.json.JSONObject

class SubjectTracker(private val pageLocation: PageLocation) {

    fun trackColumnClick(
        item: CustomPageItem,
        game: GameEntity?,
        text: String,
        buttonType: String = "",
        link: LinkEntity? = null
    ) {
        val subject = when {
            item is CustomGameItem -> item.linkColumn
            item is CustomSubjectItem -> item.data
            item is CustomSplitSubjectItem -> item.data
            else -> null
        }
        SensorsBridge.trackColumnClick(
            pageLocation.bottomTab,
            pageLocation.severalTabPageId,
            pageLocation.severalTabPageName,
            pageLocation.tabPosition,
            pageLocation.tabContent,
            pageLocation.pageId,
            pageLocation.pageName,
            link?.type ?: "",
            link?.link ?: "",
            link?.linkText ?: "",
            subject?.id ?: "",
            subject?.name ?: "",
            game?.id ?: "",
            game?.name ?: "",
            "自定义页面",
            item.componentStyle,
            text,
            buttonType,
            adGroupId = game?.adGroupId ?: "",
        )
    }

    fun trackMiniGameClick(item: CustomPageItem, game: GameEntity) {
        val subject = when {
            item is CustomGameItem -> item.linkColumn
            item is CustomSubjectItem -> item.data
            item is CustomSplitSubjectItem -> item.data
            else -> null
        }
        MiniGameItemHelper.trackMiniGameClick(
            gameEntity = game,
            pageLocation.bottomTab,
            pageLocation.severalTabPageId,
            pageLocation.severalTabPageName,
            pageLocation.tabPosition,
            pageLocation.tabContent,
            pageLocation.pageId,
            pageLocation.pageName,
            subject?.id ?: "",
            subject?.name ?: "",
            "自定义页面",
            item.componentStyle,
            "",
        )
    }

    fun trackQqGameRecentlyPlayedClick(text: String, game: GameEntity?) {
        SensorsBridge.trackQqGameRecentlyPlayedClick(
            text,
            pageLocation.bottomTab,
            pageLocation.severalTabPageId,
            pageLocation.severalTabPageName,
            pageLocation.tabPosition,
            pageLocation.tabContent,
            pageLocation.pageId,
            pageLocation.pageName,
            game?.id ?: "",
            game?.name ?: ""
        )
    }

    fun trackDspGameClick(
        item: CustomPageItem,
        game: GameEntity,
        text: String,
        location: String
    ): JSONObject? {
        val subject = (item as? CustomSplitSubjectItem)?.data
            ?: (item as? CustomSubjectItem)?.data
            ?: return null

        // 先尝试上报 show 再上报 click，保证先 show 后 click (已在 report 方法中进行简单去重处理)
        DspReportHelper.report(game.showUrl)
        DspReportHelper.report(game.clickUrl)

        val json = SensorsBridge.trackDspAdClick(
            pageId = pageLocation.pageId,
            pageBusinessId = GlobalActivityManager.getCurrentPageEntity().pageBusinessId,
            lastPageId = GlobalActivityManager.getLastPageEntity().pageId,
            lastPageName = GlobalActivityManager.getLastPageEntity().pageName,
            lastPageBusinessId = GlobalActivityManager.getLastPageEntity().pageBusinessId,
            location = location,
            bottomTab = pageLocation.bottomTab,
            severalTabPageName = pageLocation.severalTabPageName,
            severalTabPageId = pageLocation.severalTabPageId,
            position = pageLocation.tabPosition,
            tabContent = pageLocation.tabContent,
            customPageName = pageLocation.pageName,
            customPageId = pageLocation.pageId,
            gameColumnName = subject.name ?: "",
            gameColumnId = subject.id ?: "",
            searchContent = "",
            adId = game.dspAdId ?: "",
            interactionType = game.interactionType.toString(),
            wechatAppid = game.miniGameAppId,
            gameName = game.name ?: "",
            text = text
        )

        return json
    }
}