package kr.co.vividnext.sodalive.explorer.profile.cheers

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.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ItemUserProfileCheersBinding
import kr.co.vividnext.sodalive.explorer.profile.GetCheersResponseItem
import kr.co.vividnext.sodalive.extensions.dpToPx

class UserProfileCheersAdapter(
    private val userId: Long,
    private val enterReply: (Long, String) -> Unit,
    private val modifyReply: (Long, String?) -> Unit,
    private val modifyCheers: (Long, String) -> Unit,
    private val onClickReport: (Long) -> Unit,
    private val onClickDelete: (Long) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : RecyclerView.Adapter<UserProfileCheersAdapter.ViewHolder>() {

    val items = mutableListOf<GetCheersResponseItem>()

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

        fun bind(cheers: GetCheersResponseItem) {
            binding.tvWriteReply.visibility = View.GONE
            binding.llCheerReply.visibility = View.GONE
            binding.rlCheerReply.visibility = View.GONE

            binding.ivProfile.setOnClickListener {
                if (SharedPreferenceManager.userId != cheers.memberId) {
                    onClickProfile(cheers.memberId)
                }
            }

            binding.ivProfile.load(cheers.profileUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(16.7f.dpToPx()))
            }
            binding.tvContent.text = cheers.content
            binding.tvNickname.text = cheers.nickname
            binding.tvDate.text = cheers.date

            if (
                cheers.memberId == SharedPreferenceManager.userId ||
                userId == SharedPreferenceManager.userId
            ) {
                binding.etContentModify.setText(cheers.content)
                binding.ivMenu.visibility = View.VISIBLE
                binding.ivMenu.setOnClickListener {
                    showOptionMenu(
                        context,
                        binding.ivMenu,
                        cheersId = cheers.cheersId,
                        memberId = cheers.memberId,
                        creatorId = userId,
                        onClickModify = {
                            binding.rlContentModify.visibility = View.VISIBLE
                            binding.tvContent.visibility = View.GONE
                        },
                        onClickDelete = {
                            binding.rlContentModify.visibility = View.GONE
                            binding.tvContent.visibility = View.VISIBLE
                            onClickDelete(cheers.cheersId)
                        }
                    )
                }

                binding.tvModify.setOnClickListener {
                    binding.rlContentModify.visibility = View.GONE
                    binding.tvContent.visibility = View.VISIBLE
                    modifyCheers(cheers.cheersId, binding.etContentModify.text.toString())
                }
            } else {
                binding.ivMenu.visibility = View.GONE
            }

            if (cheers.replyList.isNotEmpty()) {
                binding.tvWriteReply.visibility = View.GONE
                binding.llCheerReply.visibility = View.VISIBLE
                val reply = cheers.replyList[0]
                binding.tvReply.text = reply.content
                binding.tvReplyDate.text = reply.date
                if (userId == SharedPreferenceManager.userId) {
                    binding.tvModifyReply.visibility = View.VISIBLE
                    binding.tvModifyReply.setOnClickListener {
                        binding.etCheerReply.setText(binding.tvReply.text)
                        binding.rlCheerReply.visibility = View.VISIBLE
                        binding.tvSend.setOnClickListener {
                            val content = binding.etCheerReply.text.toString()
                            modifyReply(reply.cheersId, content)
                        }
                    }
                } else {
                    binding.tvModifyReply.visibility = View.GONE
                }
            } else {
                if (userId == SharedPreferenceManager.userId) {
                    binding.tvWriteReply.visibility = View.VISIBLE
                    binding.tvWriteReply.setOnClickListener {
                        binding.tvWriteReply.visibility = View.GONE
                        binding.rlCheerReply.visibility = View.VISIBLE
                        binding.tvSend.setOnClickListener {
                            val content = binding.etCheerReply.text.toString()
                            enterReply(cheers.cheersId, content)
                        }
                    }
                } else {
                    binding.tvWriteReply.visibility = View.GONE
                }
            }

            binding.tvReply.requestLayout()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context,
            ItemUserProfileCheersBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

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

    override fun getItemCount() = items.count()

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

        if (memberId == SharedPreferenceManager.userId) {
            inflater.inflate(R.menu.review_option_menu3, popup.menu)
        } else if (creatorId == SharedPreferenceManager.userId) {
            inflater.inflate(R.menu.review_option_menu2, popup.menu)
        } else {
            inflater.inflate(R.menu.review_option_menu, popup.menu)
        }

        popup.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.menu_review_report -> {
                    onClickReport(cheersId)
                }

                R.id.menu_review_modify -> {
                    onClickModify()
                }

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

            true
        }

        popup.show()
    }
}
