package com.gh.gamecenter.servers.gametest2

import android.content.Context
import android.util.SparseArray
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.IExposable
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.SubItemGameServerTestTopGamesBinding
import com.gh.gamecenter.entity.GameServerTestTopGame
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.lightgame.adapter.BaseRecyclerAdapter

class GameServerTestTopGameAdapter(
    context: Context,
    private val dataList: List<GameServerTestTopGame>,
    private val linkText: String,
    private val pageLocation: PageLocation,
    private val exposureInvoke: ((GameEntity, Int) -> ExposureEvent)? = null
) :
    BaseRecyclerAdapter<GameServerTestTopGameAdapter.GameServerTestTopGameItemViewHolder>(context), IExposable {
    private val exposureEventArray: SparseArray<ExposureEvent> = SparseArray()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameServerTestTopGameItemViewHolder =
        GameServerTestTopGameItemViewHolder(parent.toBinding())

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: GameServerTestTopGameItemViewHolder, position: Int) {
        val topGame = dataList.getOrNull(position) ?: return
        holder.binding.run {
            root.setCardBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(mContext))
            gameNameTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
            root.updateLayoutParams<MarginLayoutParams> {
                leftMargin = if (position == 0) 16F.dip2px() else 8F.dip2px()
                rightMargin = if (position == itemCount - 1) 16F.dip2px() else 0
            }
            coverImage.display(topGame.game?.homeSettingImage)
            iconIv.display(topGame.icon)
            eventNameTv.text = topGame.description
            contentTv.text = topGame.date
            topGame.game?.toGameEntity()?.let {
                val exposureEvent = exposureInvoke?.invoke(it, position)
                exposureEventArray.put(position, exposureEvent)
                gameNameTv.text = it.name
                GameItemViewHolder.initGameSubtitleAndAdLabel(it, gameSubtitleTv, gameNameContainer, gameNameTv)
                root.setOnClickListener { _ ->
                    DirectUtils.directToGameDetail(
                        mContext,
                        it.id,
                        it.name ?: "",
                        traceEvent = exposureEvent
                    )
                    SensorsBridge.trackColumnTestClick(
                        location = "详情页",
                        recommendType = linkText,
                        text = "置顶游戏",
                        bottomTab = pageLocation.bottomTab,
                        multiTabName = pageLocation.severalTabPageName,
                        multiTabId = pageLocation.severalTabPageId,
                        position = pageLocation.tabPosition,
                        tabContent = pageLocation.tabContent,
                        gameId = it.id,
                        gameName = it.name ?: ""
                    )
                }
            }
        }
    }

    class GameServerTestTopGameItemViewHolder(var binding: SubItemGameServerTestTopGamesBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun getEventByPosition(pos: Int): ExposureEvent? = exposureEventArray.get(pos)

    override fun getEventListByPosition(pos: Int): List<ExposureEvent>? = null
}