package kr.co.vividnext.sodalive.explorer.profile.creator_community.all.comment

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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.ItemCommunityPostCommentBinding
import kr.co.vividnext.sodalive.explorer.profile.creator_community.GetCommunityPostCommentListItem

class CreatorCommunityCommentAdapter(
    private val creatorId: Long,
    private val modifyComment: (Long, String) -> Unit,
    private val onClickDelete: (Long) -> Unit,
    private val onItemClick: (GetCommunityPostCommentListItem) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : RecyclerView.Adapter<CreatorCommunityCommentAdapter.ViewHolder>() {
    var items = mutableSetOf<GetCommunityPostCommentListItem>()

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

        fun bind(item: GetCommunityPostCommentListItem) {
            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)
                }
            }

            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,
                        commentId = item.id,
                        writerId = item.writerId,
                        creatorId = creatorId,
                        onClickModify = {
                            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
            }

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

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