package com.gh.gamecenter.home.custom.viewholder

import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.setMargins
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.entity.PKEntity
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.databinding.ItemPkBinding
import com.gh.gamecenter.gamedetail.detail.viewholder.GameDetailContentRecommendItemViewHolder.Companion.TYPE_PK
import com.gh.gamecenter.gamedetail.detail.viewholder.GameDetailContentRecommendItemViewHolder.Companion.bindPKItem
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPKItem
import com.gh.gamecenter.home.custom.model.CustomPageItem

class CustomPKItemViewHolder(
    viewModel: CustomPageViewModel,
    private val lifecycleOwner: LifecycleOwner,
    val binding: ItemPkBinding
) : BaseCustomViewHolder(viewModel, binding.root) {
    private var pkEntity: PKEntity? = null

    override val childEventHelper by lazy(LazyThreadSafetyMode.NONE) {
        SubjectEventHelper(viewModel)
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        val link = (item as? CustomPKItem)?.link ?: return
        pkEntity = (item as? CustomPKItem)?.data

        if (pkEntity == null) {
            binding.root.updateLayoutParams<MarginLayoutParams> {
                height = 0
                setMargins(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())
                }
                bindPKItem(itemView.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.trackPKClick(
                        GlobalActivityManager.getCurrentPageEntity().pageId,
                        GlobalActivityManager.getCurrentPageEntity().pageName,
                        GlobalActivityManager.getCurrentPageEntity().pageBusinessId,
                        pkEntity!!.id,
                        pkEntity!!.title,
                        "",
                        pkEntity!!.link?.type ?: "",
                        pkEntity!!.link?.link ?: "",
                        pkEntity!!.link?.text ?: "",
                        "跳转链接"
                    )
                }
            }
        }
    }

}