package com.gh.gamecenter.category2

import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.databinding.CategoryDirectoryItemBinding
import com.gh.gamecenter.entity.CategoryEntity

class CategoryDirectoryAdapter(
    private val listener: SearchCategoryPop.OnSearchCategoryListener
) : RecyclerView.Adapter<CategoryDirectoryAdapter.CategoryDirectoryItemViewHolder>() {

    private val data = arrayListOf<CategoryEntity>()

    @SuppressLint("NotifyDataSetChanged")
    fun setListData(newData: List<CategoryEntity>) {
        data.clear()
        data.addAll(newData)
        notifyDataSetChanged()
    }

    override fun getItemCount() = data.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        CategoryDirectoryItemViewHolder(listener, parent.toBinding())

    override fun onBindViewHolder(holder: CategoryDirectoryItemViewHolder, position: Int) {
        holder.onBind(position, data[position])
    }

    override fun onBindViewHolder(holder: CategoryDirectoryItemViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            holder.notifyItemSelectedChanged()
        }

    }

    fun notifyItemSelectedChanged(parentId: String) {
        val position = data.indexOfFirst { it.id == parentId }
        if (position != -1) {
            notifyItemChanged(position, "")
        }
    }

    class CategoryDirectoryItemViewHolder(
        private val listener: SearchCategoryPop.OnSearchCategoryListener,
        val binding: CategoryDirectoryItemBinding
    ) :
        ViewHolder(binding.root) {

        private val childAdapter by lazy {
            SubCategoryAdapter(listener)
        }

        fun onBind(position: Int, item: CategoryEntity) {

            val context = binding.root.context
            binding.title.text = item.name

            if (binding.subCategoryRv.adapter == null) {
                binding.subCategoryRv.layoutManager = object : GridLayoutManager(context, 4) {
                    override fun canScrollVertically(): Boolean {
                        return false
                    }
                }
                binding.subCategoryRv.adapter = childAdapter
                binding.subCategoryRv.addItemDecoration(
                    GridSpacingItemColorDecoration(
                        context,
                        8,
                        8,
                        com.gh.gamecenter.common.R.color.transparent
                    )
                )
            }
            childAdapter.setData(position, item)
        }

        fun notifyItemSelectedChanged() {
            childAdapter.notifyItemRangeChanged(0, childAdapter.itemCount, "")
        }
    }
}