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

import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutParams
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.visibleIf
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.ItemHomeRecentVgameCustomBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.custom.CustomPageTracker.Companion.LOCATION_CUSTOM_PAGE
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.IGameChangedNotifier
import com.gh.gamecenter.home.custom.adapter.CustomHomeRecentVGameAdapter
import com.gh.gamecenter.home.custom.adapter.CustomPageAdapter
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.OtherItemEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomRecentGamesItem
import com.gh.vspace.VDownloadManagerActivity
import com.gh.vspace.VHelper

/**
 * va-最近在玩
 */
class CustomHomeRecentVGameViewHolder(
    private val showVGameSquare: Boolean,
    viewModel: CustomPageViewModel,
    var binding: ItemHomeRecentVgameCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

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

    private val adapter by lazy(LazyThreadSafetyMode.NONE) {
        CustomHomeRecentVGameAdapter(itemView.context, childEventHelper)
    }

    override val gameChangedNotifier: IGameChangedNotifier
        get() = adapter

    init {
        binding.recyclerView.itemAnimator = null
        binding.recyclerView.addItemDecoration(
            GridSpacingItemColorDecoration(binding.root.context, 4, 0, com.gh.gamecenter.common.R.color.transparent)
        )
        binding.recyclerView.addOnScrollListener(object : OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                val layoutManager = binding.recyclerView.layoutManager
                if (layoutManager is LinearLayoutManager) {
                    val scrollToEnd =
                        layoutManager.findLastCompletelyVisibleItemPosition() == (binding.recyclerView.adapter!!.itemCount - 1)
                    binding.divider.visibleIf(scrollToEnd)
                }
            }
        })

        binding.moreTv.setOnClickListener {
            childEventHelper.trackCWRecentlyPlayedClick("更多", null)
            binding.root.context.startActivity(
                VDownloadManagerActivity.getIntent(
                    binding.root.context,
                    LOCATION_CUSTOM_PAGE,
                    pageLocation
                )
            )
        }

        binding.vspaceIv.setOnClickListener {
            childEventHelper.trackCWRecentlyPlayedClick("畅玩广场", null)
            NewFlatLogUtils.logHaloFunEvent("halo_fun_manage_square_entrance_click")
            VHelper.startVSpaceSquare(
                it.context, ExposureEvent.createEvent(
                    null,
                    listOf(
                        ExposureSource("自定义页面", "${pageLocation.pageName}+${pageLocation.pageId}"),
                        ExposureSource("最近在玩")
                    )
                )
            )
        }
    }

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

            fillExposureEventList(item)

            val entities = item.data
            if (entities.isEmpty()) {
                binding.clRoot.goneIf(true)
                binding.clRoot.updateLayoutParams {
                    height = if (bindingAdapterPosition == 0) {
                        1
                    } else {
                        0
                    }
                    width = LayoutParams.MATCH_PARENT
                    if (this is MarginLayoutParams) {
                        topMargin = 0
                    }
                }
            } else {
                binding.clRoot.goneIf(false)
                binding.clRoot.updateLayoutParams {
                    height = 122F.dip2px()
                    width = LayoutParams.MATCH_PARENT
                    if (this is MarginLayoutParams) {
                        topMargin = 16F.dip2px()
                    }
                }

                binding.divider.goneIf(!showVGameSquare)
                binding.vspaceIv.goneIf(!showVGameSquare)

                if (binding.recyclerView.adapter == null) {
                    binding.recyclerView.adapter = adapter
                    binding.moreTv.visibility = View.VISIBLE
                }
                adapter.submitList(entities)
            }

            val bindingAdapter = bindingAdapter
            if (bindingAdapter is CustomPageAdapter) {
                bindingAdapter.updateFirstItemId()
            }
        }
    }

    private fun fillExposureEventList(item: CustomRecentGamesItem) {
        runOnIoThread(isLightWeightTask = true) {
            item.data.forEachIndexed { index, vGame ->
                vGame.exposureEvent = createExposureEvent(
                    VHelper.toGameEntity(vGame.downloadEntity),
                    listOf(ExposureSource("最近在玩", "")),
                    pageConfigure.exposureSourceList,
                    index,
                    item.componentPosition,
                    createTrackData(item)
                )
            }
        }
    }
}

