package kr.co.vividnext.sodalive.audio_content.main.new_content

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemAudioContentMainNewContentThemeBinding

class AudioContentMainNewContentThemeAdapter(
    private val onClickItem: (String) -> Unit
) : RecyclerView.Adapter<AudioContentMainNewContentThemeAdapter.ViewHolder>() {

    private val themeList = mutableListOf<String>()
    private var selectedTheme = ""

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemAudioContentMainNewContentThemeBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("NotifyDataSetChanged")
        fun bind(theme: String) {
            if (
                theme == selectedTheme ||
                (selectedTheme == "" && theme == "전체") ||
                (selectedTheme == "" && theme == "매출")
            ) {
                binding.tvTheme.setBackgroundResource(
                    R.drawable.bg_round_corner_16_7_transparent_3bb9f1
                )
                binding.tvTheme.setTextColor(ContextCompat.getColor(context, R.color.color_3bb9f1))
            } else {
                binding.tvTheme.setBackgroundResource(
                    R.drawable.bg_round_corner_16_7_transparent_777777
                )
                binding.tvTheme.setTextColor(ContextCompat.getColor(context, R.color.color_777777))
            }

            binding.tvTheme.text = theme
            binding.root.setOnClickListener {
                onClickItem(theme)
                selectedTheme = theme
                notifyDataSetChanged()
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addItems(themeList: List<String>) {
        this.selectedTheme = ""
        this.themeList.clear()
        this.themeList.addAll(themeList)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        parent.context,
        ItemAudioContentMainNewContentThemeBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun getItemCount() = themeList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(themeList[position])
    }
}
