package com.gh.gamecenter.subject.tab

import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.debounceActionWithInterval
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.view.Chips
import com.gh.gamecenter.databinding.ItemColumnCollectionCustomTabBinding
import com.gh.gamecenter.entity.SubjectData

class CustomTabMoreAdapter(private val viewModel: ColumnCollectionTabViewModel?) :
    ListAdapter<SubjectData, CustomTabMoreAdapter.CustomTabMoreItemViewHolder>(createDiffItemCallBack()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomTabMoreItemViewHolder {
        return CustomTabMoreItemViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: CustomTabMoreItemViewHolder, position: Int) {
        val subjectData = currentList.getOrNull(position) ?: return
        holder.binding.chips.run {
            setState(Chips.STATE_DEFAULT)
            setText(subjectData.subjectName ?: "")
            startIcon.isVisible = true
            startIcon.setImageResource(R.drawable.ic_auxiliary_plus_secondary_8)
            startIcon.imageTintList = null
            setOnClickListener {
                debounceActionWithInterval(500) {
                    viewModel?.addFollow(subjectData)
                }
            }
        }
    }

    class CustomTabMoreItemViewHolder(val binding: ItemColumnCollectionCustomTabBinding) : ViewHolder(binding.root)

    companion object {

        fun createDiffItemCallBack() = object : ItemCallback<SubjectData>() {
            override fun areItemsTheSame(oldItem: SubjectData, newItem: SubjectData): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: SubjectData, newItem: SubjectData): Boolean {
                return oldItem == newItem
            }

        }
    }
}