package com.gh.gamecenter.gamedetail.detail.viewholder

import android.content.Context
import androidx.core.view.updateLayoutParams
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.ItemGameDetailLinkBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.gh.gamecenter.gamedetail.entity.GameDetailLink

class GameDetailAdvertisingItemViewHolder(
    val binding: ItemGameDetailLinkBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {

    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkAdvertising ?: return
        bindItem(context, binding, entity, false, "游戏详情-广告推荐", baseTrackData) { gameStatus }
    }

    companion object {
        fun bindItem(
            context: Context,
            binding: ItemGameDetailLinkBinding,
            data: GameDetailLink,
            isAnnouncement: Boolean,
            entrance: String,
            trackData: GameDetailModuleTrackData,
            getGameStatus: () -> String
        ) {
            binding.run {
                container.updateLayoutParams { height = if (isAnnouncement) 28F.dip2px() else 40F.dip2px() }
                container.background = if (isAnnouncement) null else R.drawable.bg_shape_ui_container_1_radius_8_item_style.toDrawable(context)
                iconIv.goneIf(data.componentIcon?.icon.isNullOrEmpty()) {
                    iconIv.setFixedHeight(14)
                    iconIv.setTag(ImageUtils.TAG_TARGET_WIDTH, DisplayUtils.getScreenWidth() - 32F.dip2px())
                    ImageUtils.display(iconIv, data.componentIcon?.icon)
                    iconIv.updateRoundingParams {
                        setOverlayColor(com.gh.gamecenter.common.R.color.ui_container_1.toColor(context))
                    }
                }
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
                titleTv.textSize = if (isAnnouncement) 12F else 13F
                titleTv.text = if (isAnnouncement) data.text else data.title
                container.setOnClickListener { _ ->
                    data.link?.let {
                        DirectUtils.directToLinkPage(context, it, entrance, "")
                        SensorsBridge.trackGameDetailModuleClick(
                            trackData.gameId,
                            trackData.gameName,
                            trackData.gameType,
                            "组件内容",
                            trackData.moduleType,
                            trackData.moduleName,
                            trackData.sequence,
                            trackData.subModuleName,
                            trackData.supSequence,
                            it.type,
                            it.link,
                            it.link,
                            getGameStatus()
                        )
                    }
                }
            }
        }
    }

}