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

import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.isGone
import androidx.core.view.isVisible
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.ExtraTagHandler
import com.gh.gamecenter.databinding.ItemGameDetailCustomColumnBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailCustomColumn
import com.gh.gamecenter.gamedetail.entity.GameDetailCustomColumn.Companion.SHOW_DES_TYPE_ALL
import com.gh.gamecenter.gamedetail.entity.GameDetailCustomColumn.Companion.SHOW_DES_TYPE_PART
import com.gh.gamecenter.gamedetail.entity.GameDetailCustomColumn.RightTopInfo.Companion.TYPE_BUTTON
import com.gh.gamecenter.gamedetail.entity.GameDetailCustomColumn.RightTopInfo.Companion.TYPE_TEXT
import com.gh.gamecenter.gamedetail.entity.GameDetailData

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

    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkCustomColumn ?: return
        binding.run {
            root.setPadding(16F.dip2px(), if (entity.showName) 0 else 16F.dip2px(), 16F.dip2px(), if (entity.showBottomPadding) 8F.dip2px() else 0)
            columnContainer.background = R.drawable.bg_shape_f8_radius_8.toDrawable(context)
            titleTv.goneIf(!entity.showName) {
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
                titleTv.text = entity.name
            }
            imageIv.updateRoundingParams {
                setOverlayColor(com.gh.gamecenter.common.R.color.ui_container_1.toColor(context))
            }
            imageIv.display(entity.img, true)
            imageIv.setOnClickListener {
                skipTitleInfoLink(entity)
            }
            columnTitleContainer.goneIf(entity.titleInfo == null || !entity.titleInfo.showTitle) {
                gameIconIv.displayGameIcon(entity.titleInfo?.icon, entity.titleInfo?.iconSubscript, true, entity.titleInfo?.iconFloat)
                columnTitleIv.isVisible = entity.titleInfo?.showTitle == true
                columnTitleTv.goneIf(entity.titleInfo?.showTitle == false) {
                    columnTitleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
                    columnTitleTv.text = entity.titleInfo?.title
                }
                columnDesTv.goneIf(entity.titleInfo?.text.isNullOrEmpty()) {
                    columnDesTv.text = entity.titleInfo?.text
                    columnDesTv.setTextColor(if (entity.titleInfo?.link == null) com.gh.gamecenter.common.R.color.text_tertiary.toColor(context) else com.gh.gamecenter.common.R.color.text_theme.toColor(context))
                    columnDesTv.setOnClickListener { _ ->
                        skipTitleInfoLink(entity)
                    }
                }
            }
            secondTitleContainer.goneIf(entity.secondTitleInfo == null || !entity.secondTitleInfo.showTitle) {
                secondTitleIconIv.display(entity.secondTitleInfo?.icon, true)
                secondTitleTv.setTextColor(entity.secondTitleInfo?.color?.hexStringToIntColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context)) ?: com.gh.gamecenter.common.R.color.text_primary.toColor(context))
                secondTitleTv.text = entity.secondTitleInfo?.text
            }
            rightTopContainer.goneIf(entity.rightTopInfo == null) {
                val showRightTopButton = entity.rightTopInfo?.style == TYPE_BUTTON
                rightTopIconIv.goneIf(entity.rightTopInfo?.icon.isNullOrEmpty() || showRightTopButton) {
                    ImageUtils.display(rightTopIconIv, entity.rightTopInfo?.icon)
                }
                rightTopButtonTv.goneIf(!showRightTopButton) {
                    rightTopButtonTv.background = com.gh.gamecenter.common.R.drawable.download_button_normal_style.toDrawable(context)
                    rightTopButtonTv.text = entity.rightTopInfo?.text
                }
                val showRightTopText = entity.rightTopInfo?.style == TYPE_TEXT
                rightTopTextTv.goneIf(!showRightTopText) {
                    rightTopTextTv.setDrawableEnd(com.gh.gamecenter.common.R.drawable.ic_auxiliary_arrow_right_12.toDrawable(context))
                    rightTopTextTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
                    rightTopTextTv.text = entity.rightTopInfo?.text
                }
                rightTopContainer.setOnClickListener {
                    entity.rightTopInfo?.link?.let {
                        DirectUtils.directToLinkPage(context, it, "游戏详情-自定义栏目", "")
                        SensorsBridge.trackGameDetailModuleClick(
                            gameId,
                            gameName,
                            gameType,
                            "右上角",
                            moduleType,
                            entity.name,
                            sequence,
                            null,
                            null,
                            it.type,
                            it.link,
                            it.text,
                            gameStatus
                        )
                    }
                }
            }

            contentTv.goneIf(entity.tagDes.isEmpty()) {
                contentTv.updateLayoutParams<MarginLayoutParams> {
                    topMargin = if (imageIv.isGone && secondTitleContainer.isGone && columnTitleContainer.isGone) 0 else 8F.dip2px()
                }
                contentTv.run {
                    setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
                    showExpandOrShrink = entity.showDesType == SHOW_DES_TYPE_PART
                    shrinkLines = entity.showDesRowNum
                    isExpanded = viewModel.expandTextStatusSparseBooleanArray.get(data.position, entity.showDesType == SHOW_DES_TYPE_ALL)
                    setTextWithInterceptingInternalUrl(
                        entity.tagDes.dropFontColorInDarkMode(contentTv.context).fromHtmlCompat(PicassoImageGetter(contentTv), ExtraTagHandler())
                    )
                    onExpandCallback = {
                        viewModel.expandTextStatusSparseBooleanArray.put(data.position, true)
                    }
                    onShrinkCallback = {
                        viewModel.expandTextStatusSparseBooleanArray.put(data.position, false)
                    }
                }
            }
        }
    }

    private fun skipTitleInfoLink(entity: GameDetailCustomColumn) {
        entity.titleInfo?.link?.let {
            DirectUtils.directToLinkPage(context, it, "游戏详情-自定义栏目", "")
            SensorsBridge.trackGameDetailModuleClick(
                gameId,
                gameName,
                gameType,
                "组件内容",
                moduleType,
                entity.name,
                sequence,
                null,
                null,
                it.type,
                it.link,
                it.text,
                gameStatus
            )
        }
    }
}