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

import android.graphics.Color
import android.graphics.drawable.Animatable
import android.graphics.drawable.ColorDrawable
import android.view.View
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.imagepipeline.image.ImageInfo
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.HomeSlideListItemCustomBinding
import com.gh.gamecenter.entity.HomeSlide
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.halo.assistant.HaloApp

class CustomHomeSlideListItemViewHolder(val binding: HomeSlideListItemCustomBinding) :
    BaseRecyclerViewHolder<Any>(binding.root), IExposureProvider {

    private var boundedGameEntity: GameEntity? = null
    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(com.gh.gamecenter.common.R.color.white.toColor())
        binding.includeGame.gameName.setTextColor(com.gh.gamecenter.common.R.color.white.toColor())

        homeSlide.linkGame?.let {
            GameItemViewHolder.initGameSubtitleAndAdLabel(it, binding.includeGame.gameSubtitleTv)
        }

        if (homeSlide.image != binding.slideBackground.tag) {
            binding.bottomGradient.visibility = View.GONE
        }
        binding.slideBackground.setTag(ImageUtils.TAG_TARGET_WIDTH, mImageWith)
        ImageUtils.displayWithCallback(
            binding.slideBackground,
            homeSlide.image,
            false,
            object : BaseControllerListener<ImageInfo>() {
                override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
                    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

        boundedGameEntity = linkGame

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

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