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

import androidx.core.view.isVisible
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.ExtraTagHandler
import com.gh.gamecenter.databinding.ItemGameDetailDeveloperWordBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.dialog.GameDetailScrollableTextDialogFragment
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailDeveloperWordItemViewHolder(
    val binding: ItemGameDetailDeveloperWordBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkDeveloperWord ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            contentTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
            titleTv.text = entity.name
            contentTv.setTextWithInterceptingInternalUrl(
                entity.text.dropFontColorInDarkMode(contentTv.context)
                    .fromHtmlCompat(PicassoImageGetter(contentTv), ExtraTagHandler())
            )
            contentTv.post {
                expandTv.isVisible = (contentTv.lineCount == 3 && (contentTv.layout?.getEllipsisCount(2) ?: 0) > 0) || contentTv.lineCount > 3
            }
            expandTv.background = R.drawable.bg_ui_surface_expand_gradient.toDrawable(context)
            expandTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            expandTv.setOnClickListener {
                GameDetailScrollableTextDialogFragment.show(
                    context,
                    entity.name,
                    entity.text,
                    isHtmlContent = true
                )
                SensorsBridge.trackGameDetailModuleClick(
                    gameId,
                    gameName,
                    gameType,
                    "组件内容",
                    moduleType,
                    entity.name,
                    sequence,
                    gameStatus = gameStatus
                )
            }
        }
    }
}