package com.gh.common.util

import android.net.Uri
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.EnvHelper

/**
 * 活动相关数据统计辅助类
 */
object ActivityCountHelper {

    private const val MAX_STAY_TIME = 320
    private val ACTIVITY_URL_COMMON_PREF by lazy {
        if (EnvHelper.isDevEnv) Constants.ACTIVITY_DETAIL_ADDRESS_DEV else Constants.ACTIVITY_DETAIL_ADDRESS
    }
    private var mStayTimeArray = arrayOf(5, 10, 20, 40, 80, 160)

    @JvmStatic
    fun postActivityPageEnter(url: String?, entrance: String) {
        if (url?.contains(ACTIVITY_URL_COMMON_PREF) == true) {
            val uri = Uri.parse(url)
            val activityId = uri.getQueryParameter("id") ?: return
            NewFlatLogUtils.logActivityPageEnter(transformEntrance(entrance), activityId)
        }
    }

    private fun transformEntrance(originalEntrance: String): String {
        return when {
            originalEntrance.contains("(启动广告)") -> "开屏页"
            originalEntrance.contains(EntranceConsts.ENTRANCE_WELCOME) -> "首页弹窗"
            originalEntrance.contains("首页tab") -> "首页二级导航"
            originalEntrance.contains("论坛-活动") -> "活动tab"
            originalEntrance.contains("新首页-轮播图") -> "首页banner"
            originalEntrance.contains("资讯") -> "资讯广场"
            else -> originalEntrance
        }
    }

    fun handleStayTime(url: String, stayTime: Int) {
        if (url.contains(ACTIVITY_URL_COMMON_PREF)) {
            val uri = Uri.parse(url)
            val activityId = uri.getQueryParameter("id") ?: return
            when {
                stayTime % MAX_STAY_TIME == 0 -> NewFlatLogUtils.logActivityPageView(
                    MAX_STAY_TIME,
                    activityId
                )
                mStayTimeArray.contains(stayTime) -> NewFlatLogUtils.logActivityPageView(
                    stayTime,
                    activityId
                )
            }
        }
    }
}