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

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.widget.PopupMenu
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
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 creatorId: Long,
    private val modifyComment: (Long, String) -> Unit,
    private val onClickDelete: (Long) -> Unit,
    private val onItemClick: (GetAudioContentCommentListItem) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : RecyclerView.Adapter<AudioContentCommentAdapter.ViewHolder>() {

    var items = mutableSetOf<GetAudioContentCommentListItem>()

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

        fun bind(item: GetAudioContentCommentListItem) {
            binding.tvSecret.visibility = if (item.isSecret) {
                View.VISIBLE
            } else {
                View.GONE
            }

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

            binding.ivCommentProfile.setOnClickListener {
                if (SharedPreferenceManager.userId != item.writerId) {
                    onClickProfile(item.writerId)
                }
            }

            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 >= 10000 -> {
                            R.drawable.bg_round_corner_10_7_973a3a
                        }

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

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

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

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

                        can >= 50 -> {
                            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 {
                "답글 쓰기"
            }

            if (
                item.writerId == SharedPreferenceManager.userId ||
                creatorId == SharedPreferenceManager.userId
            ) {
                binding.etCommentModify.setText(item.comment)
                binding.ivMenu.visibility = View.VISIBLE
                binding.ivMenu.setOnClickListener {
                    showOptionMenu(
                        context,
                        binding.ivMenu,
                        writerId = item.writerId,
                        creatorId = creatorId,
                        onClickModify = {
                            binding.rlCommentModify.visibility = View.VISIBLE
                            binding.tvComment.visibility = View.GONE
                        },
                        onClickDelete = {
                            binding.rlCommentModify.visibility = View.GONE
                            binding.tvComment.visibility = View.VISIBLE
                            onClickDelete(item.id)
                        }
                    )
                }

                binding.tvModify.setOnClickListener {
                    binding.rlCommentModify.visibility = View.GONE
                    binding.tvComment.visibility = View.VISIBLE
                    modifyComment(item.id, binding.etCommentModify.text.toString())
                }
            } else {
                binding.ivMenu.visibility = View.GONE
            }

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        parent.context,
        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

    private fun showOptionMenu(
        context: Context,
        v: View,
        writerId: Long,
        creatorId: Long,
        onClickModify: () -> Unit,
        onClickDelete: () -> Unit
    ) {
        val popup = PopupMenu(context, v)
        val inflater = popup.menuInflater

        if (writerId == SharedPreferenceManager.userId) {
            inflater.inflate(R.menu.content_comment_option_menu, popup.menu)
        } else if (creatorId == SharedPreferenceManager.userId) {
            inflater.inflate(R.menu.content_comment_option_menu2, popup.menu)
        }

        popup.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.menu_review_modify -> {
                    onClickModify()
                }

                R.id.menu_review_delete -> {
                    onClickDelete()
                }
            }

            true
        }

        popup.show()
    }
}
