package com.gh.gamecenter.home.custom.viewholder

import android.view.View
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.databinding.GameItemCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider

class CustomGamePluggableViewHolder(val binding: GameItemCustomBinding)
    : BaseRecyclerViewHolder<Any>(binding.root), IExposureProvider {

    private var boundedGameEntity: GameEntity? = null

    fun initServerType(gameEntity: GameEntity) {
        boundedGameEntity = gameEntity
        val serverLabel = gameEntity.serverLabel
        when {
            gameEntity.test != null -> {
                binding.gameKaifuType.visibility = View.GONE
                binding.gameKaifuType.text = ""
            }
            serverLabel != null && !gameEntity.advanceDownload -> {
                binding.gameKaifuType.visibility = View.VISIBLE
                binding.gameKaifuType.text = serverLabel.value
                if (gameEntity.isUseDefaultServerStyle()) {
                    binding.gameKaifuType.background =
                        com.gh.gamecenter.feature.R.drawable.server_label_default_bg.toDrawable(binding.root.context)
                    binding.gameKaifuType.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(binding.root.context))
                } else {
                    binding.gameKaifuType.background =
                        DrawableView.getServerDrawable(serverLabel.color)
                    binding.gameKaifuType.setTextColor(com.gh.gamecenter.common.R.color.white.toColor(binding.root.context))
                }
            }
            else -> binding.gameKaifuType.visibility = View.GONE
        }

        // 由于RecyclerView的复用机制 需要每次测量gameName的宽
        binding.gameName.requestLayout()
    }

    override fun provideExposureData(): ExposureEvent? {
        return boundedGameEntity?.exposureEvent?.getFreshExposureEvent()
    }
}