package com.gh.gamecenter.minigame.qq

import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.databinding.ItemHomeRecentVgameBinding
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.minigame.MiniGameRecentlyPlayListAdapter
import com.gh.gamecenter.minigame.MiniGameRecentlyPlayUseCase

class QGameRecentlyPlayViewHolder(
    private val lifecycleOwner: LifecycleOwner,
    val binding: ItemHomeRecentVgameBinding
) : RecyclerView.ViewHolder(binding.root) {

    private val adapter by lazy {
        MiniGameRecentlyPlayListAdapter(binding.root.context)
    }

    fun bindView(subject: SubjectEntity) {
        binding.vspaceIv.visibility = View.GONE
        binding.divider.visibility = View.GONE

        if (subject.showName) {
            binding.titleTv.visibility = View.VISIBLE
            binding.titleTv.text = subject.getFilterName()
        } else {
            binding.titleTv.visibility = View.GONE
        }

        val text = if ("change" == subject.home) {
            "换一批"
        } else {
            when (subject.home) {
                "more" -> "更多"
                "hide" -> ""
                else -> "全部"
            }
        }
        binding.moreTv.text = text

        if (subject.indexRightTop != null && subject.indexRightTop != "none") {
            // 开测表用到的
            binding.moreTv.visibility = View.VISIBLE
            if (subject.indexRightTop == "all") {
                binding.moreTv.text = "全部"
            } else {
                binding.moreTv.text = "更多"
            }
        } else if (text == "全部"
            && subject.data != null
            && subject.data!!.size >= (subject.more ?: 0)
        ) {
            binding.moreTv.visibility = View.GONE
        } else if (subject.home == "hide") {
            binding.moreTv.visibility = View.GONE
        } else {
            binding.moreTv.visibility = View.VISIBLE
        }
        if (binding.moreTv.visibility == View.VISIBLE && (binding.moreTv.text == "更多" || binding.moreTv.text == "全部")) {
            binding.moreTv.visibility = View.VISIBLE
        }

        if (binding.recyclerView.adapter == null) {
            val layoutManager =
                LinearLayoutManager(binding.root.context, RecyclerView.HORIZONTAL, false)

            binding.recyclerView.layoutManager = layoutManager
            binding.recyclerView.itemAnimator = null
            binding.recyclerView.adapter = adapter
            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) {
                    if (dx == 0) {
                        val scrollToEnd =
                            layoutManager.findLastCompletelyVisibleItemPosition() == (binding.recyclerView.adapter!!.itemCount - 1)
                        // 当游戏数量超过一屏时，需去除列表右侧的分隔线；去除后，列表最后一个游戏图标与卡片右侧的边距应为16dp
                        if (scrollToEnd) {
                            binding.recyclerViewContainer.post {
                                binding.recyclerViewContainer.layoutParams =
                                    (binding.recyclerViewContainer.layoutParams as MarginLayoutParams).apply {
                                        rightMargin = 10F.dip2px()
                                    }
                            }
                            binding.divider.visibility = View.VISIBLE

                        } else {
                            binding.recyclerViewContainer.post {
                                binding.recyclerViewContainer.layoutParams =
                                    (binding.recyclerViewContainer.layoutParams as MarginLayoutParams).apply {
                                        rightMargin = 0
                                    }
                            }
                            binding.divider.visibility = View.GONE
                        }
                    }
                }
            })
        }

    }

    private val observer = Observer<List<GameEntity>>(adapter::submitList)

    fun onAttached() {
        MiniGameRecentlyPlayUseCase.qqRecentGamesItemLiveData.observe(lifecycleOwner, observer)
    }

    fun onDetached() {
        MiniGameRecentlyPlayUseCase.qqRecentGamesItemLiveData.removeObserver(observer)
    }
}


