package kr.co.vividnext.sodalive.explorer

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemExplorerSectionBinding

class ExplorerSectionAdapter(
    private val onClickItem: (Long) -> Unit,
    private val isVisibleRanking: Boolean
) : RecyclerView.Adapter<ExplorerSectionAdapter.ViewHolder>() {

    private val items = mutableListOf<GetExplorerSectionCreatorResponse>()

    inner class ViewHolder(
        private val binding: ItemExplorerSectionBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetExplorerSectionCreatorResponse, index: Int) {
            binding.root.setOnClickListener { onClickItem(item.id) }
            binding.tvNickname.text = item.nickname
            binding.tvTags.text = item.tags

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

            if (isVisibleRanking) {
                when (index) {
                    0 -> {
                        binding.ivBg.setImageResource(R.drawable.bg_circle_ffdc00_ffb600)
                        binding.ivBg.visibility = View.VISIBLE

                        binding.ivCrown.setImageResource(R.drawable.ic_crown_1)
                        binding.ivCrown.visibility = View.VISIBLE
                    }

                    1 -> {
                        binding.ivBg.setImageResource(R.drawable.bg_circle_ffffff_9f9f9f)
                        binding.ivBg.visibility = View.VISIBLE

                        binding.ivCrown.setImageResource(R.drawable.ic_crown_2)
                        binding.ivCrown.visibility = View.VISIBLE
                    }

                    2 -> {
                        binding.ivBg.setImageResource(R.drawable.bg_circle_e6a77a_c67e4a)
                        binding.ivBg.visibility = View.VISIBLE

                        binding.ivCrown.setImageResource(R.drawable.ic_crown_3)
                        binding.ivCrown.visibility = View.VISIBLE
                    }

                    else -> {
                        binding.ivBg.setImageResource(0)
                        binding.ivBg.visibility = View.GONE
                        binding.ivCrown.visibility = View.GONE
                    }
                }
            }
        }
    }

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

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

    override fun getItemCount() = items.size

    @SuppressLint("NotifyDataSetChanged")
    fun addItems(items: List<GetExplorerSectionCreatorResponse>) {
        this.items.addAll(items)
        notifyDataSetChanged()
    }
}
