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

import android.graphics.Paint
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.gh.common.util.AdHelper
import com.gh.common.util.DownloadItemUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.databinding.RecyclerSingleGameCardCustomBinding
import com.gh.gamecenter.feature.entity.CustomPageTrackData
import com.gh.gamecenter.feature.entity.TagStyleEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.CustomViewExt.setGameRattingWithSubject
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectItem

/**
 * 游戏专题-单游戏卡片
 */
class CustomSingleGameCardViewHolder(
    viewModel: CustomPageViewModel,
    val binding: RecyclerSingleGameCardCustomBinding,
) : BaseCustomViewHolder(viewModel, binding.root), IExposureProvider {

    private var exposureEvent: ExposureEvent? = null

    override val onlyNotifyItemChanged: Boolean
        get() = true

    override val childEventHelper by lazy {
        SubjectEventHelper(viewModel)
    }

    fun bindView(item: CustomPageItem, adapter: Adapter<out RecyclerView.ViewHolder>) {
        super.bindView(item)
        if (item is CustomSubjectItem) {
            val data = item.data.data ?: return
            val showPosition = viewModel.refreshCount % data.size
            val game = data[showPosition]

            exposureEvent = createExposureEvent(
                game,
                listOf(
                    ExposureSource(
                        "专题",
                        "${item.data.name ?: ""}+${item.componentStyle}+${item.data.id ?: ""}"
                    )
                ),
                pageConfigure.exposureSourceList,
                showPosition,
                item.componentPosition,
                createTrackData(item)
            )

            if (game.adGroupId.isNotEmpty() && !game.isAdRequestReported) {
                AdHelper.reportAdRequest(game.exposureEvent!!)
                game.isAdRequestReported = true
            }

            with(binding) {
                tvTitle.text = game.abbreviation.ifBlank { game.name }
                tvTodayRecommend.text = item.data.name

                val rechargeDiscount = game.welfareInfo?.rechargeDiscount
                if (rechargeDiscount.isNullOrBlank()) {
                    gWelfare.goneIf(true)
                    tvDesc.goneIf(false)
                    tvDesc.text = game.recommendText.ifBlank { game.brief }

                } else {
                    gWelfare.goneIf(false)
                    tvDesc.goneIf(true)
                    tvDiscount.text = rechargeDiscount

                    val labelText = game.welfareInfo?.labelText
                    tvLabel.text = if (labelText.isNullOrBlank()) {
                        itemView.context.getText(R.string.recharge)
                    } else {
                        labelText
                    }

                    tvPrice.text = game.welfareInfo?.rechargeCurrentPrice ?: "0"

                    tvOriginalPrice.text =
                        itemView.context.getString(
                            R.string.price_with_symbol,
                            "${game.welfareInfo?.rechargeOriginalPrice ?: 0}"
                        )
                    tvOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                }

                ivIcon.displayGameIconWithIfShowSubscript(game, item.data.showIndexIconSubscript)

                setGameRattingWithSubject(tvRatting, game.commentCount > 3 && game.star >= 7, game)
                gameTags.setData(game.tagStyle.map(TagStyleEntity::name))

                itemView.setOnClickListener {
                    childEventHelper.navigateToGameDetailPage(item.position, game)
                }

                game.customPageTrackData = CustomPageTrackData(
                    viewModel.pageLocation,
                    item.componentName,
                    item.componentStyle,
                    item.link.text ?: "",
                    item.link.link ?: ""
                )

                DownloadItemUtils.setOnClickListener(
                    root.context,
                    btnDownload,
                    game,
                    bindingAdapterPosition,
                    adapter,
                    entrance = pageConfigure.entrance,
                    location = "",
                    traceEvent = item.exposureEvent
                ) {
                    childEventHelper.onDownloadButtonClick(item.position, game)
                }

                DownloadItemUtils.updateItem(
                    root.context,
                    game,
                    GameViewHolder(root).apply {
                        gameDownloadBtn = btnDownload
                        multiVersionDownloadTv = this@with.multiVersionDownloadTv
                        gameDownloadTips = this@with.downloadTipsLottie
                    }
                )
            }
        }
    }

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