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

import android.content.Context
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.entity.PKEntity
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.ItemPkBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailContentRecommendItemViewHolder(
    val binding: ItemPkBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel,
    private val lifecycleOwner: LifecycleOwner,
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    private var pkEntity: PKEntity? = null

    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val link = data.linkContentRecommend ?: return
        pkEntity = data.pkData
        if (pkEntity == null) {
            binding.root.updateLayoutParams<MarginLayoutParams> { height = 0 }
            return
        }

        binding.run {
            if (link.type == TYPE_PK) {
                if (!pkView.isInitialized()) {
                    viewModel.pkVoteResultLiveData.observe(lifecycleOwner) {
                        if (it.peekContent().first == pkEntity?.id) {
                            val pair = it.getContentWithHandled()
                            if (pair != null && pkView.isInitialized()) {
                                pkView.vote(pair.second)
                                statisticsTv.text = "已有${pkEntity?.totalNum}人参与"
                                pkView.showResultWithAnimation()
                            }
                        }
                    }
                } else {
                    pkView.updateView()
                }
                binding.root.updateLayoutParams<MarginLayoutParams> {
                    height = ViewGroup.LayoutParams.WRAP_CONTENT
                    setMargins(16F.dip2px(), 16F.dip2px(), 16F.dip2px(), 8F.dip2px())
                }
                bindPKItem(context, this, pkEntity!!, onPositiveClickAction = positive@{
                    if (pkEntity == null) return@positive
                    SensorsBridge.trackPKClick(
                        GlobalActivityManager.getCurrentPageEntity().pageId,
                        GlobalActivityManager.getCurrentPageEntity().pageName,
                        GlobalActivityManager.getCurrentPageEntity().pageBusinessId,
                        pkEntity!!.id,
                        pkEntity!!.title,
                        pkEntity!!.option1.text,
                        clickSite = "选项"
                    )
                    viewModel.postPKVote(pkEntity!!.id, true)
                }, onNegativeClickAction = negative@{
                    if (pkEntity == null) return@negative
                    SensorsBridge.trackPKClick(
                        GlobalActivityManager.getCurrentPageEntity().pageId,
                        GlobalActivityManager.getCurrentPageEntity().pageName,
                        GlobalActivityManager.getCurrentPageEntity().pageBusinessId,
                        pkEntity!!.id,
                        pkEntity!!.title,
                        pkEntity!!.option2.text,
                        clickSite = "选项"
                    )
                    viewModel.postPKVote(pkEntity!!.id, false)
                }) {
                    SensorsBridge.trackGameDetailModuleClick(
                        gameId,
                        gameName,
                        gameType,
                        "组件内容",
                        moduleType,
                        pkEntity?.title,
                        sequence,
                        null,
                        null,
                        pkEntity?.link?.type,
                        pkEntity?.link?.link,
                        pkEntity?.link?.text,
                        gameStatus
                    )
                    SensorsBridge.trackPKClick(
                        GlobalActivityManager.getCurrentPageEntity().pageId,
                        GlobalActivityManager.getCurrentPageEntity().pageName,
                        GlobalActivityManager.getCurrentPageEntity().pageBusinessId,
                        pkEntity!!.id,
                        pkEntity!!.title,
                        "",
                        pkEntity!!.link?.type ?: "",
                        pkEntity!!.link?.link ?: "",
                        pkEntity!!.link?.text ?: "",
                        clickSite = "跳转链接"
                    )
                }
            }
        }
    }

    companion object {
        const val TYPE_PK = "pk"
        const val TIME_PATTERN = "yyyy.MM.dd HH:mm:ss"

        fun bindPKItem(
            context: Context,
            binding: ItemPkBinding,
            pkEntity: PKEntity,
            onPositiveClickAction: () -> Unit,
            onNegativeClickAction: () -> Unit,
            onLinkClickAction: () -> Unit
        ) {
            binding.run {
                root.background = R.drawable.bg_shape_f8_radius_8.toDrawable(context)
                pkTitleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
                endDateTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
                statisticsTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
                linkTv.background = com.gh.gamecenter.common.R.drawable.button_round_theme_alpha_10.toDrawable(context)
                linkTv.setTextColor(com.gh.gamecenter.common.R.color.primary_theme.toColor(context))
                pkView.run {
                    setData(pkEntity)
                    onPositiveClick = onPositiveClickAction
                    onNegativeClick = onNegativeClickAction
                }
                pkTitleTv.text = pkEntity.title
                val endDateText = if (pkEntity.isExpired) "已" else "将"
                endDateTv.text = "${endDateText}于${TimeUtils.getFormatTime(pkEntity.time.end, TIME_PATTERN)}结束"
                statisticsTv.text = "已有${pkEntity.totalNum}人参与"
                linkTv.goneIf(pkEntity.link == null) {
                    linkTv.setOnClickListener { _ ->
                        pkEntity.link?.let {
                            DirectUtils.directToLinkPage(
                                context,
                                it,
                                "内容推荐-PK",
                                ""
                            )
                        }
                        onLinkClickAction.invoke()
                    }
                }
            }
        }
    }
}