package kr.co.vividnext.sodalive.live.room.tag

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.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemLiveTagBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LiveTagAdapter(
    private val selectedTags: Set<String>,
    private val onItemClick: (String, Boolean) -> Boolean
) : RecyclerView.Adapter<LiveTagAdapter.ViewHolder>() {
    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemLiveTagBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        private var isChecked = false

        fun bind(item: GetLiveTagResponse) {
            if (selectedTags.contains(item.tag)) {
                binding.ivTagChecked.visibility = View.VISIBLE
                binding.tvTag.setTextColor(ContextCompat.getColor(context, R.color.color_3bb9f1))
                isChecked = true
            } else {
                binding.ivTagChecked.visibility = View.GONE
                binding.tvTag.setTextColor(ContextCompat.getColor(context, R.color.color_bbbbbb))
                isChecked = false
            }

            binding.ivTag.load(item.image) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(30f.dpToPx()))
            }
            binding.tvTag.text = item.tag

            binding.root.setOnClickListener {
                isChecked = !isChecked

                if (onItemClick(item.tag, isChecked)) {
                    if (isChecked) {
                        binding.ivTagChecked.visibility = View.VISIBLE
                        binding.tvTag.setTextColor(
                            ContextCompat.getColor(
                                context,
                                R.color.color_3bb9f1
                            )
                        )
                    } else {
                        binding.ivTagChecked.visibility = View.GONE
                        binding.tvTag.setTextColor(
                            ContextCompat.getColor(
                                context,
                                R.color.color_bbbbbb
                            )
                        )
                    }
                } else {
                    isChecked = !isChecked
                }
            }
        }
    }

    val items = mutableSetOf<GetLiveTagResponse>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context,
            ItemLiveTagBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

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

    override fun getItemCount() = items.size
}
