package com.gh.gamecenter.home.custom.eventlistener

import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.model.CustomSubjectItem
import com.gh.gamecenter.home.custom.tracker.SubjectTracker
import com.lightgame.utils.Utils

class SubjectEventHelper(viewModel: CustomPageViewModel) : CustomPageItemChildEventHelper(viewModel) {

    private val tracker = SubjectTracker(viewModel.pageLocation)

    fun navigateToGameDetailPage(position: Int, gameEntity: GameEntity) {
        when {
            gameEntity.isDspGame -> {
                val json = tracker.trackDspGameClick(_item, gameEntity, "广告内容", "自定义页面")

                // Parse the JSON string into a JSONObject
                if (json != null) {
                    val map = mutableMapOf<String, String>()
                    val keys = json.keys() // Returns an Iterator

                    while (keys.hasNext()) {
                        val key = keys.next()
                        try {
                            val value = json.get(key)
                            map[key] = value.toString()
                        } catch (e: Exception) {
                            Utils.log("Error parsing JSON key: $key, value: ${json.get(key)}")
                        }
                    }

                    gameEntity.tempDspLogMap = map.toMap() // Return an immutable copy
                }
            }
            gameEntity.isMiniGame() -> {
                tracker.trackMiniGameClick(_item, gameEntity)
            }
            else -> {
                tracker.trackColumnClick(_item, gameEntity, "游戏")
            }
        }

        viewModel.navigateToGameDetailPageByGame(_item, position, gameEntity)
    }

    fun onDownloadButtonClick(childPosition: Int, gameEntity: GameEntity) {
        when {
            gameEntity.isDspGame -> {
                tracker.trackDspGameClick(_item, gameEntity, "按钮", "自定义页面")
            }
            gameEntity.isMiniGame() -> {
                tracker.trackMiniGameClick(_item, gameEntity)
            }
            else -> {
                tracker.trackColumnClick(_item, gameEntity, "按钮")
            }
        }
    }

    fun navigateSubjectPage() {
        tracker.trackColumnClick(_item, null, "游戏专题")
        (_item as? CustomSubjectItem)?.data?.let {
            viewModel.navigateSubjectPage(_item, it)
        }
    }

    fun onChangeABatch(subject: SubjectEntity) {
        tracker.trackColumnClick(_item, null, "右上角", "换一批")
        viewModel.onChangeABatch(subject)
    }

    fun onMoreClick(link: LinkEntity) {
        tracker.trackColumnClick(_item, null, "右上角", "更多", link)
        viewModel.navigateToLinkPage(_item, link, "", null)
    }

    fun onAllClick(subject: SubjectEntity) {
        tracker.trackColumnClick(_item, null, "右上角", "全部")
        viewModel.navigateSubjectPage(_item, subject)
    }

}