package com.gh.gamecenter.feature.minigame

import android.view.View
import android.widget.TextView
import com.therouter.TheRouter
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.core.provider.IDirectProvider
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.minigame.wechat.WGameSubjectCPMListReportHelper
import com.gh.gamecenter.feature.utils.NewFlatLogUtils

object MiniGameItemHelper {

    /**
     * 设置已玩人数，仅支持微信小游戏显示
     */
    fun setMiniGameUsage(tv: TextView, gameEntity: GameEntity) =
        if (gameEntity.isWechatMiniGame() && gameEntity.usage > 0) {
            tv.visibility = View.VISIBLE
            tv.text = getUsageText(gameEntity.usage)
        } else {
            tv.visibility = View.GONE
        }

    private fun getUsageText(usage: Int) = when {
        usage < 10000 -> "${usage}人玩过"
        else -> {
            val value = usage * 1.0F / 10000
            "${"%.1f".format(value)}万人玩过"
        }
    }

    fun launchMiniGame(gameEntity: GameEntity) {
        launchMiniGame(
            gameEntity.miniGameAppId,
            gameEntity.miniGameType,
            gameEntity.miniGameAppPath,
            gameEntity.miniGameExtData
        )
    }

    fun launchMiniGame(miniAppId: String, miniAppType: String, miniAppPath: String = "", miniAppExtData: String = "") {
        CurrentActivityHolder.getCurrentActivity()?.let { activity ->
            val directProvider = TheRouter.get(IDirectProvider::class.java)
            if (miniAppType == Constants.QQ_MINI_GAME) {
                directProvider?.directToQQGameById(activity, miniAppId)
            } else {
                directProvider?.directToWechatGameById(activity, miniAppId, miniAppPath, miniAppExtData)
            }
        }
    }

    fun trackMiniGameClick(
        gameEntity: GameEntity,
        bottomTab: String = "",
        multiTabId: String = "",
        multiTabName: String = "",
        position: Int = -1,
        tabContent: String = "",
        customPageId: String = "",
        customPageName: String = "",
        gameColumnId: String = "",
        gameColumnName: String = "",
        location: String = "",
        columnPattern: String = "",
        searchContent: String = "",
    ) {
        when (gameEntity.miniGameType) {
            Constants.QQ_MINI_GAME -> {
                NewFlatLogUtils.logQGameClick(gameEntity.miniGameAppId, gameEntity.name)
                SensorsBridge.trackQQGameClick(
                    bottomTab = bottomTab,
                    multiTabId = multiTabId,
                    multiTabName = multiTabName,
                    position = position,
                    tabContent = tabContent,
                    customPageId = customPageId,
                    customPageName = customPageName,
                    gameColumnId = gameColumnId,
                    gameColumnName = gameColumnName,
                    location = location,
                    columnPattern = columnPattern,
                    searchContent = searchContent,
                    gameId = gameEntity.id,
                    gameName = gameEntity.name ?: "",
                )
            }
            else -> {
                SensorsBridge.trackWechatGameClick(
                    bottomTab = bottomTab,
                    multiTabId = multiTabId,
                    multiTabName = multiTabName,
                    position = position,
                    tabContent = tabContent,
                    customPageId = customPageId,
                    customPageName = customPageName,
                    gameColumnId = gameColumnId,
                    gameColumnName = gameColumnName,
                    location = location,
                    columnPattern = columnPattern,
                    searchContent = searchContent,
                    gameId = gameEntity.id,
                    gameName = gameEntity.name ?: ""
                )
                if (gameEntity.miniGameType == Constants.WECHAT_MINI_GAME_CPM) {
                    WGameSubjectCPMListReportHelper.reportClick(gameEntity)
                }
            }
        }
    }
}