package com.gh.gamecenter.category2

import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.SubCategoryItemBinding
import com.gh.gamecenter.entity.CategoryEntity

class SubCategoryAdapter(
    private val listener: SearchCategoryPop.OnSearchCategoryListener,
) : RecyclerView.Adapter<SubCategoryAdapter.SubCategoryItemViewHolder>() {

    private lateinit var itemData: CategoryEntity
    private val data: List<CategoryEntity>
        get() = itemData.data ?: emptyList()

    private var directoryPosition = 0

    @SuppressLint("NotifyDataSetChanged")
    fun setData(directoryPosition: Int, newItem: CategoryEntity) {
        this.directoryPosition = directoryPosition
        itemData = newItem
        notifyDataSetChanged()
    }

    override fun getItemCount() = data.size

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

    override fun onBindViewHolder(holder: SubCategoryItemViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            val item = data[position]
            updateSelectedState(item.selected, holder.binding)
        }
    }

    override fun onBindViewHolder(holder: SubCategoryItemViewHolder, position: Int) {
        holder.binding.run {
            val categoryEntity = data[position]
            tvName.text = categoryEntity.name
            recommendIv.goneIf(categoryEntity.recommend == false)

            updateSelectedState(categoryEntity.selected, this)

            root.setOnClickListener {
                when {
                    !categoryEntity.selected && !listener.isEnableSelected() -> {
                        ToastUtils.toast(R.string.selected_category_tags_max_toast.toResString())
                    }

                    categoryEntity.selected -> {

                        listener.onItemRemoved(itemData.id, categoryEntity.id)
                    }

                    !categoryEntity.selected -> {

                        listener.onItemSelected(
                            CategoryV2ViewModel.SelectedTags(
                                itemData.id,
                                itemData.name ?: "",
                                directoryPosition,
                                categoryEntity,
                                position
                            )
                        )
                    }
                }
            }
        }
    }

    private fun updateSelectedState(isSelected: Boolean, binding: SubCategoryItemBinding) {
        with(binding) {
            val context = root.context
            if (isSelected) {
                container.background = R.drawable.bg_category_selected.toDrawable(context)
                tvName.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            } else {
                container.background = com.gh.gamecenter.common.R.drawable.bg_shape_space_radius_8.toDrawable(context)
                tvName.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(context))
            }
        }
    }

    class SubCategoryItemViewHolder(val binding: SubCategoryItemBinding) : ViewHolder(binding.root)
}