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 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.ItemCommunityPostCommentBinding
import kr.co.vividnext.sodalive.databinding.ItemCommunityPostCommentReplyBinding
import kr.co.vividnext.sodalive.explorer.profile.creator_community.GetCommunityPostCommentListItem
import kr.co.vividnext.sodalive.extensions.loadUrl

class CreatorCommunityCommentReplyAdapter(
    private val creatorId: Long,
    private val modifyComment: (Long, String) -> Unit,
    private val onClickDelete: (Long) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : RecyclerView.Adapter<CreatorCommunityCommentReplyViewHolder>() {
    var items = mutableSetOf<GetCommunityPostCommentListItem>()
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CreatorCommunityCommentReplyViewHolder {
        return if (viewType == 0) {
            CreatorCommunityCommentReplyHeaderViewHolder(
                binding = ItemCommunityPostCommentBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            CreatorCommunityCommentReplyItemViewHolder(
                context = parent.context,
                creatorId = creatorId,
                binding = ItemCommunityPostCommentReplyBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),
                showOptionMenu = { context, view, commentId, writerId, creatorId, onClickModify ->
                    showOptionMenu(context, view, commentId, writerId, creatorId, onClickModify)
                },
                modifyComment = modifyComment,
                onClickProfile = onClickProfile
            )
        }
    }

    override fun onBindViewHolder(holder: CreatorCommunityCommentReplyViewHolder, 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 CreatorCommunityCommentReplyViewHolder(
    binding: ViewBinding
) : RecyclerView.ViewHolder(binding.root) {
    abstract fun bind(item: GetCommunityPostCommentListItem)
}

class CreatorCommunityCommentReplyHeaderViewHolder(
    private val binding: ItemCommunityPostCommentBinding
) : CreatorCommunityCommentReplyViewHolder(binding) {
    override fun bind(item: GetCommunityPostCommentListItem) {
        binding.ivCommentProfile.loadUrl(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

        binding.tvWriteReply.visibility = View.GONE
        binding.ivMenu.visibility = View.GONE
    }
}

class CreatorCommunityCommentReplyItemViewHolder(
    private val context: Context,
    private val creatorId: Long,
    private val binding: ItemCommunityPostCommentReplyBinding,
    private val showOptionMenu: (
        Context, View, Long, Long, Long, onClickModify: () -> Unit
    ) -> Unit,
    private val modifyComment: (Long, String) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : CreatorCommunityCommentReplyViewHolder(binding) {
    override fun bind(item: GetCommunityPostCommentListItem) {
        binding.ivCommentProfile.setOnClickListener {
            if (SharedPreferenceManager.userId != item.writerId) {
                onClickProfile(item.writerId)
            }
        }
        
        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
        }
    }

}
