package com.gh.gamecenter.gamedetail.detail.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.databind.BindingAdapters
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.databinding.ItemGameDetailRelatedVersionBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.gh.gamecenter.gamedetail.detail.viewholder.BaseGameDetailItemViewHolder

class GameDetailRelatedGameAdapter(
    private val context: Context,
    private val game: GameEntity?,
    private val relatedGameList: ArrayList<GameEntity>,
    private val entrance: String,
    private val trackData: BaseGameDetailItemViewHolder.GameDetailModuleTrackData,
    private val getGameStatus: () -> String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var exposureEventList: ArrayList<ExposureEvent> = arrayListOf()
    private val maxWidth = context.resources.displayMetrics.widthPixels

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GameDetailRelatedViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = relatedGameList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val gameEntity = relatedGameList.getOrNull(position) ?: return
        if (holder is GameDetailRelatedViewHolder) {
            val paddingStart = 16F.dip2px()
            val isEndOfRow = position >= if (itemCount % 3 == 0) {
                itemCount - 3
            } else {
                itemCount - itemCount % 3
            }
            // 你可能会问为什么这下面会有一些，"加1减1" 的魔法。没错，这里的 "加1减1"就是为了规避 bug，做出的一些 UI 补偿
            // bug 的表现可以 checkout https://git.shanqu.cc/android/playground/-/tree/test-snap_adapter_culprit 代码，然后滑动到最后一列体验一下
            var paddingEnd = if (isEndOfRow) 16F.dip2px() else 0F.dip2px()
            holder.itemView.layoutParams = if (!isEndOfRow) {
                paddingEnd += 1
                ViewGroup.LayoutParams(maxWidth - 56F.dip2px(), ViewGroup.LayoutParams.WRAP_CONTENT)
            } else {
                ViewGroup.LayoutParams(maxWidth - 1, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            holder.binding.root.setPadding(paddingStart, 8F.dip2px(), paddingEnd, 8F.dip2px())
            holder.binding.gameNameTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            holder.binding.gameNameTv.text = gameEntity.name
            BindingAdapters.setGameTags(holder.binding.gameLabelLl, gameEntity, "")
            holder.binding.gameIconView.displayGameIcon(gameEntity)
            holder.binding.gameIconView.setBorderColor(com.gh.gamecenter.common.R.color.resource_border)
            holder.itemView.setOnClickListener {
                GameDetailActivity.startGameDetailActivity(
                    context,
                    gameEntity.id,
                    StringUtils.buildString(
                        entrance,
                        "+(",
                        "游戏详情",
                        "[",
                        gameEntity.name,
                        "]:相关游戏[",
                        (position + 1).toString(),
                        "])"
                    ),
                    exposureEventList.safelyGetInRelease(position)
                )
                SensorsBridge.trackGameDetailModuleClick(
                    trackData.gameId,
                    trackData.gameName,
                    trackData.gameType,
                    "组件内容",
                    trackData.moduleType,
                    "相关游戏",
                    trackData.sequence,
                    supSequence = position + 1,
                    gameStatus = getGameStatus()
                )
            }
            GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, holder.binding.gameSubtitleTv)
        }
    }

    class GameDetailRelatedViewHolder(var binding: ItemGameDetailRelatedVersionBinding) :
        RecyclerView.ViewHolder(binding.root)
}