package com.gh.gamecenter.adapter.viewholder

import com.facebook.drawee.generic.RoundingParams
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.databind.BindingAdapters
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.GameImageItemBinding
import com.gh.gamecenter.feature.entity.GameEntity

class GameImageViewHolder(var binding: GameImageItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    // 注意：专题详情的大图不能用此方法
    fun bindImage(entity: GameEntity, applyRoundCorner: Boolean = false) {
        binding.run {
            gameContainer.goneIf(!(entity.type == "game" && entity.getApk().isNotEmpty()))
            gameIcon.displayGameIcon(entity)
            gameName.text = entity.name
            gameSize.text = if (entity.getApk().isNotEmpty()) entity.getApk()[0].size.toString() else ""
            BindingAdapters.setDownloadButton(progressBar, entity, null, null, null, "")
        }
        val context = binding.root.context
        val width = context.resources.displayMetrics.widthPixels - DisplayUtils.dip2px(context, 16F)

        if (applyRoundCorner) {
            val roundingParams = RoundingParams.fromCornersRadius(
                binding.root.resources.getDimensionPixelSize(R.dimen.home_large_image_radius).toFloat()
            )
            binding.gameImageIcon.hierarchy.roundingParams = roundingParams
        }

        ImageUtils.display(binding.gameImageIcon, entity.image, width)
    }
}
