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

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.gamecenter.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toResString
import com.gh.gamecenter.common.utils.visibleIf
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.databinding.ItemHomeRecentVgameCustomBinding
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.CustomPageAdapter
import com.gh.gamecenter.home.custom.adapter.CustomRecentAcceleratorAdapter
import com.gh.gamecenter.home.custom.eventlistener.RecentMiniGameItemEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomRecentAcceleratorItem

/**
 * 最近在玩-加速器快速启动
 */
class CustomRecentAcceleratorViewHolder(
    viewModel: CustomPageViewModel,
    var binding: ItemHomeRecentVgameCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

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

    private val adapter by lazy(LazyThreadSafetyMode.NONE) {
        CustomRecentAcceleratorAdapter(itemView.context)
    }

    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)
                    val item = _item
                    if (item is CustomRecentAcceleratorItem && item.moreLink != null) {
                        binding.divider.visibleIf(scrollToEnd)
                    }
                }
            }
        })
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        binding.moreTv.goneIf(true)

        if (item is CustomRecentAcceleratorItem) {

            if (item.moreLink != null) {
                binding.vspaceIv.goneIf(false)
                binding.vspaceIv.setImageResource(R.drawable.ic_accelerator_recently_played)
                binding.vspaceIv.setOnClickListener {
                    val link = item.moreLink
                    viewModel.navigateToLinkPage(item, link, "更多", null)
                }
            } else {
                binding.vspaceIv.goneIf(true)
            }
            binding.divider.goneIf(item.moreLink == null)

            binding.titleTv.text = R.string.recent_played.toResString()
            val entities = item.data
            if (entities.isEmpty()) {
                binding.root.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()
                    }
                }

                if (binding.recyclerView.adapter == null) {
                    binding.recyclerView.adapter = adapter
                }
                adapter.submitList(entities)
            }

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