package com.gh.gamecenter.home.custom

import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_GAME_COLLECT_ID
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_GAME_COLLECT_TITLE
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_GAME_ID
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_GAME_NAME
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_LINK_ID
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_LINK_TEXT
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_LINK_TYPE
import com.gh.gamecenter.common.utils.SensorsBridge.KEY_MONGOLD_ID
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem

class CustomPageTracker(val pageLocation: PageLocation) {

    fun trackGameListCollectionClickWithMore(item: CustomSubjectCollectionItem, link: LinkEntity) {
        val otherParams = HashMap<String, Any>().apply {
            put(KEY_LINK_ID, link.link ?: "")
            put(KEY_LINK_TYPE, link.type ?: "")
            put(KEY_LINK_TEXT, link.text ?: "")
        }
        trackGameListCollectionClick(item, "更多", otherParams)
    }

    fun trackGameListCollectionClickWithGameListSquare(item: CustomSubjectCollectionItem) {
        trackGameListCollectionClick(item, "游戏单广场")
    }

    fun trackGameListCollectionClickWithUser(item: CustomSubjectCollectionItem, userId: String) {
        val otherParams = hashMapOf<String, Any>(
            KEY_MONGOLD_ID to userId
        )
        trackGameListCollectionClick(item, "个人主页", otherParams)
    }

    fun trackGameListCollectionClickWithGame(item: CustomSubjectCollectionItem, gameId: String, gameName: String?) {
        val otherParams = hashMapOf<String, Any>(
            KEY_GAME_ID to gameId,
            KEY_GAME_NAME to (gameName ?: "")
        )
        trackGameListCollectionClick(item, "游戏", otherParams)
    }

    fun trackGameListCollectionClickWithGameCollection(
        item: CustomSubjectCollectionItem,
        gameCollectId: String,
        gameCollectTitle: String

    ) {
        val otherParams = hashMapOf<String, Any>(
            KEY_GAME_COLLECT_ID to gameCollectId,
            KEY_GAME_COLLECT_TITLE to gameCollectTitle
        )
        trackGameListCollectionClick(item, "游戏单", otherParams)
    }

    fun trackGameListCollectionClick(
        item: CustomSubjectCollectionItem,
        text: String,
        otherParams: HashMap<String, Any> = hashMapOf()
    ) {
        SensorsBridge.trackGameListCollectionClick(
            LOCATION_CUSTOM_PAGE,
            item.data.id,
            item.data.name,
            text,
            pageLocation.bottomTab,
            pageLocation.severalTabPageId,
            pageLocation.severalTabPageName,
            pageLocation.tabPosition,
            pageLocation.tabContent,
            pageLocation.pageId,
            pageLocation.pageName,
            item.componentStyle,
            otherParams
        )
    }

    fun trackColumnCollectionClick(
        item: CustomSubjectCollectionItem,
        subject: CustomPageData.LinkColumnCollection.CustomSubjectEntity?,
        text: String
    ) {
        SensorsBridge.trackColumnCollectionClick(
            LOCATION_CUSTOM_PAGE,
            "",
            "",
            item.data.name,
            item.data.id,
            item.position,
            subject?.title ?: "",
            subject?.id ?: "",
            text,
            item.componentStyle,
            pageLocation.bottomTab,
            pageLocation.severalTabPageId,
            pageLocation.severalTabPageName,
            pageLocation.tabPosition,
            pageLocation.tabContent,
            pageLocation.pageId,
            pageLocation.pageName
        )
    }

    companion object {

        const val LOCATION_CUSTOM_PAGE = "自定义页面"

        fun buildGameSubjectEntrance(subjectName: String, childPosition: Int) =
            "(游戏-专题:${subjectName}-列表[${childPosition + 1}])"

        fun buildSubjectCollectionEntrance(item: CustomSubjectCollectionItem): String {
            return if (item.isSubjectCollection) {
                "专题合集-${item.componentStyle}"
            } else {
                ""
            }
        }
    }
}