package com.gh.gamecenter.home.slide

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import com.facebook.imagepipeline.image.ImageInfo
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.RandomUtils
import com.gh.gamecenter.databinding.HomeSlideListItemBinding
import com.gh.gamecenter.entity.HomeSlide
import com.gh.gamecenter.game.GameItemViewHolder
import com.halo.assistant.HaloApp

class HomeSlideListItemViewHolder(val binding: HomeSlideListItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    private val mImageWith = HaloApp.getInstance().application.resources.displayMetrics.widthPixels - 40F.dip2px()

    fun bindSlideListItem(homeSlide: HomeSlide) {
        binding.playIv.goneIf(homeSlide.linkType != "video")
        binding.title.goneIf(homeSlide.title.isEmpty())
        binding.title.text = homeSlide.title
        binding.digest.goneIf(!(homeSlide.text.isNotEmpty() && homeSlide.linkGame == null))
        binding.digest.text = homeSlide.text
        if (homeSlide.linkGame != null) {
            binding.includeGame.root.visibility = View.VISIBLE
            val linkGame = homeSlide.linkGame
            binding.includeGame.run {
                gameIcon.displayGameIcon(linkGame)
                gameName.text = linkGame.name
                gameRating.goneIf(!(linkGame.commentCount > 3 && linkGame.star >= 7))
                gameRating.text = linkGame.star.toString()
            }
        } else {
            binding.includeGame.root.visibility = View.GONE
        }
        binding.includeGame.gameRating.setTextColor(R.color.white.toColor())
        binding.includeGame.gameName.setTextColor(R.color.white.toColor())

        homeSlide.linkGame?.let {
            GameItemViewHolder.initGameSubtitle(it, binding.includeGame.gameSubtitleTv, null, binding.includeGame.root)
        }

        binding.bottomGradient.visibility = View.GONE
        ImageUtils.addLimitWidthAndLoad(
            binding.slideBackground,
            homeSlide.image,
            mImageWith,
            object : ImageUtils.OnImageloadListener {
                override fun onLoadFinal(imageInfo: ImageInfo?) {
                    binding.bottomGradient.visibility = View.VISIBLE
                }
            })

        val hierarchy = binding.slideBackground.hierarchy
        try {
            hierarchy.setPlaceholderImage(ColorDrawable(Color.parseColor(homeSlide.placeholderColor)))
        } catch (ignore: Throwable) {
            hierarchy.setPlaceholderImage(RandomUtils.getRandomPlaceholderColor())
        }

        val linkGame = homeSlide.linkGame ?: return

        // 轮播图的标题不需要游戏后缀
        linkGame.nameSuffix = ""

        if (homeSlide.text.isNotEmpty()) linkGame.name = homeSlide.text

        // 强制更改标签颜色
        val homeTags = linkGame.tagStyle
        if (!homeTags.isNullOrEmpty()) {
            for (homeTag in homeTags) homeTag.color = "cccccc"
        }
    }
}