package com.gh.gamecenter.gamedetail.desc

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.databind.BindingAdapters
import com.gh.common.exposure.ExposureEvent
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.databinding.ItemGameDetailRelatedVersionBinding
import com.gh.gamecenter.entity.GameDetailEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.game.GameItemViewHolder

class GameRelatedVersionAdapter(
    private val mContext: Context,
    private val mGameName: String,
    private val mDatas: ArrayList<GameDetailEntity.RelatedVersion>,
    private val mEntrance: String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var exposureEventList: ArrayList<ExposureEvent> = arrayListOf()
    private val mMaxWidth = mContext.resources.displayMetrics.widthPixels - 32F.dip2px()

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

    override fun getItemCount(): Int = mDatas.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val relatedVersion = mDatas[position]
        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(mMaxWidth - 24F.dip2px(), ViewGroup.LayoutParams.WRAP_CONTENT)
            } else {
                ViewGroup.LayoutParams(mMaxWidth - 1, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            holder.binding.root.setPadding(paddingStart, 8F.dip2px(), paddingEnd, 8F.dip2px())
            holder.binding.gameNameTv.setTextColor(R.color.text_title.toColor(mContext))
            holder.binding.gameNameTv.text = relatedVersion.gameName
            BindingAdapters.setGameTags(
                holder.binding.gameLabelLl,
                GameEntity().apply { tagStyle = relatedVersion.gameTags }
            )
            holder.binding.gameIconView.displayGameIcon(
                relatedVersion.game?.getIcon(),
                relatedVersion.game?.iconSubscript
            )
            holder.binding.gameIconView.setBorderColor(R.color.border)
            holder.itemView.setOnClickListener {
                MtaHelper.onEvent("游戏详情_新", "相关游戏版本", "${mGameName}+${relatedVersion.gameName}")
                GameDetailActivity.startGameDetailActivity(
                    mContext,
                    relatedVersion.gameId,
                    StringUtils.buildString(
                        mEntrance,
                        "+(",
                        "游戏详情",
                        "[",
                        relatedVersion.gameName,
                        "]:相关游戏[",
                        (position + 1).toString(),
                        "])"
                    ),
                    exposureEventList.safelyGetInRelease(position)
                )
            }
            relatedVersion.game?.toGameEntity()?.let {
                GameItemViewHolder.initGameSubtitle(it, holder.binding.gameSubtitleTv)
            }
        }
    }

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