package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.graphics.Paint
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.marginStart
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.RecyclerRecommendCardItemBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.home.custom.model.CustomPageData
import com.gh.gamecenter.home.custom.ui.RecommendCardUi

class RecommendCardAdapter(
    context: Context,
    private val listener: RecommendCardUi.OnRecommendCardEventListener
) : CustomBaseChildAdapter<CustomPageData.RecommendCard, RecommendCardAdapter.RecommendCardChildViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecommendCardChildViewHolder {
        return RecommendCardChildViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: RecommendCardChildViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item, position)
        holder.itemView.setOnClickListener {
            listener.onItemClick(position, item)
        }
    }

    class RecommendCardChildViewHolder(val binding: RecyclerRecommendCardItemBinding) :
        RecyclerView.ViewHolder(binding.root), IExposureProvider {

        private var exposureEvent: ExposureEvent? = null

        fun bind(item: CustomPageData.RecommendCard, position: Int) {
            exposureEvent = item.exposureEvent

            binding.tvTitle.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(itemView.context))
            binding.tvOriginalPrice.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(itemView.context))

            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)
            }
        }

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