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

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
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.ItemLiveRoomDonationRankingBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

class LiveRoomDonationRankingAdapter :
    RecyclerView.Adapter<LiveRoomDonationRankingAdapter.ViewHolder>() {
    val items = mutableListOf<GetLiveRoomDonationItem>()

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

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

    override fun getItemCount() = items.count()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemLiveRoomDonationRankingBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bind(item: GetLiveRoomDonationItem, position: Int) {
            binding.tvRank.text = "${position + 1}"
            binding.tvNickname.text = item.nickname

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

            binding.tvDonationCan.text = item.can.moneyFormat()
            binding.tvDonationSecretCan.text = item.secretCan.moneyFormat()
            binding.llDonationSecret.visibility = if (item.secretCan > 0) {
                View.VISIBLE
            } else {
                View.GONE
            }

            val lp = binding.rlDonationRanking.layoutParams as FrameLayout.LayoutParams

            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
                    binding.root.setBackgroundResource(
                        if (items.size == 1) {
                            R.drawable.bg_round_corner_4_7_13181b
                        } else {
                            R.drawable.bg_top_round_corner_4_7_13181b
                        }
                    )

                    lp.setMargins(
                        0,
                        20.dpToPx().toInt(),
                        0,
                        if (items.size == 1) {
                            20.dpToPx().toInt()
                        } else {
                            13.3f.dpToPx().toInt()
                        }
                    )
                    binding.rlDonationRanking.layoutParams = lp
                }

                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

                    if (items.size == 2) {
                        binding.root.setBackgroundResource(
                            R.drawable.bg_bottom_round_corner_4_7_13181b
                        )
                    } else {
                        binding.root.setBackgroundColor(
                            ContextCompat.getColor(context, R.color.color_13181b)
                        )
                    }

                    lp.setMargins(
                        0,
                        0,
                        0,
                        if (items.size == 2) {
                            20.dpToPx().toInt()
                        } else {
                            13.3f.dpToPx().toInt()
                        }
                    )
                    binding.rlDonationRanking.layoutParams = lp
                }

                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
                    binding.root.setBackgroundResource(
                        R.drawable.bg_bottom_round_corner_4_7_13181b
                    )

                    lp.setMargins(
                        0,
                        0,
                        0,
                        20.dpToPx().toInt()
                    )
                    binding.rlDonationRanking.layoutParams = lp
                }

                else -> {
                    binding.ivBg.setImageResource(0)
                    binding.ivBg.visibility = View.GONE
                    binding.ivCrown.visibility = View.GONE
                    binding.root.setBackgroundResource(0)
                    binding.root.background = null

                    lp.setMargins(0, 0, 0, 0)
                    binding.rlDonationRanking.layoutParams = lp
                }
            }
        }
    }
}
