package kr.co.vividnext.sodalive.explorer.profile.donation

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.ItemUserProfileDonationBinding
import kr.co.vividnext.sodalive.explorer.profile.UserDonationRankingResponse

class UserProfileDonationAdapter : RecyclerView.Adapter<UserProfileDonationAdapter.ViewHolder>() {

    val items = mutableListOf<UserDonationRankingResponse>()

    inner class ViewHolder(
        private val binding: ItemUserProfileDonationBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: UserDonationRankingResponse, position: Int) {
            binding.tvNickname.text = item.nickname

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

            when (position) {
                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(
        ItemUserProfileDonationBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

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

    override fun getItemCount() = items.count()
}
