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

import android.view.ViewGroup.MarginLayoutParams
import com.gh.common.util.AdHelper
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.GameVerticalImageTextBinding
import com.gh.gamecenter.databinding.RecyclerGameVerticalImageTextBinding
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.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSplitSubjectItem

class CustomGameVerticalImageTextViewHolder(
    viewModel: CustomPageViewModel,
    private val binding: RecyclerGameVerticalImageTextBinding
) : BaseCustomViewHolder(viewModel, binding.root), IExposureListProvider {

    private var exposureEventList: ArrayList<ExposureEvent>? = arrayListOf()

    private val posterWidth by lazy {
        (itemView.context.resources.displayMetrics.widthPixels - 40F.dip2px()) / 2
    }

    override val childEventHelper: SubjectEventHelper by lazy {
        SubjectEventHelper(viewModel)
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSplitSubjectItem) {

            val gameList = item.data.data ?: return

            fillExposureList(item)

            setSplitSubjectTitle(binding.layoutTitle, item, childEventHelper)

            (itemView.layoutParams as? MarginLayoutParams)?.let {
                it.bottomMargin = 16F.dip2px()
                itemView.layoutParams = it
            }

            val leftPosition = item.startChildPosition

            val rightPosition = leftPosition + 1

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

            val hasRight = gameList.size > rightPosition
            binding.rightCardView.root.visibleIf(hasRight) {
                bindSubView(
                    item,
                    gameList[rightPosition],
                    binding.rightCardView,
                    rightPosition
                )
            }
        }
    }

    private fun bindSubView(
        item: CustomSplitSubjectItem,
        gameEntity: GameEntity,
        subBinding: GameVerticalImageTextBinding,
        position: Int
    ) {
        subBinding.run {
            commonCollectionImage.setTag(ImageUtils.TAG_TARGET_WIDTH, posterWidth)
            commonCollectionImage.post {
                if (gameEntity.columnImage.isBlank()) {
                    commonCollectionImage.display(gameEntity.image)
                } else {
                    commonCollectionImage.display(gameEntity.columnImage)
                }
            }
            gameName.text = gameEntity.name
            GameItemViewHolder.initGameSubtitleAndAdLabelOnGameSubject(
                gameEntity,
                gameSubtitle,
                canShowTitle = item.data.showIndexSubtitle
            )
            tvExplain.goneIf(gameEntity.explainText.isBlank()) {
                tvExplain.text = gameEntity.explainText
            }
            tvRecommend.text = gameEntity.recommendText
            root.setOnClickListener {
                childEventHelper.navigateToGameDetailPage(position, gameEntity)
            }
        }
    }

    private fun fillExposureList(item: CustomSplitSubjectItem) {
        exposureEventList = arrayListOf<ExposureEvent>()
        for (i in item.startChildPosition..item.endChildPosition) {
            val game = item.data.data?.getOrNull(i) ?: break
            val event = createExposureEvent(
                game,
                item.exposureSource,
                pageConfigure.exposureSourceList,
                i,
                item.componentPosition,
                createTrackData(item)
            )
            game.exposureEvent = event

            if (game.adGroupId.isNotEmpty() && !game.isAdRequestReported) {
                AdHelper.reportAdRequest(game.exposureEvent!!)
                game.isAdRequestReported = true
            }

            exposureEventList?.add(event)
        }
    }

    override fun provideExposureData(): ExposureEvent? = null

    override fun provideExposureDataList(): List<ExposureEvent>? {
        exposureEventList?.forEach {
            it.getFreshExposureEvent()
        }
        return exposureEventList
    }

}