package com.gh.gamecenter.adapter.viewholder

import android.graphics.Paint
import android.widget.TextView
import androidx.core.view.marginStart
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.goneIf
import com.gh.gamecenter.databinding.*
import com.gh.gamecenter.home.custom.model.CustomPageData

class SearchGameFooterViewHolder(val binding: SearchGameFooterBinding) : BaseRecyclerViewHolder<Any>(binding.root)
class PersonalHomeRatingViewHolder(val binding: PersonalHomeRatingBinding) : BaseRecyclerViewHolder<Any>(binding.root)
class GameColumnCollectionItemViewHolder(val binding: GameColumnCollectionItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root)

class CommonCollectionItemViewHolder(val binding: CommonCollectionItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root)

class CommonCollectionDetailOneItemViewHolder(val binding: CommonCollectionDetailOneItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root)

class CommonCollectionDetailTwoItemViewHolder(val binding: CommonCollectionDetailTwoItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root)

class CommonCollectionImageTextItemViewHolder(val binding: CommonCollectionImageTextItemBinding) :
    BaseRecyclerViewHolder<Any>(binding.root)

class CommonCollectionDetailTwoItemHorizontalViewHolder(val binding: CommonCollectionDetailTwoItemHorizontalCustomBinding) :
    BaseRecyclerViewHolder<Any>(binding.root)

class CustomCollectionDetailRecommendCardViewHolder(
    val binding: RecyclerRecommendCardCommonContentCollectionDetailCustomBinding
) : BaseRecyclerViewHolder<Any>(binding.root) {

    fun bind(item: CustomPageData.RecommendCard) {
        ImageUtils.display(binding.ivCover, item.image)
        binding.tvTitle.text = item.title
        binding.tvLabel.text = item.tag
        binding.tvPrice.text = item.highlight.text
        binding.tvOriginalPrice.text = if (item.deletion.isShowCurrencySymbol) {
            itemView.context.getString(R.string.price_with_symbol, item.deletion.text)
        } else {
            item.deletion.text
        }
        binding.tvOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
        binding.tvCopy.text = item.addedContent
        binding.tvPriceSymbol.goneIf(!item.highlight.isShowCurrencySymbol)

        binding.root.post {
            val views = mutableListOf(
                binding.tvPrice,
                binding.tvOriginalPrice,
                binding.tvCopy,
            )
            if (item.highlight.isShowCurrencySymbol) {
                views.add(0, binding.tvPriceSymbol)
            }

            binding.tvLabel.goneIf(item.tag.isBlank()) {
                views.add(binding.tvLabel)
            }
            views.forEach {
                it.goneIf(false)
            }

            hideOutOfBoundViewsIfNeed(views)
        }
    }

    /**
     * 计算统一排的view能否完整显示在 itemView 内，如果显示不下，则根据优先级依次隐藏优先级较低的
     * 数据中的View优先级由高到低排列，最后一个优先级最低
     */
    private fun hideOutOfBoundViewsIfNeed(views: MutableList<TextView>) {
        if (views.isEmpty()) {
            return
        }
        val totalWidth = views.sumOf {
            it.width + it.marginStart
        }
        if (totalWidth > itemView.width) {
            views.removeLast().goneIf(true)
            hideOutOfBoundViewsIfNeed(views)
        }
    }

}