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

import androidx.lifecycle.LifecycleOwner
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.ItemGameDetailContentCardTripleBinding
import com.gh.gamecenter.databinding.LayoutGameDetailContentCardSmallBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.detail.viewholder.GameDetailContentCardSingleItemViewHolder.Companion.jumpToContentCardLink
import com.gh.gamecenter.gamedetail.entity.ContentCardEntity
import com.gh.gamecenter.gamedetail.entity.ContentCardEntity.Companion.TYPE_ARCHIVE
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailContentCardTripleItemViewHolder(
    val binding: ItemGameDetailContentCardTripleBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel,
    private val lifecycleOwner: LifecycleOwner
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        data.linkContentCard?.let {
            val getGameStatus = { gameStatus }
            GameDetailContentCardSingleItemViewHolder.bindContentCard(
                lifecycleOwner,
                viewModel,
                it.first(),
                data.cardBackground,
                binding.firstCardContainer,
                baseTrackData.apply { supSequence = 0 },
                getGameStatus
            )
            bindSmallContentCard(
                lifecycleOwner,
                viewModel,
                it[1].apply { showDes = false },
                binding.secondCardContainer,
                baseTrackData.apply { supSequence = 1 },
                getGameStatus
            )
            bindSmallContentCard(
                lifecycleOwner,
                viewModel,
                it[2].apply { showDes = false },
                binding.thirdCardContainer,
                baseTrackData.apply { supSequence = 2 },
                getGameStatus
            )
        }
    }

    companion object {
        fun bindSmallContentCard(
            lifecycleOwner: LifecycleOwner,
            viewModel: GameDetailViewModel,
            contentCardEntity: ContentCardEntity,
            itemBinding: LayoutGameDetailContentCardSmallBinding,
            trackData: GameDetailModuleTrackData,
            getGameStatus: () -> String
        ) {
            with(itemBinding) {
                val context = root.context
                if (contentCardEntity.id == root.tag) {
                    root.background = com.gh.gamecenter.common.R.drawable.bg_shape_f8_radius_8.toDrawable(context)
                    titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))

                    return@with
                }

                root.tag = contentCardEntity.id
                titleTv.text = contentCardEntity.title
                ImageUtils.display(iconIv, contentCardEntity.icon)

                val showRedDot = contentCardEntity.redDot != 0 && viewModel.shouldShowContentCardRedDot(contentCardEntity)
                redDotTv.goneIf(!showRedDot) {
                    redDotTv.text = contentCardEntity.redDot.toString()
                }

                val showNewTag = contentCardEntity.link.type == TYPE_ARCHIVE && contentCardEntity.archive != null && contentCardEntity.showNewTag
                newIv.goneIf(!showNewTag)

                root.setOnClickListener {
                    NewFlatLogUtils.logGameDetailGameContentCardClick(
                        contentCardEntity.title ?: "",
                        viewModel.game?.name ?: "",
                        viewModel.game?.id ?: "",
                        contentCardEntity.link.type ?: "",
                        contentCardEntity.link.link ?: "",
                        contentCardEntity.link.text ?: ""
                    )
                    SensorsBridge.trackGameDetailModuleClick(
                        gameId = trackData.gameId,
                        gameName = trackData.gameName,
                        gameType = trackData.gameType,
                        contentType = "组件内容",
                        moduleType = trackData.moduleType,
                        moduleName = trackData.moduleName,
                        sequence = trackData.sequence,
                        linkText = contentCardEntity.link.text ?: "",
                        linkType = contentCardEntity.link.type ?: "",
                        linkId = contentCardEntity.id,
                        gameStatus = getGameStatus()
                    )

                    val dialog = contentCardEntity.dialog
                    if (dialog != null) {// 展示内容卡片提示弹窗
                        DialogHelper.showDialog(
                            context = context,
                            title = dialog.title ?: "",
                            content = dialog.body ?: "",
                            confirmText = context.getString(com.gh.gamecenter.common.R.string.confirm),
                            cancelText = context.getString(com.gh.gamecenter.common.R.string.cancel),
                            confirmClickCallback = {
                                jumpToContentCardLink(context, contentCardEntity, viewModel, redDotTv)
                            }
                        )
                    } else {
                        jumpToContentCardLink(context, contentCardEntity, viewModel, redDotTv)
                    }
                }

                viewModel.contentCardClickedLiveData.observe(lifecycleOwner) {
                    if (it.peekContent() == contentCardEntity.link.type) {
                        it.getContentWithHandled()?.let { root.performClick() }
                    }
                }
            }
        }
    }
}