package com.gh.gamecenter.home

import com.gh.common.exposure.ExposureEvent
import com.gh.common.exposure.ExposureSource
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.callback.OnViewClickListener
import com.gh.common.util.*
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.databinding.HomeRecommendItemBinding
import com.gh.gamecenter.entity.GameEntity

class HomeRecommendItemViewHolder(val binding: HomeRecommendItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindRecommend(itemData: HomeItemData, basicExposureSource: List<ExposureSource>) {
        val recommends = itemData.recommends!!
        val clickListener = OnViewClickListener<Int> { _, position ->
            // 很奇怪这里居然抛出 IndexOutOfBoundsException 异常(有可能数据改变了,页面还没刷新导致)
            if (position >= recommends.size) return@OnViewClickListener

            MtaHelper.onEvent("首页_新", "推荐入口_点击", (position + 1).toString())
            val recommend = recommends[position]

            LogUtils.logRecommendClick(
                "首页",
                recommend.name,
                recommend.linkType,
                recommend.linkText,
                recommend.linkId,
                position
            )

            if (recommend.linkType == "top_game_comment") {
                MtaHelper.onEvent("安利墙", "进入", "首页推荐入口")
            }
            if (recommend.linkType == "common_collection") {
                NewLogUtils.logAccessToCommonCollectionDetail(recommend.linkId, recommend.linkText, "首页推荐入口")
            }
            PageSwitchDataHelper.pushCurrentPageData(hashMapOf(Pair(PageSwitchDataHelper.PAGE_BUSINESS_TYPE, "首页-推荐位")))
            DirectUtils.directToLinkPage(
                binding.root.context,
                recommend.transformLinkEntity(),
                "首页推荐入口",
                "新首页-推荐入口",
            )
        }
        recommends.forEachIndexed { index, homeRecommend ->
            val exposureEvent = ExposureEvent.createEventWithSourceConcat(
                gameEntity = GameEntity().also { it.sequence = index },
                basicSource = basicExposureSource,
                source = listOf(ExposureSource("推荐入口", homeRecommend.name))
            )
            exposureEvent.payload.controlType = "推荐入口"
            exposureEvent.payload.controlName = homeRecommend.name
            exposureEvent.payload.controlLinkName = homeRecommend.linkText
            exposureEvent.payload.controlLinkType = homeRecommend.linkType
            itemData.exposureEventList?.add(exposureEvent)
        }

        binding.run {
            containerOne.goneIf(recommends.isEmpty())
            containerTwo.goneIf(recommends.size < 2)
            containerThree.goneIf(recommends.size < 3)
            containerFour.goneIf(recommends.size < 4)
            containerFive.goneIf(recommends.size < 5)
            spaceOne.goneIf(recommends.size < 2)
            spaceTwo.goneIf(recommends.size < 3)
            spaceThree.goneIf(recommends.size < 4)
            spaceFour.goneIf(recommends.size < 5)
            containerOne.setOnClickListener { clickListener.onClick(it, 0) }
            containerTwo.setOnClickListener { clickListener.onClick(it, 1) }
            containerThree.setOnClickListener { clickListener.onClick(it, 2) }
            containerFour.setOnClickListener { clickListener.onClick(it, 3) }
            containerFive.setOnClickListener { clickListener.onClick(it, 4) }
            ImageUtils.display(iconIvOne, if (recommends.isNotEmpty()) recommends[0].icon else "")
            ImageUtils.display(iconIvTwo, if (recommends.size > 1) recommends[1].icon else "")
            ImageUtils.display(iconIvThree, if (recommends.size > 2) recommends[2].icon else "")
            ImageUtils.display(iconIvFour, if (recommends.size > 3) recommends[3].icon else "")
            ImageUtils.display(iconIvFive, if (recommends.size > 4) recommends[4].icon else "")
            nameTvOne.text = if (recommends.isNotEmpty()) recommends[0].name else "推荐入口"
            nameTvTwo.text = if (recommends.size > 1) recommends[1].name else "推荐入口"
            nameTvThree.text = if (recommends.size > 2) recommends[2].name else "推荐入口"
            nameTvFour.text = if (recommends.size > 3) recommends[3].name else "推荐入口"
            nameTvFive.text = if (recommends.size > 4) recommends[4].name else "推荐入口"
            nameTvOne.setTextColor(R.color.text_title.toColor(root.context))
            nameTvTwo.setTextColor(R.color.text_title.toColor(root.context))
            nameTvThree.setTextColor(R.color.text_title.toColor(root.context))
            nameTvFour.setTextColor(R.color.text_title.toColor(root.context))
            nameTvFive.setTextColor(R.color.text_title.toColor(root.context))
        }
    }
}