package kr.co.vividnext.sodalive.audio_content.comment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemAudioContentCommentBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

class AudioContentCommentAdapter(
    private val onItemClick: (GetAudioContentCommentListItem) -> Unit
) : RecyclerView.Adapter<AudioContentCommentAdapter.ViewHolder>() {

    var items = mutableSetOf<GetAudioContentCommentListItem>()

    inner class ViewHolder(
        private val binding: ItemAudioContentCommentBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: GetAudioContentCommentListItem) {
            binding.ivCommentProfile.load(item.profileUrl) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                transformations(CircleCropTransformation())
            }

            val tvCommentLayoutParams = binding.tvComment.layoutParams as LinearLayout.LayoutParams
            val can = item.donationCan
            if (can > 0) {
                tvCommentLayoutParams.topMargin = 0
                binding.llDonationCan.visibility = View.VISIBLE
                binding.tvDonationCan.text = can.moneyFormat()
                binding.llDonationCan.setBackgroundResource(
                    when {
                        can >= 100000 -> {
                            R.drawable.bg_round_corner_10_7_973a3a
                        }

                        can >= 50000 -> {
                            R.drawable.bg_round_corner_10_7_d85e37
                        }

                        can >= 10000 -> {
                            R.drawable.bg_round_corner_10_7_d38c38
                        }

                        can >= 5000 -> {
                            R.drawable.bg_round_corner_10_7_59548f
                        }

                        can >= 1000 -> {
                            R.drawable.bg_round_corner_10_7_4d6aa4
                        }

                        can >= 500 -> {
                            R.drawable.bg_round_corner_10_7_2d7390
                        }

                        else -> {
                            R.drawable.bg_round_corner_10_7_548f7d
                        }
                    }
                )
            } else {
                tvCommentLayoutParams.topMargin = 13.3f.dpToPx().toInt()
                binding.llDonationCan.visibility = View.GONE
            }
            binding.tvComment.layoutParams = tvCommentLayoutParams

            binding.tvComment.text = item.comment
            binding.tvCommentDate.text = item.date
            binding.tvCommentNickname.text = item.nickname

            binding.tvWriteReply.text = if (item.replyCount > 0) {
                "답글 ${item.replyCount}개"
            } else {
                "답글 쓰기"
            }

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

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

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

    override fun getItemCount() = items.size
}
