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.databinding.ItemGameDetailUpdateBinding
import com.gh.gamecenter.feature.entity.GameEntity
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
import com.gh.gamecenter.gamedetail.history.HistoryApkListActivity

class GameDetailUpdateItemViewHolder(
    val binding: ItemGameDetailUpdateBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkUpdate ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            versionTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
            dateTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
            contentTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
            historyTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            historyTv.setDrawableEnd(R.drawable.ic_auxiliary_arrow_right_text_theme_12.toDrawable(context))
            historyTv.isVisible = entity.historyApkStatus == "on" && entity.historyApkCount >= 1
            titleTv.text = entity.name
            versionTv.goneIf(entity.version.isEmpty()) {
                versionTv.text = "版本：${entity.version}"
            }
            dateTv.goneIf(entity.date.isEmpty()) {
                dateTv.text = "${entity.date}"
            }
            contentTv.text = entity.updateDes
            contentTv.post {
                expandTv.isVisible = contentTv.lineCount == 3 && (contentTv.layout?.getEllipsisCount(2) ?: 0) > 0
            }
            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.updateDes,
                    "版本：${entity.version}",
                    entity.date
                )
                SensorsBridge.trackGameDetailModuleClick(
                    gameId,
                    gameName,
                    gameType,
                    "组件内容",
                    moduleType,
                    entity.name,
                    sequence,
                    gameStatus = gameStatus
                )
            }
            historyTv.setOnClickListener {
                context.startActivity(
                    HistoryApkListActivity.getIntent(
                    context,
                    viewModel.game ?: GameEntity(),
                    viewModel.entrance ?: "",
                    "游戏详情-更新内容"
                ))
                SensorsBridge.trackGameDetailModuleClick(
                    gameId,
                    gameName,
                    gameType,
                    "右上角",
                    moduleType,
                    entity.name,
                    sequence,
                    gameStatus = gameStatus
                )
            }
        }
    }
}