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 androidx.viewbinding.ViewBinding
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.databinding.ItemAudioContentCommentReplyBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

class AudioContentCommentReplyAdapter(
    private val creatorId: Long,
    private val modifyComment: (Long, String) -> Unit,
    private val onClickDelete: (Long) -> Unit
) : RecyclerView.Adapter<AudioContentCommentReplyViewHolder>() {

    var items = mutableSetOf<GetAudioContentCommentListItem>()

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): AudioContentCommentReplyViewHolder {
        return if (viewType == 0) {
            AudioContentCommentReplyHeaderViewHolder(
                binding = ItemAudioContentCommentBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),
            )
        } else {
            AudioContentCommentReplyItemViewHolder(
                context = parent.context,
                creatorId = creatorId,
                ItemAudioContentCommentReplyBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),
                showOptionMenu = { context, view, commentId, writerId, creatorId, onClickModify ->
                    showOptionMenu(context, view, commentId, writerId, creatorId, onClickModify)
                },
                modifyComment = modifyComment
            )
        }
    }

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

    override fun getItemCount() = items.size

    override fun getItemViewType(position: Int): Int {
        return position
    }

    private fun showOptionMenu(
        context: Context,
        v: View,
        commentId: Long,
        writerId: Long,
        creatorId: Long,
        onClickModify: () -> 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(commentId)
                }
            }

            true
        }

        popup.show()
    }
}

abstract class AudioContentCommentReplyViewHolder(
    binding: ViewBinding
) : RecyclerView.ViewHolder(binding.root) {
    abstract fun bind(item: GetAudioContentCommentListItem)
}

class AudioContentCommentReplyHeaderViewHolder(
    private val binding: ItemAudioContentCommentBinding
) : AudioContentCommentReplyViewHolder(binding) {

    override fun bind(item: GetAudioContentCommentListItem) {
        binding.ivCommentProfile.load(item.profileUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            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 >= 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.visibility = View.GONE
        binding.ivMenu.visibility = View.GONE
    }
}

class AudioContentCommentReplyItemViewHolder(
    private val context: Context,
    private val creatorId: Long,
    private val binding: ItemAudioContentCommentReplyBinding,
    private val showOptionMenu: (
        Context, View, Long, Long, Long, onClickModify: () -> Unit
    ) -> Unit,
    private val modifyComment: (Long, String) -> Unit
) : AudioContentCommentReplyViewHolder(binding) {

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

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

        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,
                    item.id,
                    item.writerId,
                    creatorId
                ) {
                    binding.rlCommentModify.visibility = View.VISIBLE
                    binding.tvComment.visibility = View.GONE
                }
            }

            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
        }
    }
}
