package com.gh.gamecenter.forum.home.follow.viewholder

import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.common.util.NewsUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.RecyclerGiftPackBinding
import com.gh.gamecenter.entity.FollowDynamicEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.LibaoEntity
import com.gh.gamecenter.feature.entity.MeEntity

class GiftPackViewHolder(
    val binding: RecyclerGiftPackBinding,
    val listener: OnChildEventListener
) : ViewHolder(binding.root) {
    fun bind(
        libao: LibaoEntity,
        time: Long
    ) {
        val game = libao.game?.toGameEntity() ?: return
        binding.ivIcon.displayGameIcon(game)
        binding.tvGameName.text = game.name
        binding.tvTime.text = NewsUtils.getFormattedTime(libao.time)
        binding.tvGiftPackName.text = libao.name
        binding.tvGiftPackContent.text = libao.content
        binding.gCode.goneIf(libao.code.isNullOrBlank()) {
            binding.tvCode.text = libao.code
        }

        arrayOf(binding.ivIcon, binding.tvGameName).forEach {
            it.setOnClickListener {
                listener.onGameClick(game)
            }
        }
        binding.tvCopy.setOnClickListener {
            listener.onCopy(libao.code ?: "")
        }

        itemView.setOnClickListener {
            listener.onGiftPackageClick(libao)
        }
    }

    interface OnChildEventListener {

        fun onGameClick(game: GameEntity)

        fun onGiftPackageClick(libaoEntity: LibaoEntity)

        fun onCopy(code: String)
    }
}