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

import android.content.Context
import android.graphics.drawable.Animatable
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.imagepipeline.image.ImageInfo
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.ItemGameDetailImageLinkBinding
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 GameDetailAdvertisingImageItemViewHolder(
    val binding: ItemGameDetailImageLinkBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkAdvertising ?: return
        bindImageItem(context, binding, entity, false, "游戏详情-广告推荐", baseTrackData) { gameStatus }
    }

    companion object {
        fun bindImageItem(
            context: Context,
            binding: ItemGameDetailImageLinkBinding,
            data: GameDetailLink,
            isAnnouncement: Boolean,
            entrance: String,
            trackData: GameDetailModuleTrackData,
            getGameStatus: () -> String
        ) {
            binding.run {
                if (data.img.isNotEmpty()) {
                    imageIv.updateRoundingParams {
                        setOverlayColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(context))
                    }
                    ImageUtils.displayWithCallback(imageIv, data.img, false, object : BaseControllerListener<ImageInfo>() {
                        override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
                            if (imageInfo == null || imageInfo.height == 0) {
                                return
                            }
                            val scale = imageInfo.width.toFloat() / imageInfo.height.toFloat()
                            maskView.updateLayoutParams<ConstraintLayout.LayoutParams> {
                                height = if (scale > 2F) 40F.dip2px() else 60F.dip2px()
                            }
                        }
                    })
                }
                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)
                }
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_aw_primary.toColor(context))
                titleTv.text = if (isAnnouncement) data.text else data.title
                linkTv.goneIf(data.text.isEmpty() || isAnnouncement) {
                    linkTv.text = data.text
                    linkTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
                }
                arrowIv.goneIf(data.text.isNotEmpty() || isAnnouncement)
                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()
                        )
                    }
                }
            }
        }
    }
}