package com.gh.gamecenter.game.doublecard

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.common.utils.display
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.databinding.GameDoubleCardItemAlBinding
import com.gh.gamecenter.databinding.GameDoubleCardItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.game.GameItemViewHolder

class DoubleCardViewHolder(val binding: GameDoubleCardItemAlBinding) : RecyclerView.ViewHolder(binding.root) {

    fun bindView(gameList: MutableList<GameEntity>, subjectName: String, positionInOriginSubject: Int) {
        bindSubView(
            gameList[positionInOriginSubject],
            GameDoubleCardItemBinding.bind(binding.leftCardView.posterCardView),
            subjectName,
            positionInOriginSubject
        )

        if (gameList.size > positionInOriginSubject + 1) {
            binding.rightCardView.posterCardView.visibility = View.VISIBLE
            bindSubView(
                gameList[positionInOriginSubject + 1],
                GameDoubleCardItemBinding.bind(binding.rightCardView.posterCardView),
                subjectName,
                positionInOriginSubject + 1
            )
        } else {
            binding.rightCardView.posterCardView.visibility = View.INVISIBLE
        }
    }

    private fun bindSubView(
        gameEntity: GameEntity,
        subBinding: GameDoubleCardItemBinding,
        subjectName: String,
        positionInOriginSubject: Int
    ) {
        subBinding.run {
            poster.post { poster.display(gameEntity.columnImage) }
            gameName.text = gameEntity.name
            brief.text =
                if (gameEntity.columnRecommend?.text.isNullOrBlank()) gameEntity.brief
                else gameEntity.columnRecommend!!.text
            GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, gameSubtitle)

            root.setOnClickListener {
                GameDetailActivity.startGameDetailActivity(
                    root.context,
                    gameEntity.id,
                    StringUtils.buildString(
                        "(游戏-专题:",
                        subjectName,
                        "-列表[",
                        (positionInOriginSubject + 1).toString(),
                        "])"
                    ),
                    traceEvent = gameEntity.exposureEvent
                )
            }
        }
    }

}