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

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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.ItemCreatorCommunityAllBinding
import kr.co.vividnext.sodalive.explorer.profile.creator_community.GetCommunityPostListResponse
import kr.co.vividnext.sodalive.extensions.loadUrl

class CreatorCommunityAllAdapter(
    private val onClickLike: (Long) -> Unit,
    private val writeComment: (Long, Long?, String) -> Unit,
    private val showCommentBottomSheetDialog: (Long) -> Unit
) : RecyclerView.Adapter<CreatorCommunityAllAdapter.ViewHolder>() {

    val items = mutableListOf<GetCommunityPostListResponse>()

    inner class ViewHolder(
        private val binding: ItemCreatorCommunityAllBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("NotifyDataSetChanged")
        fun bind(item: GetCommunityPostListResponse, index: Int) {
            binding.tvNickname.text = item.creatorNickname
            binding.ivCreatorProfile.loadUrl(item.creatorProfileUrl) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                transformations(CircleCropTransformation())
            }

            binding.tvContent.text = item.content

            binding.ivLike.setImageResource(
                if (item.isLike) {
                    R.drawable.ic_audio_content_heart_pressed
                } else {
                    R.drawable.ic_audio_content_heart_normal
                }
            )

            binding.tvLike.text = "${item.likeCount}"
            binding.llLike.setOnClickListener {
                val isLike = !item.isLike

                items[index] = items[index].copy(
                    isLike = !item.isLike,
                    likeCount = if (isLike) item.likeCount + 1 else item.likeCount - 1
                )
                notifyDataSetChanged()

                onClickLike(item.postId)
            }

            if (item.imageUrl != null) {
                binding.ivContent.visibility = View.VISIBLE
                binding.ivContent.loadUrl(item.imageUrl) {
                    crossfade(true)
                    placeholder(R.drawable.bg_placeholder)
                }
            } else {
                binding.ivContent.visibility = View.GONE
            }

            if (item.isCommentAvailable) {
                binding.llComment.visibility = View.VISIBLE
                binding.tvCommentCount.text = "${item.commentCount}"
            } else {
                binding.llComment.visibility = View.GONE
            }

            if (item.commentCount > 0) {
                binding.ivCommentProfile.load(item.firstComment!!.profileUrl) {
                    crossfade(true)
                    placeholder(R.drawable.bg_placeholder)
                    transformations(CircleCropTransformation())
                }
                binding.tvCommentText.text = item.firstComment.comment
                binding.tvCommentText.visibility = View.VISIBLE
                binding.rlInputComment.visibility = View.GONE

                binding.llComment.setOnClickListener { showCommentBottomSheetDialog(item.postId) }
            } else {
                binding.tvCommentText.visibility = View.GONE
                binding.rlInputComment.visibility = View.VISIBLE
                binding.ivCommentProfile.load(SharedPreferenceManager.profileImage) {
                    crossfade(true)
                    placeholder(R.drawable.bg_placeholder)
                    transformations(CircleCropTransformation())
                }

                binding.ivCommentSend.setOnClickListener {
                    val comment = binding.etComment.text.toString()
                    binding.etComment.setText("")
                    writeComment(item.postId, null, comment)
                }

                binding.llComment.setOnClickListener {}
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        ItemCreatorCommunityAllBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun getItemCount() = items.size

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