package com.gh.gamecenter.home.slide

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.core.graphics.ColorUtils
import androidx.core.view.isVisible
import com.facebook.imagepipeline.image.ImageInfo
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.RandomUtils
import com.gh.gamecenter.databinding.HomeSubSlideListItemBinding
import com.gh.gamecenter.entity.HomeSlide
import com.gh.gamecenter.feature.game.GameItemViewHolder

class HomeSubSlideListItemViewHolder(val binding: HomeSubSlideListItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root) {

    private val mImageWith = ((DisplayUtils.getScreenWidth() / 2F) - 20F.dip2px()).toInt()

    fun bindSubSlideListItem(homeSlide: HomeSlide) {
        if (homeSlide.linkGame != null) {
            binding.gameGroup.visibility = View.VISIBLE
            binding.gameName.text = homeSlide.linkGame.name
            binding.gameIconIv.displayGameIcon(homeSlide.linkGame)
            GameItemViewHolder.initGameSubtitleAndAdLabel(homeSlide.linkGame, binding.gameSubtitleTv)
        } else {
            binding.gameGroup.visibility = View.GONE
        }
        binding.recommendTypeIv.isVisible = homeSlide.recommendType.isNotEmpty()
        binding.recommendTypeIv.setImageDrawable(
            when (homeSlide.recommendType) {
                "activity" -> R.drawable.label_huodong.toDrawable(binding.recommendTypeIv.context)
                "update" -> R.drawable.label_gengxin.toDrawable(binding.recommendTypeIv.context)
                "new" -> R.drawable.label_shangxin.toDrawable(binding.recommendTypeIv.context)
                "first_release" -> R.drawable.label_shoufa.toDrawable(binding.recommendTypeIv.context)
                "hot" -> R.drawable.label_remen.toDrawable(binding.recommendTypeIv.context)
                "libao" -> R.drawable.label_libao.toDrawable(binding.recommendTypeIv.context)
                else -> null
            }
        )

        if (homeSlide.placeholderColor.isNotEmpty()) {
            val colorAlpha100 = ColorUtils.setAlphaComponent(homeSlide.placeholderColor.hexStringToIntColor(), 0xFF)
            val colorHSV = colorAlpha100.colorToHSV().apply {
                this[2] *= 0.6F
            }
            val startColor = Color.HSVToColor(colorHSV)
            val endColor = ColorUtils.setAlphaComponent(startColor, 0)

            binding.slideMask.visibility = View.GONE
            binding.slideMask.background = GradientDrawable().apply {
                gradientType = GradientDrawable.LINEAR_GRADIENT
                orientation = GradientDrawable.Orientation.BOTTOM_TOP
                colors = intArrayOf(startColor, endColor)
            }
        }

        ImageUtils.addLimitWidthAndLoad(
            binding.slideBackground,
            homeSlide.image,
            mImageWith,
            object : ImageUtils.OnImageloadListener {
                override fun onLoadFinal(imageInfo: ImageInfo?) {
                    if (homeSlide.placeholderColor.isNotEmpty() && homeSlide.linkGame != null) {
                        binding.slideMask.visibility = View.VISIBLE
                    }
                }
            })

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