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

import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.LeftPagerSnapHelper
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.HomeAmwayListCustomBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.CustomHomeAmwayAdapter
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.OtherItemEventHelper
import com.gh.gamecenter.home.custom.model.CustomAmwayItem
import com.gh.gamecenter.home.custom.model.CustomPageItem

/**
 * 安利墙
 */
class CustomHomeAmwayListViewHolder(
    viewModel: CustomPageViewModel,
    val binding: HomeAmwayListCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

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

    private val adapter by lazy {
        CustomHomeAmwayAdapter(
            itemView.context,
            childEventHelper
        ) { v, childPosition, amway ->
            when (v.id) {
                R.id.rating_block -> {
                    childEventHelper.navigateToRatingReplyPage(amway.game.id, amway.comment.id)
                    NewLogUtils.logHomeShareWallCardClick(amway.game.name ?: "", amway.game.id, "评论内容")
                }

                R.id.user_icon_container -> {
                    childEventHelper.navigateUserHomePage(amway.comment.user.id ?: "")
                    NewLogUtils.logHomeShareWallCardClick(amway.game.name ?: "", amway.game.id, "用户信息")
                }

                else -> {
                    childEventHelper.navigateToGameDetailPageById(amway.game.id, amway.game.name, childPosition)
                    NewLogUtils.logHomeShareWallCardClick(amway.game.name ?: "", amway.game.id, "游戏信息")
                }
            }
        }
    }

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

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

            setTitle()

            if (binding.recyclerView.adapter == null) {
                binding.recyclerView.adapter = adapter
                binding.recyclerView.isNestedScrollingEnabled = false
                val snapHelper = LeftPagerSnapHelper(4F.dip2px())
                snapHelper.attachToRecyclerView(binding.recyclerView)
            }
            adapter.submitList(item.data)
        }
    }

    private fun setTitle() {
        setTitleStyle(binding.layoutTitle)
        binding.layoutTitle.lavRefresh.goneIf(true)
        binding.layoutTitle.gUser.goneIf(true)
        binding.layoutTitle.tvTitle.text = "安利墙"
        binding.layoutTitle.tvRight.text = "全部"
        binding.layoutTitle.tvRight.setOnClickListener {
            NewLogUtils.logHomeShareWallButtonClick("右上角")
            childEventHelper.navigateToAmway()
        }
    }

    private fun fillExposureEventList(item: CustomAmwayItem) {
        item.data.forEachIndexed { index, amway ->
            amway.game.exposureEvent = createExposureEvent(
                amway.game.toGameEntity(),
                listOf(ExposureSource("安利墙", "")),
                pageConfigure.exposureSourceList,
                index,
                item.componentPosition,
                createTrackData(item)
            )
        }
    }

}