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

import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.display
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.GameDoubleCardItemAlCustomBinding
import com.gh.gamecenter.databinding.GameDoubleCardItemCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureListProvider
import com.gh.gamecenter.feature.game.GameItemViewHolder
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSplitSubjectItem

/**
 *
 * 游戏专题-双列卡片-v2
 */
class CustomDoubleCardViewHolder(
    viewModel: CustomPageViewModel,
    val binding: GameDoubleCardItemAlCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root), IExposureListProvider {

    private val mPosterWidth = (DisplayUtils.getScreenWidth() - 40F.dip2px()) / 2

    private var leftBoundedGameEntity: GameEntity? = null
    private var rightBoundedGameEntity: GameEntity? = null

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

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSplitSubjectItem) {
            val gameList = item.data.data ?: return

            runOnIoThread(isLightWeightTask = true) {
                fillExposureInfoAndReportAd(item)
            }

            setSplitSubjectTitle(binding.layoutTitle, item, childEventHelper)

            val topMargin = if (item.isFirstLine) {
                0
            } else {
                8f.dip2px()
            }
            val bottomMargin = if (item.isLastLine) {
                16f.dip2px()
            } else {
                0
            }
            (itemView.layoutParams as? MarginLayoutParams)?.let {
                it.topMargin = topMargin
                it.bottomMargin = bottomMargin
                itemView.layoutParams = it
            }

            val leftPosition = item.startChildPosition

            val rightPosition = leftPosition + 1

            val hasLeft = gameList.size > leftPosition
            if (hasLeft) {
                leftBoundedGameEntity = gameList[leftPosition]
                bindSubView(
                    item,
                    leftBoundedGameEntity!!,
                    binding.leftCardView,
                    leftPosition
                )
            }

            val hasRight = gameList.size > rightPosition
            if (hasRight) {
                rightBoundedGameEntity = gameList[rightPosition]
                binding.rightCardView.posterCardView.visibility = View.VISIBLE
                bindSubView(
                    item,
                    rightBoundedGameEntity!!,
                    binding.rightCardView,
                    rightPosition
                )
            } else {
                rightBoundedGameEntity = null
                binding.rightCardView.posterCardView.visibility = View.INVISIBLE
            }
        }
    }

    private fun bindSubView(
        item: CustomSplitSubjectItem,
        gameEntity: GameEntity,
        subBinding: GameDoubleCardItemCustomBinding,
        position: Int
    ) {
        subBinding.run {
            poster.setTag(ImageUtils.TAG_TARGET_WIDTH, mPosterWidth)
            poster.post {
                if (gameEntity.isWechatMiniGame()) {
                    poster.display(gameEntity.banner)
                } else {
                    poster.display(gameEntity.columnImage)
                }
            }
            gameName.text = gameEntity.name
            brief.text =
                if (gameEntity.columnRecommend?.text.isNullOrBlank()) gameEntity.brief
                else gameEntity.columnRecommend!!.text
            GameItemViewHolder.initGameSubtitleAndAdLabelOnGameSubject(
                gameEntity,
                gameSubtitle,
                canShowTitle = item.data.showIndexSubtitle
            )

            root.setOnClickListener {
                childEventHelper.navigateToGameDetailPage(position, gameEntity)
            }
        }
    }

    private fun fillExposureInfoAndReportAd(item: CustomSplitSubjectItem) {
        for (i in item.startChildPosition..item.endChildPosition) {
            val game = item.data.data?.getOrNull(i) ?: break

            fillExposureAndReportAd(game, i, item)
        }
    }

    override fun provideExposureDataList(): List<ExposureEvent>? {
        val gameEntityList = arrayListOf<GameEntity?>().apply {
            leftBoundedGameEntity?.let { add(leftBoundedGameEntity) }
            rightBoundedGameEntity?.let { add(rightBoundedGameEntity) }
        }

        for (gameEntity in gameEntityList) {
            gameEntity?.exposureEvent?.getFreshExposureEvent()
        }

        return gameEntityList.mapNotNull { it?.exposureEvent }
    }

    override fun provideExposureData(): ExposureEvent? = null

}