package com.halo.assistant.fragment.game

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.GamePluggableItemBinding
import com.gh.gamecenter.feature.entity.GameEntity

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

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