package kr.co.vividnext.sodalive.audio_content.upload.theme

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemAudioContentThemeBinding

class AudioContentThemeAdapter(
    private val selectedTheme: GetAudioContentThemeResponse?,
    private val onItemClick: (GetAudioContentThemeResponse) -> Unit
) : RecyclerView.Adapter<AudioContentThemeAdapter.ViewHolder>() {

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemAudioContentThemeBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private var isChecked = false

        fun bind(item: GetAudioContentThemeResponse) {
            if (selectedTheme == item) {
                binding.ivThemeChecked.visibility = View.VISIBLE
                binding.tvTheme.setTextColor(ContextCompat.getColor(context, R.color.color_9970ff))
                isChecked = true
            } else {
                binding.ivThemeChecked.visibility = View.GONE
                binding.tvTheme.setTextColor(ContextCompat.getColor(context, R.color.color_bbbbbb))
                isChecked = false
            }

            binding.ivTheme.load(item.image) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(CircleCropTransformation())
            }

            binding.tvTheme.text = item.theme

            binding.root.setOnClickListener { onItemClick(item) }
        }
    }

    val items = mutableSetOf<GetAudioContentThemeResponse>()

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

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

    override fun getItemCount() = items.size
}
