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

import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.model.CustomSubjectCollectionItem
import com.gh.gamecenter.home.custom.tracker.GameSubjectCollectionTracker

// 专题合集/游戏单合集 点击事件处理
class GameSubjectCollectionEventHelper(
    viewModel: CustomPageViewModel
) : CustomPageItemChildEventHelper(viewModel) {

    private val tracker = GameSubjectCollectionTracker(viewModel.pageLocation)

    private fun isMatch(block: (CustomSubjectCollectionItem) -> Unit) {
        (_item as? CustomSubjectCollectionItem)?.let(block)
    }

    fun onUserClick(userId: String) {
        isMatch {
            tracker.trackGameListCollectionClickWithUser(it, userId)
            viewModel.navigateUserHomePage(it, userId)
        }
    }

    fun navigateSubjectCollectionPage(
        position: Int,
        subject: CustomPageData.LinkColumnCollection.CustomSubjectEntity
    ) {
        isMatch {
            if (it.isSubjectCollection) {
                tracker.trackColumnCollectionClick(it, subject, null, "游戏专题")
                viewModel.navigateSubjectCollectionPage(it, position, subject)
            } else {
                tracker.trackGameListCollectionClickWithGameCollection(it, subject.id, subject.title)
                viewModel.navigateGameListDetailPage(it, position, subject)
            }
        }
    }

    fun navigateToGameDetailPage(childPosition: Int, game: GameEntity, subject: CustomPageData.LinkColumnCollection.CustomSubjectEntity? = null) {
        isMatch {
            if (game.isMiniGame()) {
                tracker.trackMiniGameClick(it, subject, game)
            } else if (it.isSubjectCollection) {
                tracker.trackColumnCollectionClick(it, subject, game, "游戏")
            } else {
                tracker.trackGameListCollectionClickWithGame(it, game.id, game.name ?: "")
            }
            viewModel.navigateToGameDetailPageByGame(it, childPosition, game)
        }
    }

    fun onDownloadButtonClick(childPosition: Int, gameEntity: GameEntity, subject: CustomPageData.LinkColumnCollection.CustomSubjectEntity? = null) {
        isMatch {
            if (gameEntity.isMiniGame()) {
                tracker.trackMiniGameClick(it, subject, gameEntity)
            } else if (it.isSubjectCollection) {
                tracker.trackColumnCollectionClick(it, subject, gameEntity, "按钮")
            } else {
                tracker.trackGameListCollectionClick(it, "按钮")
            }
        }
    }

    fun navigateToLinkPage(link: LinkEntity) {
        isMatch {
            tracker.trackColumnCollectionClick(it, null, null, "更多", link)
            viewModel.navigateToLinkPage(it, link, "", null)
        }
    }

    fun onRotateRefresh() {
        isMatch {
            if (it.isSubjectCollection) {
                tracker.trackColumnCollectionClick(it, null, null, "刷新")
            } else {
                tracker.trackGameListCollectionClick(it, "刷新")
            }
            viewModel.onRotateRefresh(it)
        }

    }

    fun onMoreClick(link: LinkEntity) {
        isMatch {
            if (it.isSubjectCollection) {
                tracker.trackColumnCollectionClick(it, null, null, "更多", link)
            } else {
                tracker.trackGameListCollectionClickWithMore(it, link)

            }
            viewModel.navigateToLinkPage(it, link, "更多", null)
        }
    }

    fun onAllClick() {
        isMatch {
            if (it.isSubjectCollection) {
                tracker.trackColumnCollectionClick(it, null, null, "全部")

                val isRefreshStyle = it.data.style.contains("refresh")
                if (isRefreshStyle) {
                    val showSubject = it.showSubject
                    if (showSubject != null) {
                        viewModel.navigateSubjectDetailPage(it, showSubject)
                    }
                } else {
                    viewModel.navigateSubjectCollectionPage(it, -1, null)
                }
            } else {
                val isRefreshStyle = it.data.style.contains("refresh")
                if (isRefreshStyle) {
                    val subject = it.showSubject
                    if (subject != null) {
                        tracker.trackGameListCollectionClickWithGameCollection(
                            it,
                            it.showSubject?.id ?: "",
                            it.showSubject?.title ?: ""
                        )
                        viewModel.navigateGameListDetailPage(it, -1, subject)
                    } else {
                        tracker.trackGameListCollectionClick(it, "全部")
                        viewModel.navigateGameListCollectionDetailPage(it)
                    }

                } else {
                    tracker.trackGameListCollectionClick(it, "全部")
                    viewModel.navigateGameListCollectionDetailPage(it)
                }
            }
        }
    }

    fun navigateGameListSquare() {
        isMatch {
            tracker.trackGameListCollectionClick(it, "游戏单广场")
            viewModel.navigateToGameListSquare(it)
        }
    }
}